I have a global object in my main file
# reporter.py
from os import environ
from influxdb import InfluxDBClient
influxdb_client = InfluxDBClient(host=environ['INFLUXCLOUD_HOST'],
username=environ['INFLUXCLOUD_USERNAME'],
password=environ['INFLUXCLOUD_PASSWORD'],
ssl=True,
timeout=4*60)
def foo():
pass
I'm using pytest and I want to set faux values to these environment variables. I have the following in my conftest.py:
# conftest.py
import pytest
@pytest.fixture(scope='session', autouse=True)
def setup_env(monkeypatch):
monkeypatch.setenv('INFLUXCLOUD_HOST', 'host')
monkeypatch.setenv('INFLUXCLOUD_USERNAME', 'username')
monkeypatch.setenv('INFLUXCLOUD_PASSWORD', 'password')
However, when I import reporter
in my test file, I get a KeyError
that INFLUXCLOUD_HOST is missing in the env.
Why does not pytest execute the setup_env
and monkeypatch my environment? Is there a way to do so?