testing/test_monkeypatch: fix some patches leaking into pytest code
The tests patch `os.path.abspath` which can break some pytest internal code since the patching is not undone immediately.
This commit is contained in:
parent
0c98f19231
commit
ed83efaf4b
|
@ -50,21 +50,24 @@ def test_setattr() -> None:
|
||||||
|
|
||||||
class TestSetattrWithImportPath:
|
class TestSetattrWithImportPath:
|
||||||
def test_string_expression(self, monkeypatch: MonkeyPatch) -> None:
|
def test_string_expression(self, monkeypatch: MonkeyPatch) -> None:
|
||||||
monkeypatch.setattr("os.path.abspath", lambda x: "hello2")
|
with monkeypatch.context() as mp:
|
||||||
assert os.path.abspath("123") == "hello2"
|
mp.setattr("os.path.abspath", lambda x: "hello2")
|
||||||
|
assert os.path.abspath("123") == "hello2"
|
||||||
|
|
||||||
def test_string_expression_class(self, monkeypatch: MonkeyPatch) -> None:
|
def test_string_expression_class(self, monkeypatch: MonkeyPatch) -> None:
|
||||||
monkeypatch.setattr("_pytest.config.Config", 42)
|
with monkeypatch.context() as mp:
|
||||||
import _pytest
|
mp.setattr("_pytest.config.Config", 42)
|
||||||
|
import _pytest
|
||||||
|
|
||||||
assert _pytest.config.Config == 42 # type: ignore
|
assert _pytest.config.Config == 42 # type: ignore
|
||||||
|
|
||||||
def test_unicode_string(self, monkeypatch: MonkeyPatch) -> None:
|
def test_unicode_string(self, monkeypatch: MonkeyPatch) -> None:
|
||||||
monkeypatch.setattr("_pytest.config.Config", 42)
|
with monkeypatch.context() as mp:
|
||||||
import _pytest
|
mp.setattr("_pytest.config.Config", 42)
|
||||||
|
import _pytest
|
||||||
|
|
||||||
assert _pytest.config.Config == 42 # type: ignore
|
assert _pytest.config.Config == 42 # type: ignore
|
||||||
monkeypatch.delattr("_pytest.config.Config")
|
mp.delattr("_pytest.config.Config")
|
||||||
|
|
||||||
def test_wrong_target(self, monkeypatch: MonkeyPatch) -> None:
|
def test_wrong_target(self, monkeypatch: MonkeyPatch) -> None:
|
||||||
with pytest.raises(TypeError):
|
with pytest.raises(TypeError):
|
||||||
|
@ -80,14 +83,16 @@ class TestSetattrWithImportPath:
|
||||||
|
|
||||||
def test_unknown_attr_non_raising(self, monkeypatch: MonkeyPatch) -> None:
|
def test_unknown_attr_non_raising(self, monkeypatch: MonkeyPatch) -> None:
|
||||||
# https://github.com/pytest-dev/pytest/issues/746
|
# https://github.com/pytest-dev/pytest/issues/746
|
||||||
monkeypatch.setattr("os.path.qweqwe", 42, raising=False)
|
with monkeypatch.context() as mp:
|
||||||
assert os.path.qweqwe == 42 # type: ignore
|
mp.setattr("os.path.qweqwe", 42, raising=False)
|
||||||
|
assert os.path.qweqwe == 42 # type: ignore
|
||||||
|
|
||||||
def test_delattr(self, monkeypatch: MonkeyPatch) -> None:
|
def test_delattr(self, monkeypatch: MonkeyPatch) -> None:
|
||||||
monkeypatch.delattr("os.path.abspath")
|
with monkeypatch.context() as mp:
|
||||||
assert not hasattr(os.path, "abspath")
|
mp.delattr("os.path.abspath")
|
||||||
monkeypatch.undo()
|
assert not hasattr(os.path, "abspath")
|
||||||
assert os.path.abspath
|
mp.undo()
|
||||||
|
assert os.path.abspath
|
||||||
|
|
||||||
|
|
||||||
def test_delattr() -> None:
|
def test_delattr() -> None:
|
||||||
|
|
Loading…
Reference in New Issue