0
0
PyTesttesting~20 mins

monkeypatch.setattr 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 monkeypatch.setattr on a function attribute
What will be the output of this pytest test function when monkeypatch.setattr is used to replace a function?
PyTest
def greet():
    return "Hello"

def test_greet(monkeypatch):
    def fake_greet():
        return "Hi"
    monkeypatch.setattr(__name__, "greet", fake_greet)
    result = greet()
    print(result)
AHi
BHello
CAttributeError
DTypeError
Attempts:
2 left
💡 Hint
Think about what monkeypatch.setattr does to the function greet inside the test.
assertion
intermediate
2:00remaining
Correct assertion after monkeypatch.setattr on an object attribute
Given this code, which assertion correctly verifies the monkeypatched attribute value?
PyTest
class User:
    active = True

def test_user_active(monkeypatch):
    monkeypatch.setattr(User, "active", False)
    user = User()
    # Which assertion is correct here?
Aassert user.active is False
Bassert User.active is True
Cassert user.active is True
Dassert User.active is None
Attempts:
2 left
💡 Hint
monkeypatch.setattr changes the attribute value on the class User.
🔧 Debug
advanced
2:00remaining
Identify the error in monkeypatch.setattr usage
What error will this test raise when run with pytest?
PyTest
def test_patch(monkeypatch):
    import os
    monkeypatch.setattr(os.path, "exists", lambda x: True)
    print(os.path.exists("anyfile.txt"))
ANameError: name 'os' is not defined
BPrints True
CTypeError: setattr() arg 1 must be module, class, or instance
DAttributeError: module 'os' has no attribute 'path'
Attempts:
2 left
💡 Hint
Check the first argument to monkeypatch.setattr and what it expects.
🧠 Conceptual
advanced
2:00remaining
Understanding monkeypatch.setattr scope and cleanup
Which statement about monkeypatch.setattr is TRUE regarding its effect and cleanup?
AChanges made by monkeypatch.setattr persist after the test finishes
Bmonkeypatch.setattr permanently deletes the original attribute
Cmonkeypatch.setattr only affects the patched attribute during the test and reverts after
Dmonkeypatch.setattr requires manual cleanup to restore original attributes
Attempts:
2 left
💡 Hint
Think about test isolation and why monkeypatch is useful.
framework
expert
3:00remaining
Best practice for monkeypatching a method in a class instance
You want to monkeypatch the method 'calculate' of an instance 'obj' of class 'Calculator' during a pytest test. Which code snippet correctly patches the method only for this test?
PyTest
class Calculator:
    def calculate(self, x):
        return x * 2

def test_calculate(monkeypatch):
    obj = Calculator()
    # Which monkeypatch code is correct here?
Amonkeypatch.setattr(obj.calculate, lambda x: 42)
Bmonkeypatch.setattr(Calculator, "calculate", lambda self, x: 42)
Cmonkeypatch.setattr("Calculator", "calculate", lambda self, x: 42)
Dmonkeypatch.setattr(obj, "calculate", lambda x: 42)
Attempts:
2 left
💡 Hint
Remember monkeypatch.setattr needs the object and attribute name as separate arguments.