【发布时间】:2020-03-26 20:37:37
【问题描述】:
我想要做的是跳过我正在测试的代码不支持的测试。我的 PyTest 正在针对可能运行不同版本代码的嵌入式系统运行测试。我想要做的标记我的测试,以便它们仅在目标支持时运行。
我添加了pytest_addoption 方法:
def pytest_addoption(parser):
parser.addoption(
'--target-version',
action='store', default='28',
help='Version of firmware running in target')
创建一个夹具来决定是否应该运行测试:
@pytest.fixture(autouse = True)
def version_check(request, min_version: int = 0, max_version: int = 10000000):
version_option = int(request.config.getoption('--target-version'))
if min_version and version_option < min_version:
pytest.skip('Version number is lower that versions required to run this test '
f'({min_version} vs {version_option})')
if max_version and version_option > max_version:
pytest.skip('Version number is higher that versions required to run this test '
f'({max_version} vs {version_option})')
像这样标记测试:
@pytest.mark.version_check(min_version=24)
def test_this_with_v24_or_greater():
print('Test passed')
@pytest.mark.version_check(max_version=27)
def test_not_supported_after_v27():
print('Test passed')
@pytest.mark.version_check(min_version=13, max_version=25)
def test_works_for_range_of_versions():
print('Test passed')
在运行测试的参数中,我只想添加--target-version 22 并且只运行正确的测试。我无法弄清楚如何将参数从@pytest.mark.version_check(max_version=27) 传递到version_check。
有没有办法做到这一点,还是我完全偏离了轨道,应该寻找其他方法来实现这一点?
【问题讨论】:
标签: python testing pytest fixtures