0

Is there a way I could prevent all unit tests from running if a certain environment variable is not set to a specific value?

For example I want tests to only run if os.getenv(DB_URL) returns sqlite:///:memory:.

I'd like to configure this globally so I don't need to review every single test class / setup function to check this individually.

2
  • Not really because I would like to enforce it to be a check on all tests which is run before a setup method that may delete data eg. from a database. Manually having to confirm that the method is decorated with the checks for each test would be very annoying. Commented May 4, 2022 at 14:27
  • So you want to execute the test runner, and immediately exit? Why not just not execute the test runner? Commented May 4, 2022 at 16:02

1 Answer 1

1

There's many ways to accomplish this, but a fixture is probably the easiest

import os
import pytest

DB_URL = "foo"

@pytest.fixture(autouse=True, scope="session")
def verify_db_url():
    expected_url = "sqlite:///:memory:"
    if os.getenv(DB_URL) != expected_url:
        pytest.exit("Exiting due to incorrect database environment variable.")
    
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.