diff --git a/src/_pytest/nodes.py b/src/_pytest/nodes.py index 76d8d6a8a..032d4e1d2 100644 --- a/src/_pytest/nodes.py +++ b/src/_pytest/nodes.py @@ -447,7 +447,7 @@ class Collector(Node): def _check_initialpaths_for_relpath(session, fspath): for initial_path in session._initialpaths: if fspath.common(initial_path) == initial_path: - return fspath.relto(initial_path.dirname) + return fspath.relto(initial_path) class FSCollector(Collector): diff --git a/testing/acceptance_test.py b/testing/acceptance_test.py index 19d7946cf..315bd570a 100644 --- a/testing/acceptance_test.py +++ b/testing/acceptance_test.py @@ -663,11 +663,11 @@ class TestInvocationVariants(object): assert result.ret == 0 result.stdout.fnmatch_lines( [ - "*test_hello.py::test_hello*PASSED*", - "*test_hello.py::test_other*PASSED*", - "*test_world.py::test_world*PASSED*", - "*test_world.py::test_other*PASSED*", - "*4 passed*", + "test_hello.py::test_hello*PASSED*", + "test_hello.py::test_other*PASSED*", + "ns_pkg/world/test_world.py::test_world*PASSED*", + "ns_pkg/world/test_world.py::test_other*PASSED*", + "*4 passed in*", ] ) diff --git a/testing/test_nodes.py b/testing/test_nodes.py index d55184ef2..0a7f7ec6f 100644 --- a/testing/test_nodes.py +++ b/testing/test_nodes.py @@ -1,3 +1,5 @@ +import py + import pytest from _pytest import nodes @@ -29,3 +31,23 @@ def test_std_warn_not_pytestwarning(testdir): ) with pytest.raises(ValueError, match=".*instance of PytestWarning.*"): items[0].warn(UserWarning("some warning")) + + +def test__check_initialpaths_for_relpath(): + """Ensure that it handles dirs, and does not always use dirname.""" + cwd = py.path.local() + + class FakeSession: + _initialpaths = [cwd] + + assert nodes._check_initialpaths_for_relpath(FakeSession, cwd) == "" + + sub = cwd.join("file") + + class FakeSession: + _initialpaths = [cwd] + + assert nodes._check_initialpaths_for_relpath(FakeSession, sub) == "file" + + outside = py.path.local("/outside") + assert nodes._check_initialpaths_for_relpath(FakeSession, outside) is None diff --git a/testing/test_session.py b/testing/test_session.py index c1785b916..746618308 100644 --- a/testing/test_session.py +++ b/testing/test_session.py @@ -323,7 +323,11 @@ def test_rootdir_option_arg(testdir, monkeypatch, path): result = testdir.runpytest("--rootdir={}".format(path)) result.stdout.fnmatch_lines( - ["*rootdir: {}/root, inifile:*".format(testdir.tmpdir), "*1 passed*"] + [ + "*rootdir: {}/root, inifile:*".format(testdir.tmpdir), + "root/test_rootdir_option_arg.py *", + "*1 passed*", + ] )