Challenge - 5 Problems
Monkeypatch Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output after monkeypatch.setenv changes the environment variable?
Consider this pytest test function that uses
monkeypatch.setenv to change an environment variable. What will be printed when the test runs?PyTest
import os def test_env(monkeypatch): monkeypatch.setenv('MY_VAR', '123') print(os.getenv('MY_VAR'))
Attempts:
2 left
💡 Hint
Remember that monkeypatch.setenv temporarily sets environment variables during the test.
✗ Incorrect
The monkeypatch.setenv function sets the environment variable 'MY_VAR' to '123' for the duration of the test. So os.getenv('MY_VAR') returns '123'.
❓ assertion
intermediate2:00remaining
Which assertion correctly verifies the environment variable set by monkeypatch.setenv?
You want to check that the environment variable 'API_KEY' is set to 'secret' inside a pytest test using monkeypatch. Which assertion is correct?
Attempts:
2 left
💡 Hint
Use the standard way to get environment variables in Python.
✗ Incorrect
The correct way is to use os.getenv('API_KEY') and compare it to 'secret'. Option C can raise KeyError if 'API_KEY' is missing. Option C is invalid because monkeypatch does not have a getenv method. Option C only checks presence, not value.
🔧 Debug
advanced2:00remaining
Why does this test fail to see the environment variable set by monkeypatch.setenv?
This pytest test tries to set 'CONFIG' environment variable but fails to see it inside the tested function. Why?
import os
config_cache = os.getenv('CONFIG')
def get_config():
return config_cache
def test_config(monkeypatch):
monkeypatch.setenv('CONFIG', 'value')
assert get_config() == 'value'Attempts:
2 left
💡 Hint
Think about when environment variables are read and if caching can affect the test.
✗ Incorrect
If get_config reads and caches the environment variable before the test runs, monkeypatch.setenv will not affect the cached value. This causes the test to fail.
❓ framework
advanced2:00remaining
How does monkeypatch.setenv affect environment variables during pytest test execution?
Which statement best describes the behavior of monkeypatch.setenv in pytest?
Attempts:
2 left
💡 Hint
Think about test isolation and cleanup.
✗ Incorrect
monkeypatch.setenv temporarily sets environment variables during the test and restores the original environment after the test finishes, ensuring no side effects.
🧠 Conceptual
expert2:00remaining
What is the correct way to test code that reads environment variables using monkeypatch.setenv and pytest?
You have a function that reads an environment variable 'MODE'. You want to test it with different values using pytest and monkeypatch. Which approach is best?
Attempts:
2 left
💡 Hint
Consider test isolation and avoiding side effects.
✗ Incorrect
Using monkeypatch.setenv inside each test ensures environment variables are set temporarily and isolated per test, avoiding side effects and making tests reliable.