Before I start executing the tests in my Python project, I read some environment variables and set some variables with these values read. My tests will run on the desired en
import os
# success.py
def hello_world():
return os.environ["HELLO"]
# fail.py
global_ref = os.environ["HELLO"] # KeyError occurs this line because getting environment variable before monkeypatching
def hello_world():
return global_ref
# test.py
def test_hello_world(monkeypatch):
# Setup
envs = {
'HELLO': 'world'
}
monkeypatch.setattr(os, 'environ', envs)
# Test
result = hello_world()
# Verify
assert(result == 'world')
[Run]
-> [Edit Configuration]
-> [Defaults]
-> [py.tests]
-> [Environment Variables]