scribu
scribu

Reputation: 3078

Skip test from inside a fixture

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
  ...

Upvotes: 4

Views: 453

Answers (1)

hoefling
hoefling

Reputation: 66241

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

Upvotes: 7

Related Questions