Skip to content

Commit 31af381

Browse files
committed
Merged in hpk42/pytest-patches/prefer_installed (pull request #275)
change test module importing behaviour to append to sys.path
2 parents bac1ccd + 5c8e5ac commit 31af381

File tree

8 files changed

+46
-13
lines changed

8 files changed

+46
-13
lines changed

CHANGELOG

+13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
11
2.8.0.dev (compared to 2.7.X)
22
-----------------------------
33

4+
- change test module importing behaviour to append to sys.path
5+
instead of prepending. This better allows to run test modules
6+
against installated versions of a package even if the package
7+
under test has the same import root. In this example::
8+
9+
testing/__init__.py
10+
testing/test_pkg_under_test.py
11+
pkg_under_test/
12+
13+
the tests will preferrably run against the installed version
14+
of pkg_under_test whereas before they would always pick
15+
up the local version. Thanks Holger Krekel.
16+
417

518
2.7.1.dev (compared to 2.7.0)
619
-----------------------------

_pytest/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
#
2-
__version__ = '2.8.0.dev1'
2+
__version__ = '2.8.0.dev2'

_pytest/pytester.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ def __init__(self, request):
254254
break
255255
self.tmpdir = tmpdir
256256
self.plugins = []
257-
self._syspathremove = []
257+
self._savesyspath = list(sys.path)
258258
self.chdir() # always chdir
259259
self.request.addfinalizer(self.finalize)
260260

@@ -270,8 +270,7 @@ def finalize(self):
270270
has finished.
271271
272272
"""
273-
for p in self._syspathremove:
274-
sys.path.remove(p)
273+
sys.path[:] = self._savesyspath
275274
if hasattr(self, '_olddir'):
276275
self._olddir.chdir()
277276
# delete modules that have been loaded from tmpdir
@@ -370,7 +369,6 @@ def syspathinsert(self, path=None):
370369
if path is None:
371370
path = self.tmpdir
372371
sys.path.insert(0, str(path))
373-
self._syspathremove.append(str(path))
374372

375373
def mkdir(self, name):
376374
"""Create a new (sub)directory."""

_pytest/python.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ def collect(self):
485485
def _importtestmodule(self):
486486
# we assume we are only called once per module
487487
try:
488-
mod = self.fspath.pyimport(ensuresyspath=True)
488+
mod = self.fspath.pyimport(ensuresyspath="append")
489489
except SyntaxError:
490490
raise self.CollectError(
491491
py.code.ExceptionInfo().getrepr(style="short"))
@@ -2062,3 +2062,4 @@ def get_scope_node(node, scope):
20622062
return node.session
20632063
raise ValueError("unknown scope")
20642064
return node.getparent(cls)
2065+

setup.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ def get_version():
3131
def has_environment_marker_support():
3232
"""
3333
Tests that setuptools has support for PEP-426 environment marker support.
34-
35-
The first known release to support it is 0.7 (and the earliest on PyPI seems to be 0.7.2
34+
35+
The first known release to support it is 0.7 (and the earliest on PyPI seems to be 0.7.2
3636
so we're using that), see: http://pythonhosted.org/setuptools/history.html#id142
37-
37+
3838
References:
39-
39+
4040
* https://wheel.readthedocs.org/en/latest/index.html#defining-conditional-dependencies
4141
* https://www.python.org/dev/peps/pep-0426/#environment-markers
4242
"""
@@ -48,7 +48,7 @@ def has_environment_marker_support():
4848

4949

5050
def main():
51-
install_requires = ['py>=1.4.25']
51+
install_requires = ['py>=1.4.27.dev2']
5252
extras_require = {}
5353
if has_environment_marker_support():
5454
extras_require[':python_version=="2.6" or python_version=="3.0" or python_version=="3.1"'] = ['argparse']

testing/python/collect.py

+20
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import sys
2+
from textwrap import dedent
13
import pytest, py
24

35
class TestModule:
@@ -23,6 +25,24 @@ def test_import_duplicate(self, testdir):
2325
"*HINT*",
2426
])
2527

28+
def test_import_appends_for_import(self, testdir, monkeypatch):
29+
syspath = list(sys.path)
30+
monkeypatch.setattr(sys, "path", syspath)
31+
root1 = testdir.mkdir("root1")
32+
root2 = testdir.mkdir("root2")
33+
root1.ensure("x456.py")
34+
root2.ensure("x456.py")
35+
p = root2.join("test_x456.py")
36+
p.write(dedent("""\
37+
import x456
38+
def test():
39+
assert x456.__file__.startswith(%r)
40+
""" % str(root1)))
41+
syspath.insert(0, str(root1))
42+
with root2.as_cwd():
43+
reprec = testdir.inline_run()
44+
reprec.assertoutcome(passed=1)
45+
2646
def test_syntax_error_in_module(self, testdir):
2747
modcol = testdir.getmodulecol("this is a syntax error")
2848
pytest.raises(modcol.CollectError, modcol.collect)

testing/python/integration.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ def test_pytestconfig_is_session_scoped():
238238

239239

240240
class TestNoselikeTestAttribute:
241-
def test_module(self, testdir):
241+
def test_module_with_global_test(self, testdir):
242242
testdir.makepyfile("""
243243
__test__ = False
244244
def test_hello():
@@ -248,7 +248,7 @@ def test_hello():
248248
assert not reprec.getfailedcollections()
249249
calls = reprec.getreports("pytest_runtest_logreport")
250250
assert not calls
251-
251+
252252
def test_class_and_method(self, testdir):
253253
testdir.makepyfile("""
254254
__test__ = True

testing/test_core.py

+1
Original file line numberDiff line numberDiff line change
@@ -772,6 +772,7 @@ def test_default_markers(testdir):
772772
])
773773

774774
def test_importplugin_issue375(testdir):
775+
testdir.syspathinsert(testdir.tmpdir)
775776
testdir.makepyfile(qwe="import aaaa")
776777
excinfo = pytest.raises(ImportError, lambda: importplugin("qwe"))
777778
assert "qwe" not in str(excinfo.value)

0 commit comments

Comments
 (0)