@@ -50,21 +50,24 @@ class A:
50
50
51
51
class TestSetattrWithImportPath :
52
52
def test_string_expression (self , monkeypatch : MonkeyPatch ) -> None :
53
- monkeypatch .setattr ("os.path.abspath" , lambda x : "hello2" )
54
- assert os .path .abspath ("123" ) == "hello2"
53
+ with monkeypatch .context () as mp :
54
+ mp .setattr ("os.path.abspath" , lambda x : "hello2" )
55
+ assert os .path .abspath ("123" ) == "hello2"
55
56
56
57
def test_string_expression_class (self , monkeypatch : MonkeyPatch ) -> None :
57
- monkeypatch .setattr ("_pytest.config.Config" , 42 )
58
- import _pytest
58
+ with monkeypatch .context () as mp :
59
+ mp .setattr ("_pytest.config.Config" , 42 )
60
+ import _pytest
59
61
60
- assert _pytest .config .Config == 42 # type: ignore
62
+ assert _pytest .config .Config == 42 # type: ignore
61
63
62
64
def test_unicode_string (self , monkeypatch : MonkeyPatch ) -> None :
63
- monkeypatch .setattr ("_pytest.config.Config" , 42 )
64
- import _pytest
65
+ with monkeypatch .context () as mp :
66
+ mp .setattr ("_pytest.config.Config" , 42 )
67
+ import _pytest
65
68
66
- assert _pytest .config .Config == 42 # type: ignore
67
- monkeypatch .delattr ("_pytest.config.Config" )
69
+ assert _pytest .config .Config == 42 # type: ignore
70
+ mp .delattr ("_pytest.config.Config" )
68
71
69
72
def test_wrong_target (self , monkeypatch : MonkeyPatch ) -> None :
70
73
with pytest .raises (TypeError ):
@@ -80,14 +83,16 @@ def test_unknown_attr(self, monkeypatch: MonkeyPatch) -> None:
80
83
81
84
def test_unknown_attr_non_raising (self , monkeypatch : MonkeyPatch ) -> None :
82
85
# https://github.com/pytest-dev/pytest/issues/746
83
- monkeypatch .setattr ("os.path.qweqwe" , 42 , raising = False )
84
- assert os .path .qweqwe == 42 # type: ignore
86
+ with monkeypatch .context () as mp :
87
+ mp .setattr ("os.path.qweqwe" , 42 , raising = False )
88
+ assert os .path .qweqwe == 42 # type: ignore
85
89
86
90
def test_delattr (self , monkeypatch : MonkeyPatch ) -> None :
87
- monkeypatch .delattr ("os.path.abspath" )
88
- assert not hasattr (os .path , "abspath" )
89
- monkeypatch .undo ()
90
- assert os .path .abspath
91
+ with monkeypatch .context () as mp :
92
+ mp .delattr ("os.path.abspath" )
93
+ assert not hasattr (os .path , "abspath" )
94
+ mp .undo ()
95
+ assert os .path .abspath
91
96
92
97
93
98
def test_delattr () -> None :
0 commit comments