4

Let's say I have a fixture that requires a live database.

If the live database doesn't exist, I want to skip tests that depend on that fixture.

At the moment, I have to manually mark tests to skip, which feels redundant:

@pytest.fixture
def db_client():
  DB_URI = os.getenv('DB_URI')
  # Set up DB client and yield it

@pytest.mark.skipif(not os.getenv('DB_URI'))
def test_some_feature(db):
  # Use db fixture
  ...
hoefling
  • 59,418
  • 12
  • 147
  • 194
scribu
  • 2,958
  • 4
  • 34
  • 44

1 Answers1

7

Call pytest.skip inside the fixture:

@pytest.fixture
def db():
    db_uri = os.getenv('DB_URI', None)
    if not db_uri:
        pytest.skip('No database available')
    else:
        # Set up DB client and yield it
scribu
  • 2,958
  • 4
  • 34
  • 44
hoefling
  • 59,418
  • 12
  • 147
  • 194