Is there any way in pytest to know what markers are selected from the command line?
I have some tests marked as "slow" that need a heavy treatment. I want to process the treatment only if the marker slow is activated.
heavy_var = None
def setup_module(module):
global heavy_var
# Need help here!?
if markers["slow"]:
heavy_var = treatment()
def test_simple():
pass
@pytest.mark.slow():
def test_slow():
assert heavy_var.x == "..."
How can i know if the slow marker is selected or not? When i call pytest with -m not slow
markers["slow"]
will be False otherwise True.
If you need to run some code only if tests marked with slow
were selected, you can do that by filtering the test items in a module-scoped fixture that replaces the setup_module
. Example:
@pytest.fixture(scope='module', autouse=True)
def init_heavy_var(request):
for item in request.session.items:
if item.get_closest_marker('slow') is not None:
# found a test marked with the 'slow' marker, invoke heavy lifting
heavy_var = treatment()
break
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With