Merge pull request #1016 from hpk42/importappend3

revert importing test module behaviour to prepend by default
This commit is contained in:
Florian Bruhin 2015-09-17 12:41:28 +02:00
commit f978b545c5
4 changed files with 21 additions and 14 deletions

View File

@ -105,18 +105,18 @@
- fix issue735: assertion failures on debug versions of Python 3.4+ - fix issue735: assertion failures on debug versions of Python 3.4+
- change test module importing behaviour to append to sys.path - new option ``--import-mode`` to allow to change test module importing
instead of prepending. This better allows to run test modules behaviour to append to sys.path instead of prepending. This better allows
against installated versions of a package even if the package to run test modules against installated versions of a package even if the
under test has the same import root. In this example:: package under test has the same import root. In this example::
testing/__init__.py testing/__init__.py
testing/test_pkg_under_test.py testing/test_pkg_under_test.py
pkg_under_test/ pkg_under_test/
the tests will preferrably run against the installed version the tests will run against the installed version
of pkg_under_test whereas before they would always pick of pkg_under_test when ``--import-mode=append`` is used whereas
up the local version. Thanks Holger Krekel. by default they would always pick up the local version. Thanks Holger Krekel.
- pytester: add method ``TmpTestdir.delete_loaded_modules()``, and call it - pytester: add method ``TmpTestdir.delete_loaded_modules()``, and call it
from ``inline_run()`` to allow temporary modules to be reloaded. from ``inline_run()`` to allow temporary modules to be reloaded.

View File

@ -1,2 +0,0 @@
#
__version__ = '2.8.0.dev5'

View File

@ -173,6 +173,12 @@ def pytest_addoption(parser):
help="prefixes or glob names for Python test function and " help="prefixes or glob names for Python test function and "
"method discovery") "method discovery")
group.addoption("--import-mode", default="prepend",
choices=["prepend", "append"], dest="importmode",
help="prepend/append to sys.path when importing test modules, "
"default is to prepend.")
def pytest_cmdline_main(config): def pytest_cmdline_main(config):
if config.option.showfixtures: if config.option.showfixtures:
showfixtures(config) showfixtures(config)
@ -557,8 +563,9 @@ class Module(pytest.File, PyCollector):
def _importtestmodule(self): def _importtestmodule(self):
# we assume we are only called once per module # we assume we are only called once per module
importmode = self.config.getoption("--import-mode")
try: try:
mod = self.fspath.pyimport(ensuresyspath="append") mod = self.fspath.pyimport(ensuresyspath=importmode)
except SyntaxError: except SyntaxError:
raise self.CollectError( raise self.CollectError(
py.code.ExceptionInfo().getrepr(style="short")) py.code.ExceptionInfo().getrepr(style="short"))

View File

@ -27,7 +27,7 @@ class TestModule:
"*HINT*", "*HINT*",
]) ])
def test_import_appends_for_import(self, testdir, monkeypatch): def test_import_prepend_append(self, testdir, monkeypatch):
syspath = list(sys.path) syspath = list(sys.path)
monkeypatch.setattr(sys, "path", syspath) monkeypatch.setattr(sys, "path", syspath)
root1 = testdir.mkdir("root1") root1 = testdir.mkdir("root1")
@ -35,15 +35,17 @@ class TestModule:
root1.ensure("x456.py") root1.ensure("x456.py")
root2.ensure("x456.py") root2.ensure("x456.py")
p = root2.join("test_x456.py") p = root2.join("test_x456.py")
monkeypatch.syspath_prepend(str(root1))
p.write(dedent("""\ p.write(dedent("""\
import x456 import x456
def test(): def test():
assert x456.__file__.startswith(%r) assert x456.__file__.startswith(%r)
""" % str(root1))) """ % str(root2)))
syspath.insert(0, str(root1))
with root2.as_cwd(): with root2.as_cwd():
reprec = testdir.inline_run("--import-mode=append")
reprec.assertoutcome(passed=0, failed=1)
reprec = testdir.inline_run() reprec = testdir.inline_run()
reprec.assertoutcome(passed=1) reprec.assertoutcome(passed=1)
def test_syntax_error_in_module(self, testdir): def test_syntax_error_in_module(self, testdir):
modcol = testdir.getmodulecol("this is a syntax error") modcol = testdir.getmodulecol("this is a syntax error")