1

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.

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
nikolaj
  • 180
  • 1
  • 10

1 Answers1

2

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()
MrBean Bremen
  • 14,916
  • 3
  • 26
  • 46
  • 2
    If additional `-m` options are used, this won't work. Be sure to check if `power = not 'not power' in request.config.getoption('-m')` – BHass Jul 02 '21 at 18:17
  • True, that's what I meant with the last note. I will adapt the answer, you are right. – MrBean Bremen Jul 02 '21 at 18:21