pytester: add method ``TmpTestdir.delete_loaded_modules()``

, and call it from ``inline_run()`` to allow temporary modules to be reloaded.

--HG--
branch : pytester-inline-run-clean-sys-modules
This commit is contained in:
Eduardo Schettino 2015-04-21 10:18:04 +08:00
parent c792ab8aed
commit bc0ecd1d06
4 changed files with 25 additions and 2 deletions

View File

@ -48,3 +48,4 @@ Nicolas Delaby
Tom Viner
Dave Hunt
Charles Cloud
schettino72

View File

@ -1,6 +1,8 @@
2.8.0.dev (compared to 2.7.X)
-----------------------------
- pytester: add method ``TmpTestdir.delete_loaded_modules()``, and call it
from ``inline_run()`` to allow temporary modules to be reloaded.
2.7.1.dev (compared to 2.7.0)
-----------------------------

View File

@ -266,7 +266,7 @@ class TmpTestdir:
Some methods modify the global interpreter state and this
tries to clean this up. It does not remove the temporary
directlry however so it can be looked at after the test run
directory however so it can be looked at after the test run
has finished.
"""
@ -274,7 +274,15 @@ class TmpTestdir:
sys.path.remove(p)
if hasattr(self, '_olddir'):
self._olddir.chdir()
# delete modules that have been loaded from tmpdir
self.delete_loaded_modules()
def delete_loaded_modules(self):
"""Delete modules that have been loaded from tmpdir.
This allows the interpreter to catch module changes in case
the module is re-imported.
"""
for name, mod in list(sys.modules.items()):
if mod:
fn = getattr(mod, '__file__', None)
@ -539,6 +547,7 @@ class TmpTestdir:
assert len(rec) == 1
reprec = rec[0]
reprec.ret = ret
self.delete_loaded_modules()
return reprec
def parseconfig(self, *args):

View File

@ -2,6 +2,8 @@ import pytest
import os
from _pytest.pytester import HookRecorder
from _pytest.core import PluginManager
from _pytest.main import EXIT_OK, EXIT_TESTSFAILED
def test_make_hook_recorder(testdir):
item = testdir.getitem("def test_func(): pass")
@ -121,3 +123,12 @@ def test_inprocess_plugins(testdir):
testdir.inprocess_run([], [plugin])
assert plugin.configured
def test_inline_run_clean_modules(testdir):
test_mod = testdir.makepyfile("def test_foo(): assert True")
result = testdir.inline_run(str(test_mod))
assert result.ret == EXIT_OK
# rewrite module, now test should fail if module was re-imported
test_mod.write("def test_foo(): assert False")
result2 = testdir.inline_run(str(test_mod))
assert result2.ret == EXIT_TESTSFAILED