Challenge - 5 Problems
Monkeypatch Delattr Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output after monkeypatch.delattr removes an attribute?
Consider the following code using pytest's monkeypatch.delattr. What will be the output of the test function?
PyTest
class MyClass: def method(self): return "original" def test_method(monkeypatch): obj = MyClass() monkeypatch.delattr(obj, "method") try: result = obj.method() except AttributeError: result = "AttributeError" print(result)
Attempts:
2 left
💡 Hint
Think about what happens when you remove an attribute and then try to access it.
✗ Incorrect
The monkeypatch.delattr removes the 'method' attribute from the instance 'obj'. When calling obj.method(), Python cannot find the method and raises AttributeError.
❓ assertion
intermediate1:30remaining
Which assertion correctly verifies that monkeypatch.delattr removed an attribute?
Given an object 'obj' with attribute 'attr', and after monkeypatch.delattr(obj, 'attr'), which assertion correctly checks that 'attr' is removed?
Attempts:
2 left
💡 Hint
Use hasattr to check if an attribute exists.
✗ Incorrect
After monkeypatch.delattr removes 'attr', hasattr(obj, 'attr') returns False. So asserting it is False confirms removal.
🔧 Debug
advanced2:00remaining
Why does monkeypatch.delattr fail to remove a class attribute when called on an instance?
Given this code:
class MyClass:
attr = 5
obj = MyClass()
monkeypatch.delattr(obj, 'attr')
Why does obj.attr still return 5 after delattr?
Attempts:
2 left
💡 Hint
Think about where the attribute is stored: instance or class?
✗ Incorrect
Attributes defined on the class are not stored on the instance. monkeypatch.delattr called on the instance removes only instance attributes, so class attributes remain accessible.
❓ framework
advanced1:30remaining
How does monkeypatch.delattr behave with the 'raising' parameter set to False?
What happens when you call monkeypatch.delattr(obj, 'missing_attr', raising=False) if 'missing_attr' does not exist on obj?
Attempts:
2 left
💡 Hint
Check the effect of the 'raising' flag in monkeypatch.delattr.
✗ Incorrect
With raising=False, monkeypatch.delattr does not raise an error if the attribute does not exist; it silently skips removal.
🧠 Conceptual
expert2:30remaining
What is the main advantage of using monkeypatch.delattr in tests?
Why would a tester prefer monkeypatch.delattr over manually deleting attributes in test setup?
Attempts:
2 left
💡 Hint
Think about test isolation and cleanup.
✗ Incorrect
monkeypatch.delattr removes attributes temporarily during a test and restores them after, ensuring tests do not affect each other.