Mark2506
Mark2506

Reputation: 11

PyTest skipping test based on target code version

What I am trying to do is to skip tests that are not supported by the code I am testing. My PyTest is running tests against an embedded system that could have different versions of code running. What I want to do mark my test such that they only run if they are supported by the target.

I have added a pytest_addoption method:

def pytest_addoption(parser):
   parser.addoption(
        '--target-version',
        action='store', default='28',
        help='Version of firmware running in target')

Create a fixture to decide if the test should be run:

@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})')

Marking the tests like this:

@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')

In the arguments for running the test I just want to add --target-version 22 and have only the right tests run. I haven't been able to figure out how to pass the arguments from @pytest.mark.version_check(max_version=27), to version_check.

Is there a way to do this or am I completely off track and should be looking at something else to accomplish this?

Upvotes: 1

Views: 364

Answers (1)

hoefling
hoefling

Reputation: 66581

You are not far from a solution, but you're mixing up markers with fixtures; they are not the same, even if you give them the same name. You can, however, read markers of each test function in your version_check fixture and skip the test depending on what was provided by the version_check marker if set. Example:

@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})')

Upvotes: 2

Related Questions