jeudi 26 mars 2020

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?

Aucun commentaire:

Enregistrer un commentaire