Closed
Description
I am having a hard time writing some tests for an application. Basically I have an autouse fixture in conftest.py
that sets up some environment variables that the application needs to initialize some objects:
# conftest.py
@pytest.fixture(autouse=True)
def env_config(monkeypatch):
monkeypatch.setenv('ACCESS_KEY_ID', '12313221323')
monkeypatch.setenv('SECRET_ACCESS_KEY', '12313221323')
monkeypatch.setenv('REGION', '12313221323')
# ...
The problem is that this fixture is only run until each test starts. However, in a specific test file, I am importing the module that contains functions I want to test, outside the tests:
# test_my_func.py
import app
class TestMyFunc:
def test_foo_(self):
# Test something about app.foo
pass
Importing app
crashes because the environment variables are not set yet:
# app.py
from requests_aws4auth import AWS4Auth
awsauth = AWS4Auth(
ACCESS_KEY_ID,
SECRET_ACCESS_KEY,
REGION,
'es'
)
def foo():
pass
I cannot use a blank string as a fallback for os.getenv
since AWS4Auth
fails when using them.
If I move import app
inside the test, everything works perfectly since the autouse fixture has already been executed.
Would appreciate some advice.