Complete the code to patch the function using monkeypatch.
def test_example(monkeypatch): def fake_return(): return 42 monkeypatch.[1]('module.function', fake_return) assert module.function() == 42
The monkeypatch.setattr method replaces an attribute or function for the test.
Complete the code to patch an environment variable using monkeypatch.
def test_env(monkeypatch): monkeypatch.[1]('ENV_VAR', 'test_value') assert os.getenv('ENV_VAR') == 'test_value'
monkeypatch.setenv sets an environment variable temporarily for the test.
Fix the error in patching a dictionary key using monkeypatch.
def test_dict(monkeypatch): config = {'key': 'original'} monkeypatch.[1](config, 'key', 'patched') assert config['key'] == 'patched'
monkeypatch.setitem patches dictionary keys temporarily.
Fill both blanks to patch a function and check its call count.
def test_call_count(monkeypatch): calls = {'count': 0} def fake_func(): calls['count'] += 1 monkeypatch.[1]('module.func', fake_func) module.func() assert calls['count'] [2] 1
Use setattr to patch the function and == to assert exact call count.
Fill all three blanks to patch environment, attribute, and dictionary key correctly.
def test_all(monkeypatch): monkeypatch.[1]('ENV', 'value') class Obj: attr = 5 obj = Obj() monkeypatch.[2](obj, 'attr', 10) data = {'key': 'old'} monkeypatch.[3](data, 'key', 'new') assert os.getenv('ENV') == 'value' assert obj.attr == 10 assert data['key'] == 'new'
Use setenv for environment variables, setattr for object attributes, and setitem for dictionary keys.
