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+
- change test module importing behaviour to append to sys.path
instead of prepending. This better allows to run test modules
against installated versions of a package even if the package
under test has the same import root. In this example::
- new option ``--import-mode`` to allow to change test module importing
behaviour to append to sys.path instead of prepending. This better allows
to run test modules against installated versions of a package even if the
package under test has the same import root. In this example::
testing/__init__.py
testing/test_pkg_under_test.py
pkg_under_test/
the tests will preferrably run against the installed version
of pkg_under_test whereas before they would always pick
up the local version. Thanks Holger Krekel.
the tests will run against the installed version
of pkg_under_test when ``--import-mode=append`` is used whereas
by default they would always pick 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.

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 "
"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):
if config.option.showfixtures:
showfixtures(config)
@ -557,8 +563,9 @@ class Module(pytest.File, PyCollector):
def _importtestmodule(self):
# we assume we are only called once per module
importmode = self.config.getoption("--import-mode")
try:
mod = self.fspath.pyimport(ensuresyspath="append")
mod = self.fspath.pyimport(ensuresyspath=importmode)
except SyntaxError:
raise self.CollectError(
py.code.ExceptionInfo().getrepr(style="short"))

View File

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