pytest中的markers
时间: 2023-10-26 22:16:03 浏览: 42
在 pytest 中,marker 是一种用于标记测试函数或类的装饰器。通过使用 marker,可以在运行测试时指定要运行的测试集合。
例如,可以使用 @pytest.mark.skip 装饰器来跳过某些测试:
```python
import pytest
@pytest.mark.skip(reason="not implemented yet")
def test_something():
assert True
```
也可以使用 @pytest.mark.parametrize 装饰器来指定测试参数:
```python
import pytest
@pytest.mark.parametrize("test_input,expected", [("3+5", 8), ("2+4", 6)])
def test_eval(test_input, expected):
assert eval(test_input) == expected
```
除了内置的 marker,pytest 还支持自定义 marker。可以定义自己的 marker 并使用它来标记测试函数或类。例如,可以定义一个自定义 marker,用于标记某些测试需要在特定的操作系统上运行:
```python
import pytest
@pytest.mark.windows
def test_windows_only():
assert True
@pytest.mark.linux
def test_linux_only():
assert True
```
然后可以使用 --markers 选项列出所有可用的 marker:
```
$ pytest --markers
```
这将显示所有可用的 marker,包括内置的和自定义的。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![whl](https://img-home.csdnimg.cn/images/20210720083646.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)