Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to remove the attribute 'attr' from the object using monkeypatch.delattr.
PyTest
def test_remove_attr(monkeypatch): class Dummy: attr = 5 obj = Dummy() monkeypatch.[1](Dummy, 'attr') assert not hasattr(obj, 'attr')
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using setattr instead of delattr
Trying to remove attribute with a non-existent method
Forgetting to pass the object and attribute name
✗ Incorrect
monkeypatch.delattr is used to delete an attribute from an object during testing.
2fill in blank
mediumComplete the code to delete the attribute 'value' from the class Example using monkeypatch.delattr.
PyTest
class Example: value = 10 def test_delete_value(monkeypatch): monkeypatch.[1](Example, 'value') assert not hasattr(Example, 'value')
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using setattr which sets attributes instead of deleting
Using a non-existent method like remove or patch
✗ Incorrect
monkeypatch.delattr removes the specified attribute from the given object or class.
3fill in blank
hardFix the error in the code to correctly delete the attribute 'config' from the instance using monkeypatch.delattr.
PyTest
class Config: config = {'key': 'value'} def test_config(monkeypatch): instance = Config() monkeypatch.[1](Config, 'config') assert not hasattr(instance, 'config')
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Misspelling the method name
Using underscores or extra letters in the method name
✗ Incorrect
The correct method name is monkeypatch.delattr to delete attributes.
4fill in blank
hardFill both blanks to delete the attribute 'setting' from the object and verify it is removed.
PyTest
class Settings: setting = True s = Settings() monkeypatch.[1](Settings, [2]) assert not hasattr(s, 'setting')
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing attribute name without quotes
Using wrong method name
Passing attribute name as variable instead of string
✗ Incorrect
Use monkeypatch.delattr to delete the attribute, passing the attribute name as a string.
5fill in blank
hardFill all three blanks to delete the attribute 'flag' from the class and check it is gone.
PyTest
class Feature: flag = False monkeypatch.[1](Feature, [2]) assert not hasattr(Feature, [3])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using remove instead of delattr
Passing attribute name without quotes
Using different strings for deletion and check
✗ Incorrect
monkeypatch.delattr deletes the attribute; the attribute name must be passed as a string.