Skip to content

Instantly share code, notes, and snippets.

@shaypal5
Created October 6, 2020 17:22
Show Gist options
  • Save shaypal5/3e8250f20a60be9fe66f0c6f30b9deee to your computer and use it in GitHub Desktop.
Save shaypal5/3e8250f20a60be9fe66f0c6f30b9deee to your computer and use it in GitHub Desktop.
Temp environment variables for pytest
import os
import pytest
try:
from.temp_env_var import TEMP_ENV_VARS, ENV_VARS_TO_SUSPEND
except ImportError:
TEMP_ENV_VARS = {}
ENV_VARS_TO_SUSPEND = []
@pytest.fixture(scope="session", autouse=True)
def tests_setup_and_teardown():
# Will be executed before the first test
old_environ = dict(os.environ)
os.environ.update(TEMP_ENV_VARS)
for env_var in ENV_VARS_TO_SUSPEND:
os.environ.pop(env_var, default=None)
yield
# Will be executed after the last test
os.environ.clear()
os.environ.update(old_environ)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment