hackebrot/pytest-tricks

Conditional on mark

adrianschneider94 opened this issue · 0 comments

I had a little problem, I wanted to make a fixture which depends on the mark. I thought, I'd share the solution here, if somebody needs it.

In my example I have to test the interface to different versions (2017, 2018) of an external software, some tests apply to both versions, some just for one. So I mark all test with v2017, v2018 or both. The last step is to get an int (2017 or 2018) from the mark, the user runs pytest with. This is done with the following code.

import pytest
import re

@pytest.fixture
def version(request):
    markexpr = request.config.option.markexpr  # Here is the mark expression
    match = re.match("v(\d\d\d\d)", markexpr)  # Check against pattern
    if not match:
        pytest.exit("No valid version mark.")
    else:
        return int(match.groups()[0])

Best wishes!