Challenge - 5 Problems
Monkeypatch Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of monkeypatching a function
What is the output of the following pytest test when using the monkeypatch fixture?
PyTest
def get_data(): return "original" def test_monkeypatch(monkeypatch): def fake_get_data(): return "patched" monkeypatch.setattr(__name__, "get_data", fake_get_data) result = get_data() print(result)
Attempts:
2 left
💡 Hint
Think about what monkeypatch.setattr does to the function get_data in the current module.
✗ Incorrect
The monkeypatch fixture replaces the get_data function with fake_get_data, so calling get_data() returns 'patched'.
❓ assertion
intermediate2:00remaining
Correct assertion after monkeypatching an environment variable
Which assertion correctly verifies that the environment variable 'API_KEY' is patched to '12345' using monkeypatch?
PyTest
import os def test_env(monkeypatch): monkeypatch.setenv('API_KEY', '12345') # Which assertion below is correct?
Attempts:
2 left
💡 Hint
Remember environment variables are strings and monkeypatch.setenv changes os.environ.
✗ Incorrect
monkeypatch.setenv sets the environment variable as a string in os.environ, so assert os.environ['API_KEY'] == '12345' is correct.
🔧 Debug
advanced2:00remaining
Why does monkeypatch fail to patch a method?
Given the code below, why does monkeypatch.setattr fail to patch the method 'fetch' of class DataFetcher?
PyTest
class DataFetcher: def fetch(self): return "real data" def test_fetch(monkeypatch): def fake_fetch(self): return "fake data" monkeypatch.setattr(DataFetcher, 'fetch', fake_fetch) fetcher = DataFetcher() result = fetcher.fetch() assert result == "fake data"
Attempts:
2 left
💡 Hint
Check the method signature and how monkeypatch works on class methods.
✗ Incorrect
The fake_fetch method correctly accepts self, so monkeypatch.setattr replaces fetch with fake_fetch. The test passes.
🧠 Conceptual
advanced2:00remaining
Scope of monkeypatch fixture in pytest
Which statement best describes the scope of changes made by the monkeypatch fixture in a pytest test?
Attempts:
2 left
💡 Hint
Think about test isolation and cleanup in pytest.
✗ Incorrect
monkeypatch automatically reverts changes after each test function to keep tests isolated.
❓ framework
expert3:00remaining
Using monkeypatch to mock a network call in pytest
You want to test a function that calls requests.get to fetch data from the internet. Which monkeypatch usage correctly mocks requests.get to return a response with status_code 200 and text 'mocked data'?
PyTest
import requests def fetch_url(url): response = requests.get(url) return response.text def test_fetch_url(monkeypatch): # Which option correctly mocks requests.get?
Attempts:
2 left
💡 Hint
requests.get returns a Response object, not a dictionary.
✗ Incorrect
Option A defines a fake_get function returning an object with status_code and text attributes, matching requests.Response interface. This correctly mocks requests.get.