pythonpytestpytest-markers

Pytest test working with different versions of program


I have multiple tests which would work on different versions of my program.

For example testA is working only for versions 2,3,4 and not 5 and later. the other test is working for the test from version 4 and later. based on the pytest documentation, I can create a marker similar to below:

# content of test_mymodule.py
import mymodule

minversion3 = pytest.mark.skipif(
    myprogram.__versioninfo__ < 3, reason="at least version 3 is required"
)


@minversion3
def test_function():
    ...

minversion3 marks the tests to be run provided that program has at least version 3. I would like to parametrize it so that I can have something like this:

@minmaxversion(3.2, 6.1)
def test_function():
    ...

so that this test is only working for programs with version minimum 3.2 and maximum 6.1.


Solution

  • You can wrap pytest.mark.skipif in a parametrized decorator to do what you want. The following code provides minversion(n) and maxversion(n) decorators that do exactly what you want; you can combine the two (see test_function_2) to set a version range, or you could of course write a new minmaxversion decoration following the same pattern:

    import pytest
    
    program_version = 6
    
    
    def minversion(v):
        return pytest.mark.skipif(
            program_version < v, reason=f"Requires at least version {v}"
        )
    
    
    def maxversion(v):
        return pytest.mark.skipif(
            program_version > v, reason=f"Requires at most version {v}"
        )
    
    
    @maxversion(3)
    def test_function_1():
        assert True
    
    
    @maxversion(5)
    @minversion(3)
    def test_function_2():
        assert True
    

    With program_version = 6, running the above code yields:

    test_markers.py::test_function_1 SKIPPED (Requires at most version 3)  [ 50%]
    test_markers.py::test_function_2 SKIPPED (Requires at most version 5)  [100%]