diff --git a/CHANGELOG b/CHANGELOG index 8a09ca076..a304476c4 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -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. diff --git a/_pytest/__init__.py b/_pytest/__init__.py deleted file mode 100644 index 558539cfe..000000000 --- a/_pytest/__init__.py +++ /dev/null @@ -1,2 +0,0 @@ -# -__version__ = '2.8.0.dev5' diff --git a/_pytest/python.py b/_pytest/python.py index 414ec545e..7b9921b9a 100644 --- a/_pytest/python.py +++ b/_pytest/python.py @@ -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")) diff --git a/testing/python/collect.py b/testing/python/collect.py index 7a53cb764..fba8c477c 100644 --- a/testing/python/collect.py +++ b/testing/python/collect.py @@ -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,15 +35,17 @@ 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) + reprec.assertoutcome(passed=1) def test_syntax_error_in_module(self, testdir): modcol = testdir.getmodulecol("this is a syntax error")