MCPcopy Create free account
hub / github.com/pytest-dev/pytest / test_setattr

Function test_setattr

testing/test_monkeypatch.py:24–48  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

22
23
24def test_setattr() -> None:
25 class A:
26 x = 1
27
28 monkeypatch = MonkeyPatch()
29 pytest.raises(AttributeError, monkeypatch.setattr, A, "notexists", 2)
30 monkeypatch.setattr(A, "y", 2, raising=False)
31 assert A.y == 2 # type: ignore
32 monkeypatch.undo()
33 assert not hasattr(A, "y")
34
35 monkeypatch = MonkeyPatch()
36 monkeypatch.setattr(A, "x", 2)
37 assert A.x == 2
38 monkeypatch.setattr(A, "x", 3)
39 assert A.x == 3
40 monkeypatch.undo()
41 assert A.x == 1
42
43 A.x = 5
44 monkeypatch.undo() # double-undo makes no modification
45 assert A.x == 5
46
47 with pytest.raises(TypeError):
48 monkeypatch.setattr(A, "y") # type: ignore[call-overload]
49
50
51class TestSetattrWithImportPath:

Callers

nothing calls this directly

Calls 3

setattrMethod · 0.95
undoMethod · 0.95
MonkeyPatchClass · 0.90

Tested by

no test coverage detected