Merge cleaning of sys.modules after pytester.inline_run()

Merged in schettino72/pytest/pytester-inline-run-clean-sys-modules
(pull request #278).
This commit is contained in:
Floris Bruynooghe 2015-04-21 11:00:32 +01:00
commit 240cd1f28d
5 changed files with 27 additions and 3 deletions

View File

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

View File

@ -14,6 +14,9 @@
of pkg_under_test whereas before they would always pick of pkg_under_test whereas before they would always pick
up the local version. Thanks Holger Krekel. up the local version. Thanks Holger Krekel.
- pytester: add method ``TmpTestdir.delete_loaded_modules()``, and call it
from ``inline_run()`` to allow temporary modules to be reloaded.
Thanks Eduardo Schettino.
2.7.1.dev (compared to 2.7.0) 2.7.1.dev (compared to 2.7.0)
----------------------------- -----------------------------

View File

@ -266,14 +266,22 @@ class TmpTestdir:
Some methods modify the global interpreter state and this Some methods modify the global interpreter state and this
tries to clean this up. It does not remove the temporary 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. has finished.
""" """
sys.path[:] = self._savesyspath sys.path[:] = self._savesyspath
if hasattr(self, '_olddir'): if hasattr(self, '_olddir'):
self._olddir.chdir() 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()): for name, mod in list(sys.modules.items()):
if mod: if mod:
fn = getattr(mod, '__file__', None) fn = getattr(mod, '__file__', None)
@ -537,6 +545,7 @@ class TmpTestdir:
assert len(rec) == 1 assert len(rec) == 1
reprec = rec[0] reprec = rec[0]
reprec.ret = ret reprec.ret = ret
self.delete_loaded_modules()
return reprec return reprec
def parseconfig(self, *args): def parseconfig(self, *args):

View File

@ -1,2 +1,2 @@
sphinx==1.2.3 sphinx==1.2.3
hg+ssh://hg@bitbucket.org/RonnyPfannschmidt/regendoc#egg=regendoc hg+ssh://hg@bitbucket.org/pytest-dev/regendoc#egg=regendoc

View File

@ -2,6 +2,8 @@ import pytest
import os import os
from _pytest.pytester import HookRecorder from _pytest.pytester import HookRecorder
from _pytest.core import PluginManager from _pytest.core import PluginManager
from _pytest.main import EXIT_OK, EXIT_TESTSFAILED
def test_make_hook_recorder(testdir): def test_make_hook_recorder(testdir):
item = testdir.getitem("def test_func(): pass") item = testdir.getitem("def test_func(): pass")
@ -121,3 +123,12 @@ def test_inprocess_plugins(testdir):
testdir.inprocess_run([], [plugin]) testdir.inprocess_run([], [plugin])
assert plugin.configured 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