Pytest test working with different versions of program #10175
-
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:
minversion3 marks the tests to be run provided that program has at least version 3.
so that this test is only working for programs with version minimum 3.2 and maximum 6.1. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Hi, That's possible, but you will need to handle your marker yourself... easiest done by using an autouse fixture which inspects the mark and skips accordingly: @pytest.fixture(autouse=True)
def check_app_version_mark(request):
if m := request.get_closest_marker("minmaxversion"):
min_ver, max_ver = m.args
if not (min_ver <= app.version <= max_ver):
pytest.skip(reason=f"Outside valid version range: {app.version}") Then you use it like: @pytest.mark.minmaxversion(3.2, 6.1)
def test_function():
... |
Beta Was this translation helpful? Give feedback.
-
A perhaps slightly simpler way to what @nicoddemus has shown would be to write a function returning the appropriate def minmaxversion(min_str, max_str):
minver = tuple(map(int, min_str.split(".")))
maxver = tuple(map(int, max_str.split(".")))
return pytest.mark.skipif(
not minver <= myprogram.__versioninfo__ <= maxver,
reason=f"versions {minver} to {maxver} are required"
)
@minmaxversion("3.1", "6.1")
def test_function():
... (your |
Beta Was this translation helpful? Give feedback.
A perhaps slightly simpler way to what @nicoddemus has shown would be to write a function returning the appropriate
skipif
mark, instead of hardcoding one to a variable:(your
3.2
and6.1
would be floats, which are a weird way to represent version numbers - also, this is completely untested)