4

I have a py.test function like the follows:

def my_test(self, driver):
    # Do something with the driver

which uses a driver fixture (defined in conftest.py).

Now, I need to check something before running this test. If that check fails, I need to skip this test. Here is what I tried:

@pytest.mark.skipif(not driver.check(),
                    reason="Some check negative.")
def my_test(self, driver):
    # Do something with the driver

But that marker uses a fixture itself (it might be the same fixture, or a different fixture, it does not matter here). Is there a simple way I can use a fixture in a skipif marker?

Alex
  • 41,580
  • 88
  • 260
  • 469
  • Fixtures are injected into test functions, they don't work at import time as in decorators. You might be able to use the fixure function directly by calling it. – Klaus D. Feb 26 '19 at 07:22
  • What do you mean 'by calling it'? I am calling a fixture, no? – Alex Feb 26 '19 at 07:30
  • Try `driver()`. – Klaus D. Feb 26 '19 at 07:55
  • 2
    Even if you manage to call the fixture function directly, this behaviour is deprecated. Instead of using fixture in `skip` decorator, call `pytest.skip` in fixture function. – hoefling Feb 26 '19 at 16:25

1 Answers1

0

Why not skip the test from inside the driver fixture? Then every fixture can decide for itself whether it needs to skip all tests that depend on it.

@pytest.fixture()
def driver():
    if not check_driver():
        pytest.skip("Driver could not be loaded")
    return get_driver()

See also Skip test from inside a fixture

winni2k
  • 1,460
  • 16
  • 19