提问人:explorer2020 提问时间:10/9/2023 更新时间:10/9/2023 访问量:39
如何在 pytest 的参数化测试函数中跳过特定测试
How to skip a particular test in parametrized test functions in pytest
问:
我有一个pytest函数,它有标记@pytest.mark.win_11和@pytest.mark.parametrize('os,result',[('win_11','result1'),('win_8','result2')])
当我以“pytest -m win_11”身份运行 test 时,它正在为两组参数运行测试。我想仅在操作系统版本win_11时运行测试。
那么,我们可以将标记和参数化选项结合起来进行单次测试吗? 当测试以“pytest -m win_11”运行时,有没有办法跳过 os = win_8 的测试?
代码如下所示:
import pytest
@pytest.mark.win_11
@pytest.mark.parametrize('os_version,result',[('win_11','result1'),('win_8','result2')])
def test_set1_1(os_version, expected_result):
# some code
答:
1赞
Guy
10/9/2023
#1
一种选择是通过@pytest.mark.parametrize
def parametrize():
for version, result in [('win_11', 'result1'), ('win_8', 'result2')]:
marks = []
if version == 'win_11':
marks.append(pytest.mark.win_11)
else:
marks.append(pytest.mark.win_8)
yield pytest.param(version, result, marks=marks)
@pytest.mark.parametrize('os_version, result', parametrize())
def test_set1_1(os_version, result):
print(os_version, result)
无记号笔pytest
============================= test session starts =============================
collecting ... collected 2 items
example.py::test_set1_1[win_11-result1] PASSED [ 50%]
win_11 result1
example.py::test_set1_1[win_8-result2] PASSED [100%]
win_8 result2
跟pytest -m win_11
============================= test session starts =============================
collecting ... collected 2 items / 1 deselected / 1 selected
example.py::test_set1_1[win_11-result1] PASSED [100%]
win_11 result1
评论
os_version