Is it possible for pytest to detect "-m 'not my_mark'" in a fixture?

143 views Asked by At

It is possible to detect if a mark has been excluded?

I use pytest to run some tests against an embedded target. For some of the test setups, I can control the supply power via an epdu.

For the setups with an epdu, I want to power down the test equipment when the test is finished.

For the setups without epdu the test is called with -m "not power", but here it is also crucial that the power_on fixture will not try to communicate with the epdu.

@pytest.fixture(scope='session', autouse=True)
def power_on():
    # TODO: just return it called with `-m "not power"`
    power_on_test_equipment()
    yield
    power_off_test_equipment()

@pytest.mark.power_control
def test_something():
    power_something_off()

What I found is request.keywords['power'] will be true if I run pytest with -m power but will not exists if I run without the mark or with -m "not power", which is not really helpful for my scenario.

I can solve the problem using two markes, like `-m "no_power and not power", but it does not seem very elegant.

1

There are 1 answers

2
MrBean Bremen On BEST ANSWER

One possibility is just to check for the command line argument. If you know that you are always passing it as -m "not power", you could do something like this:

@pytest.fixture(scope='session', autouse=True)
def power_on(request):
    power = 'not power' not in request.config.getoption('-m')
    if power:
        power_on_test_equipment()
    yield
    if power:
        power_off_test_equipment()