0
0
PyTesttesting~10 mins

monkeypatch.delattr in PyTest - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
Aremoveattr
Bsetattr
Cdelattr
Dpatchattr
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
2fill in blank
medium

Complete 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'
Adelattr
Bsetattr
Cremove
Dpatch
Attempts:
3 left
💡 Hint
Common Mistakes
Using setattr which sets attributes instead of deleting
Using a non-existent method like remove or patch
3fill in blank
hard

Fix 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'
Aremoveattr
Bdelattrt
Cdel_attr
Ddelattr
Attempts:
3 left
💡 Hint
Common Mistakes
Misspelling the method name
Using underscores or extra letters in the method name
4fill in blank
hard

Fill 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'
Adelattr
Bsetting
C'setting'
Dremove
Attempts:
3 left
💡 Hint
Common Mistakes
Passing attribute name without quotes
Using wrong method name
Passing attribute name as variable instead of string
5fill in blank
hard

Fill 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'
Adelattr
B'flag'
Dremove
Attempts:
3 left
💡 Hint
Common Mistakes
Using remove instead of delattr
Passing attribute name without quotes
Using different strings for deletion and check