Monkey patching env variables for pytest

Viewed 85

I have an app.py file with variables being loaded from my template.yaml file.

KINESIS_VALID = os.environ['KINESIS_VALID']
KINESIS_INVALID = os.environ['KINESIS_INVALID']

I've created a conftest.py file in my tests directory and set a function to monkeypatch test variables:

import pytest


@pytest.fixture(autouse=True)
def env_setup(monkeypatch):
    monkeypatch.setenv('KINESIS_VALID', 'kinesis-valid-test')
    monkeypatch.setenv('KINESIS_INVALID', 'kinesis-invalid-test)

My test file is in tests/unit/test_handler.py and the conftest.py is in the same directory as test_handler.py.

When I run the test, I'm getting KeyyError on KINESIS_VALID from where it's being defined in app.py (KINESIS_VALID = os.environ['KINESIS_VALID'])

The variables are being defined in app.py and I added a conftest.py to the directory app.py is in but the same error occurs. Not sure if I'm missing something in configuration or I'm not defining either the test variables or app.py variables correctly.

0 Answers
Related