MCPcopy
hub / github.com/pytest-dev/pytest / test_setattr

Function test_setattr

testing/test_monkeypatch.py:26–51  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 3

setattrMethod · 0.95
undoMethod · 0.95
MonkeyPatchClass · 0.90

Tested by

no test coverage detected