We use monkeypatch.setattr to temporarily change how a part of the code works during a test. This helps us test without changing the real code.
monkeypatch.setattr in PyTest
monkeypatch.setattr(target, name, value, raising=True)target can be a module, class, or object where the attribute lives.
name is the attribute name as a string.
value is the new value or function you want to set temporarily.
monkeypatch.setattr(module, "function_name", fake_function)monkeypatch.setattr(obj, "attribute", 42)
monkeypatch.setattr("module.ClassName.attribute", "new_value")
This test replaces the multiply method of Calculator with a fake function that always returns 42. It checks that the replacement works by asserting the result is 42.
import pytest class Calculator: def multiply(self, a, b): return a * b def fake_multiply(a, b): return 42 def test_multiply(monkeypatch): calc = Calculator() # Replace multiply method with fake_multiply monkeypatch.setattr(calc, "multiply", fake_multiply) result = calc.multiply(3, 5) assert result == 42 print(f"Result after monkeypatch: {result}")
The change made by monkeypatch.setattr lasts only during the test. After the test ends, the original code is restored.
Use monkeypatch as a test function argument to get access to it.
Setting raising=False prevents errors if the attribute does not exist.
monkeypatch.setattr lets you temporarily change code parts during tests.
It helps test code in isolation by replacing functions or variables.
Changes are undone automatically after the test finishes.
Practice
monkeypatch.setattr in pytest?Solution
Step 1: Understand monkeypatch.setattr usage
It is used to temporarily replace or modify attributes or functions during a test to isolate behavior.Step 2: Differentiate from other pytest features
It does not permanently change code or skip tests; it only affects the test scope temporarily.Final Answer:
To temporarily replace an attribute or function during a test -> Option AQuick Check:
Temporary replacement = A [OK]
- Confusing temporary patching with permanent code changes
- Thinking it skips tests
- Assuming it runs tests in parallel
get_data in module my_module using monkeypatch in pytest?Solution
Step 1: Identify correct monkeypatch.setattr signature
The correct usage is monkeypatch.setattr(target_object, attribute_name, new_value).Step 2: Match parameters to patch get_data function
We pass the module objectmy_module, the attribute name'get_data', and the replacement functionlambda: 42.Final Answer:
monkeypatch.setattr(my_module, 'get_data', lambda: 42) -> Option CQuick Check:
Object, attribute, new value = A [OK]
- Passing attribute as a string path instead of object and attribute
- Swapping parameters order
- Passing value instead of function for patching
import my_module
def test_func(monkeypatch):
def fake_func():
return 10
monkeypatch.setattr(my_module, 'get_value', fake_func)
result = my_module.get_value()
print(result)Solution
Step 1: Understand monkeypatch effect on get_value
The originalmy_module.get_valueis replaced temporarily byfake_funcwhich returns 10.Step 2: Evaluate the call and print output
Callingmy_module.get_value()inside the test callsfake_func(), so it prints 10.Final Answer:
10 -> Option DQuick Check:
Patched function returns 10 [OK]
- Expecting original function output instead of patched
- Thinking patch is permanent outside test
- Assuming print outputs None
def test_example(monkeypatch):
monkeypatch.setattr('os.path.exists', lambda x: True)
assert os.path.exists('anyfile.txt') == TrueSolution
Step 1: Check monkeypatch.setattr usage
The first argument must be the module or object, not a string path.Step 2: Identify correct usage
It should bemonkeypatch.setattr(os.path, 'exists', lambda x: True)to patch the function properly.Final Answer:
The first argument to setattr should be the module object, not a string -> Option BQuick Check:
Object, not string path = B [OK]
- Passing string path instead of module object
- Incorrect lambda parameters
- Assuming some functions cannot be patched
fetch_data() that calls requests.get. How can you use monkeypatch.setattr to replace requests.get with a fake function that returns a mock response with status_code=200 and text='OK'?Solution
Step 1: Create a fake function returning a mock response
Define a function that returns an object withstatus_code=200andtext='OK'.Step 2: Patch requests.get with monkeypatch.setattr
Usemonkeypatch.setattr(requests, 'get', fake_func)to replace the real HTTP call during the test.Final Answer:
Define a fake function returning a mock object, then patch with monkeypatch.setattr(requests, 'get', fake_func) -> Option AQuick Check:
Patch requests.get with fake function = D [OK]
- Passing string path instead of module object
- Patching with wrong types like strings
- Replacing the wrong function instead of requests.get
Ready to prove it?
Pass the test and this pattern moves unknown
