0
0
PyTesttesting~20 mins

monkeypatch fixture 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
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)
Aoriginal
BNone
Cpatched
DRaises AttributeError
Attempts:
2 left
💡 Hint
Think about what monkeypatch.setattr does to the function get_data in the current module.
assertion
intermediate
2: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?
Aassert os.environ.get('API_KEY') is None
Bassert os.environ['API_KEY'] == '12345'
Cassert monkeypatch.getenv('API_KEY') == '12345'
Dassert os.getenv('API_KEY') == 12345
Attempts:
2 left
💡 Hint
Remember environment variables are strings and monkeypatch.setenv changes os.environ.
🔧 Debug
advanced
2: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"
AThe test will pass; monkeypatch correctly patches the method
Bmonkeypatch.setattr cannot patch class methods
CThe fake_fetch method is missing the self parameter
DThe fake_fetch method is not bound to the instance, causing a TypeError
Attempts:
2 left
💡 Hint
Check the method signature and how monkeypatch works on class methods.
🧠 Conceptual
advanced
2:00remaining
Scope of monkeypatch fixture in pytest
Which statement best describes the scope of changes made by the monkeypatch fixture in a pytest test?
AChanges made by monkeypatch affect only the imported modules, not local functions
BChanges made by monkeypatch persist across all tests in the module
CChanges made by monkeypatch require manual cleanup to revert
DChanges made by monkeypatch are reverted after the test function finishes
Attempts:
2 left
💡 Hint
Think about test isolation and cleanup in pytest.
framework
expert
3: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?
A
def fake_get(url):
    class Response:
        status_code = 200
        text = 'mocked data'
    return Response()
monkeypatch.setattr(requests, 'get', fake_get)
Bmonkeypatch.setattr(requests.get, 'return_value', {'status_code': 200, 'text': 'mocked data'})
Cmonkeypatch.setattr('requests.get', lambda url: {'status_code': 200, 'text': 'mocked data'})
D
def fake_get():
    return {'status_code': 200, 'text': 'mocked data'}
monkeypatch.setattr(requests, 'get', fake_get)
Attempts:
2 left
💡 Hint
requests.get returns a Response object, not a dictionary.