0
0
PyTesttesting~20 mins

monkeypatch.setenv in PyTest - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Monkeypatch Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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'))
ANone
B123
CMY_VAR
D'' (empty string)
Attempts:
2 left
💡 Hint
Remember that monkeypatch.setenv temporarily sets environment variables during the test.
assertion
intermediate
2: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?
Aassert os.getenv('API_KEY') is not None
Bassert os.environ['API_KEY'] == 'secret'
Cassert os.getenv('API_KEY') == 'secret'
Dassert monkeypatch.getenv('API_KEY') == 'secret'
Attempts:
2 left
💡 Hint
Use the standard way to get environment variables in Python.
🔧 Debug
advanced
2: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'
AThe get_config function caches the environment variable before the test
BThe environment variable is not set globally, so get_config cannot see it
CThe test is missing monkeypatch.delenv('CONFIG') before setting it
DThe get_config function is called before monkeypatch.setenv runs
Attempts:
2 left
💡 Hint
Think about when environment variables are read and if caching can affect the test.
framework
advanced
2:00remaining
How does monkeypatch.setenv affect environment variables during pytest test execution?
Which statement best describes the behavior of monkeypatch.setenv in pytest?
AIt temporarily sets the environment variable only during the test and restores it after
BIt sets the environment variable only for subprocesses spawned by the test
CIt sets the environment variable only if it does not already exist
DIt permanently changes the environment variable for the whole system
Attempts:
2 left
💡 Hint
Think about test isolation and cleanup.
🧠 Conceptual
expert
2: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?
APass the environment variable as a function argument instead of reading it inside the function
BSet the environment variable globally before running pytest and do not use monkeypatch
CModify os.environ directly without monkeypatch to set 'MODE' before each test
DUse monkeypatch.setenv('MODE', value) inside each test to set the variable, then call the function
Attempts:
2 left
💡 Hint
Consider test isolation and avoiding side effects.