Created
November 1, 2018 12:48
-
-
Save drorata/71ccf0c8220d4879e0e36400aad69480 to your computer and use it in GitHub Desktop.
Minimal example of mocking environment variables using PyTest
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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