【问题标题】:PyTest skipping test based on target code versionPyTest 根据目标代码版本跳过测试
【发布时间】: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


    【解决方案1】:

    您离解决方案不远,但您将标记与固定装置混为一谈;即使您给它们起相同的名称,它们也不相同。但是,您可以读取version_check fixture 中每个测试功能的标记,并根据version_check marker 提供的内容跳过测试(如果已设置)。示例:

    @pytest.fixture(autouse=True)
    def version_check(request):
        version_option = int(request.config.getoption('--target-version'))
        # request.node is the current test item
        # query the marker "version_check" of current test item
        version_marker = request.node.get_closest_marker('version_check')
        # if test item was not marked, there's no version restriction
        if version_marker is None:
            return
        # arguments of @pytest.mark.version_check(min_version=10) are in marker.kwargs
        # arguments of @pytest.mark.version_check(0, 1, 2) would be in marker.args
        min_version = version_marker.kwargs.get('min_version', 0)
        max_version = version_marker.kwargs.get('max_version', 10000000)
        # the rest is your logic unchanged
        if version_option < min_version:
            pytest.skip('Version number is lower that versions required to run this test '
                        f'({min_version} vs {version_option})')
        if version_option > max_version:
            pytest.skip('Version number is higher that versions required to run this test '
                        f'({max_version} vs {version_option})')
    

    【讨论】:

      猜你喜欢
      • 2021-07-02
      • 2019-04-30
      • 1970-01-01
      • 2013-03-28
      • 1970-01-01
      • 1970-01-01
      • 2019-10-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多