diff --git a/_pytest/main.py b/_pytest/main.py index 3bd4022b7..eacae8dab 100644 --- a/_pytest/main.py +++ b/_pytest/main.py @@ -521,8 +521,16 @@ class FSCollector(Collector): super(FSCollector, self).__init__(name, parent, config, session) self.fspath = fspath + def _check_initialpaths_for_relpath(self): + for initialpath in self.session._initialpaths: + if self.fspath.common(initialpath) == initialpath: + return self.fspath.relto(initialpath.dirname) + def _makeid(self): relpath = self.fspath.relto(self.config.rootdir) + + if not relpath: + relpath = self._check_initialpaths_for_relpath() if os.sep != nodes.SEP: relpath = relpath.replace(os.sep, nodes.SEP) return relpath diff --git a/changelog/2775.bugfix b/changelog/2775.bugfix new file mode 100644 index 000000000..8123522ac --- /dev/null +++ b/changelog/2775.bugfix @@ -0,0 +1 @@ +Fix the bug where running pytest with "--pyargs" will result in Items with empty "parent.nodeid" if run from a different root directory. diff --git a/testing/acceptance_test.py b/testing/acceptance_test.py index 712776906..8a8c32762 100644 --- a/testing/acceptance_test.py +++ b/testing/acceptance_test.py @@ -624,8 +624,10 @@ class TestInvocationVariants(object): for p in search_path: monkeypatch.syspath_prepend(p) + os.chdir('world') # mixed module and filenames: - result = testdir.runpytest("--pyargs", "-v", "ns_pkg.hello", "world/ns_pkg") + result = testdir.runpytest("--pyargs", "-v", "ns_pkg.hello", "ns_pkg/world") + testdir.chdir() assert result.ret == 0 result.stdout.fnmatch_lines([ "*test_hello.py::test_hello*PASSED",