Skip to content

Instantly share code, notes, and snippets.

@drorata
Created November 1, 2018 12:48
Show Gist options
  • Select an option

  • Save drorata/71ccf0c8220d4879e0e36400aad69480 to your computer and use it in GitHub Desktop.

Select an option

Save drorata/71ccf0c8220d4879e0e36400aad69480 to your computer and use it in GitHub Desktop.
Minimal example of mocking environment variables using PyTest
def test_example(monkeypatch):
# Print the value of $FOO. If not defined will be None
print("FOO = %s" % os.getenv('FOO'))
monkeypatch.setenv('FOO', '3.14')
print("FOO = %s" % os.getenv('FOO')) # Expected result: FOO = 3.14
with monkeypatch.context() as m:
m.setenv('FOO', '2.71')
print("FOO = %s" % os.getenv('FOO')) # Expected result: FOO = 2.71
print("FOO = %s" % os.getenv('FOO')) # Expected result: FOO = 3.14
assert 1 == 2 # So you could see the captured output above ;)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment