0
0
PyTesttesting~5 mins

monkeypatch.setenv in PyTest - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does monkeypatch.setenv do in pytest?

monkeypatch.setenv temporarily sets or changes an environment variable during a test. It helps simulate different environment settings without affecting the real system environment.

Click to reveal answer
beginner
How do you use monkeypatch.setenv to set an environment variable named API_KEY to 12345?
<pre>import os

def test_api_key(monkeypatch):
    monkeypatch.setenv('API_KEY', '12345')
    assert os.getenv('API_KEY') == '12345'</pre>
Click to reveal answer
intermediate
Why is it better to use monkeypatch.setenv instead of directly modifying os.environ in tests?

Using monkeypatch.setenv ensures the environment variable changes are temporary and automatically reverted after the test. Directly modifying os.environ can cause side effects on other tests.

Click to reveal answer
beginner
What happens to environment variables set by monkeypatch.setenv after the test finishes?

They are automatically restored to their original values or removed if they did not exist before. This keeps tests isolated and prevents side effects.

Click to reveal answer
intermediate
Can monkeypatch.setenv be used to unset an environment variable? How?

Yes. You can unset an environment variable by setting its value to None with monkeypatch.setenv('VAR_NAME', None). This removes the variable for the test duration.

Click to reveal answer
What is the main purpose of monkeypatch.setenv in pytest?
ACreate new files for testing
BPermanently change environment variables on the system
CMock HTTP requests
DTemporarily change environment variables during a test
After a test using monkeypatch.setenv finishes, what happens to the environment variables?
AThey are restored to their original state
BThey remain changed permanently
CThey are deleted from the system
DThey cause the test to fail
Which of the following is a correct way to set an environment variable DEBUG to true using monkeypatch?
Amonkeypatch.setenv('DEBUG', 'true')
Bmonkeypatch.setenv(DEBUG=true)
Cos.environ['DEBUG'] = true
Dmonkeypatch.setenv('DEBUG')
Why should you prefer monkeypatch.setenv over modifying os.environ directly in tests?
AIt is faster
BIt requires less code
CIt automatically cleans up changes after tests
DIt works only on Windows
How do you remove an environment variable PATH temporarily in a pytest test?
Amonkeypatch.delenv('PATH')
Bmonkeypatch.setenv('PATH', None)
Cos.environ.pop('PATH')
Dmonkeypatch.setenv('PATH', '')
Explain how monkeypatch.setenv helps keep tests isolated and why this is important.
Think about what happens if environment variables stay changed after a test.
You got /4 concepts.
    Describe a simple pytest test example using monkeypatch.setenv to simulate an environment variable and verify it.
    Show how to set and check an environment variable inside a test.
    You got /3 concepts.