From a62d12634c01b73930e46dde8af0bc9aa2894785 Mon Sep 17 00:00:00 2001 From: holger krekel Date: Wed, 16 Sep 2015 22:12:20 +0200 Subject: [PATCH 1/2] revert importing test module behaviour to prepend by default --- CHANGELOG | 14 +++++++------- _pytest/__init__.py | 6 ++++-- _pytest/python.py | 9 ++++++++- testing/python/collect.py | 10 ++++++---- 4 files changed, 25 insertions(+), 14 deletions(-) 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 index 558539cfe..18cc26d31 100644 --- a/_pytest/__init__.py +++ b/_pytest/__init__.py @@ -1,2 +1,4 @@ -# -__version__ = '2.8.0.dev5' +# coding: utf-8 +# file generated by setuptools_scm +# don't change, don't track in version control +version = '2.7.4.dev394+ng2575053.d20150916' 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") From 6192e74f039faec00337cd6c1e3f45d36fb889dc Mon Sep 17 00:00:00 2001 From: holger krekel Date: Thu, 17 Sep 2015 10:51:03 +0200 Subject: [PATCH 2/2] remove __init__.py as advised by ronny --- _pytest/__init__.py | 4 ---- 1 file changed, 4 deletions(-) delete mode 100644 _pytest/__init__.py diff --git a/_pytest/__init__.py b/_pytest/__init__.py deleted file mode 100644 index 18cc26d31..000000000 --- a/_pytest/__init__.py +++ /dev/null @@ -1,4 +0,0 @@ -# coding: utf-8 -# file generated by setuptools_scm -# don't change, don't track in version control -version = '2.7.4.dev394+ng2575053.d20150916'