pytest.mark
时间: 2023-10-30 21:58:04 浏览: 49
`pytest.mark` 是 Pytest 提供的一个装饰器,用于为测试用例打上标记。通过使用标记,可以方便地选择运行指定标记的测试用例。
例如,为一个测试用例打上 `@pytest.mark.smoke` 标记,表示该测试用例属于 smoke 测试,然后在运行测试时,使用命令 `pytest -m smoke` 就可以只运行被标记为 smoke 的测试用例。
除了 `@pytest.mark.smoke`,还有许多其他的标记可以使用,例如 `@pytest.mark.parametrize` 用于参数化测试用例,`@pytest.mark.skip` 用于跳过测试用例等等。
相关问题
pytest.mark.flaky
pytest.mark.flaky 是 pytest 测试框架中的一个装饰器。它用于标记测试用例为“脆弱”的,意味着测试用例可能会因为一些非确定性的原因而偶尔失败。
当测试用例被标记为 flaky 后,pytest 将会重复运行该用例多次,直到达到预设的重试次数或者测试用例通过为止。这个功能特别适用于那些依赖外部环境、网络请求等不稳定因素的测试用例。
使用 @pytest.mark.flaky 装饰器可以为测试函数或方法添加 flaky 标记。例如:
```python
import pytest
@pytest.mark.flaky(reruns=5, reruns_delay=1)
def test_example():
# 测试逻辑...
assert some_condition == True
```
在上面的示例中,test_example() 函数被标记为 flaky,它将会运行 5 次,并且每次运行失败后会延迟 1 秒再次重试,直到达到最大重试次数或者断言成功。
这样的机制可以增加测试的稳定性,尤其适用于那些依赖于外部资源或非确定性因素的测试用例。
pytest.mark.skip
pytest.mark.skip用于跳过测试用例,可以在函数或类的前面使用注解来标记。如果在函数前面使用注解,那么这个函数将被跳过执行。如果在类前面使用注解,那么这个类中的所有测试函数都会被跳过执行。另外,如果希望在整个模块中跳过用例,可以使用pytestmark变量,并将其设置为@pytest.mark.skipif注解,通过设置条件来决定是否跳过用例的执行。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *3* [pytest关键字详解](https://blog.csdn.net/qq_42910365/article/details/119516451)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
- *2* [Pytest跳过执行之@pytest.mark.skip()详解大全](https://blog.csdn.net/qq_36502272/article/details/103303010)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)