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.
This commit is contained in:
parent
d104487282
commit
3cd19a7e45
|
@ -439,7 +439,7 @@ def test_exception_printing_skip():
|
||||||
s = excinfo.exconly(tryshort=True)
|
s = excinfo.exconly(tryshort=True)
|
||||||
assert s.startswith("Skipped")
|
assert s.startswith("Skipped")
|
||||||
|
|
||||||
def test_importorskip():
|
def test_importorskip(monkeypatch):
|
||||||
importorskip = pytest.importorskip
|
importorskip = pytest.importorskip
|
||||||
def f():
|
def f():
|
||||||
importorskip("asdlkj")
|
importorskip("asdlkj")
|
||||||
|
@ -457,7 +457,7 @@ def test_importorskip():
|
||||||
pytest.raises(SyntaxError, "pytest.importorskip('x=y')")
|
pytest.raises(SyntaxError, "pytest.importorskip('x=y')")
|
||||||
mod = py.std.types.ModuleType("hello123")
|
mod = py.std.types.ModuleType("hello123")
|
||||||
mod.__version__ = "1.3"
|
mod.__version__ = "1.3"
|
||||||
sys.modules["hello123"] = mod
|
monkeypatch.setitem(sys.modules, "hello123", mod)
|
||||||
pytest.raises(pytest.skip.Exception, """
|
pytest.raises(pytest.skip.Exception, """
|
||||||
pytest.importorskip("hello123", minversion="1.3.1")
|
pytest.importorskip("hello123", minversion="1.3.1")
|
||||||
""")
|
""")
|
||||||
|
@ -471,11 +471,11 @@ def test_importorskip_imports_last_module_part():
|
||||||
ospath = pytest.importorskip("os.path")
|
ospath = pytest.importorskip("os.path")
|
||||||
assert os.path == ospath
|
assert os.path == ospath
|
||||||
|
|
||||||
def test_importorskip_dev_module():
|
def test_importorskip_dev_module(monkeypatch):
|
||||||
try:
|
try:
|
||||||
mod = py.std.types.ModuleType("mockmodule")
|
mod = py.std.types.ModuleType("mockmodule")
|
||||||
mod.__version__ = '0.13.0.dev-43290'
|
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')
|
mod2 = pytest.importorskip('mockmodule', minversion='0.12.0')
|
||||||
assert mod2 == mod
|
assert mod2 == mod
|
||||||
pytest.raises(pytest.skip.Exception, """
|
pytest.raises(pytest.skip.Exception, """
|
||||||
|
|
Loading…
Reference in New Issue