From 3cd19a7e45fac0694e9d91bb01611ff2d85684f5 Mon Sep 17 00:00:00 2001 From: Eric Hunsberger Date: Fri, 24 Jul 2015 01:12:01 -0400 Subject: [PATCH] Use monkeypatch for setting modules in tests Instead of directly setting `sys.modules`. This ensures that they get removed at the end of the test. --- testing/test_runner.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/testing/test_runner.py b/testing/test_runner.py index e359127e2..167ddc57b 100644 --- a/testing/test_runner.py +++ b/testing/test_runner.py @@ -439,7 +439,7 @@ def test_exception_printing_skip(): s = excinfo.exconly(tryshort=True) assert s.startswith("Skipped") -def test_importorskip(): +def test_importorskip(monkeypatch): importorskip = pytest.importorskip def f(): importorskip("asdlkj") @@ -457,7 +457,7 @@ def test_importorskip(): pytest.raises(SyntaxError, "pytest.importorskip('x=y')") mod = py.std.types.ModuleType("hello123") mod.__version__ = "1.3" - sys.modules["hello123"] = mod + monkeypatch.setitem(sys.modules, "hello123", mod) pytest.raises(pytest.skip.Exception, """ pytest.importorskip("hello123", minversion="1.3.1") """) @@ -471,11 +471,11 @@ def test_importorskip_imports_last_module_part(): ospath = pytest.importorskip("os.path") assert os.path == ospath -def test_importorskip_dev_module(): +def test_importorskip_dev_module(monkeypatch): try: mod = py.std.types.ModuleType("mockmodule") mod.__version__ = '0.13.0.dev-43290' - sys.modules['mockmodule'] = mod + monkeypatch.setitem(sys.modules, 'mockmodule', mod) mod2 = pytest.importorskip('mockmodule', minversion='0.12.0') assert mod2 == mod pytest.raises(pytest.skip.Exception, """