Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to set an environment variable using monkeypatch.
PyTest
def test_env_var(monkeypatch): monkeypatch.[1]('MY_VAR', '123') import os assert os.getenv('MY_VAR') == '123'
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect method names like set_env or set_environment.
Trying to set environment variables without monkeypatch.
✗ Incorrect
The correct method to set an environment variable with monkeypatch is setenv.
2fill in blank
mediumComplete the code to check that the environment variable is changed inside the test.
PyTest
def test_change_env(monkeypatch): monkeypatch.setenv('PATH', '/tmp') import os assert os.getenv('PATH') == [1]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Expecting the original PATH value instead of the patched one.
Using incorrect string quotes.
✗ Incorrect
After monkeypatch.setenv changes PATH to '/tmp', os.getenv('PATH') returns '/tmp'.
3fill in blank
hardFix the error in the code to correctly set the environment variable using monkeypatch.
PyTest
def test_fix_env(monkeypatch): monkeypatch.[1]('DEBUG', 'True') import os assert os.getenv('DEBUG') == 'True'
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect method names that cause AttributeError.
Forgetting to import monkeypatch fixture.
✗ Incorrect
The correct method is monkeypatch.setenv, not set_env_var or others.
4fill in blank
hardFill both blanks to set and then delete an environment variable using monkeypatch.
PyTest
def test_set_and_delete(monkeypatch): monkeypatch.[1]('TEMP_VAR', 'value') monkeypatch.[2]('TEMP_VAR', raising=False) import os assert os.getenv('TEMP_VAR') is None
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using unsetenv or removeenv which are not monkeypatch methods.
Not passing raising=False to delenv to avoid errors if variable missing.
✗ Incorrect
Use monkeypatch.setenv to set and monkeypatch.delenv to delete environment variables.
5fill in blank
hardFill all three blanks to set an environment variable, check its value, and then delete it using monkeypatch.
PyTest
def test_full_env(monkeypatch): monkeypatch.[1]('API_KEY', 'abc123') import os assert os.getenv([2]) == 'abc123' monkeypatch.[3]('API_KEY', raising=False) assert os.getenv('API_KEY') is None
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong case for environment variable name.
Forgetting to delete the variable after test.
✗ Incorrect
Use setenv to set, the exact string 'API_KEY' to check, and delenv to delete the variable.