Skip to content

Commit 0a03217

Browse files
authored
Merge pull request #7046 from blueyed/k-skip-session-upstream
2 parents 8821431 + c5b367b commit 0a03217

File tree

5 files changed

+44
-9
lines changed

5 files changed

+44
-9
lines changed

changelog/7040.breaking.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
``-k`` no longer matches against the names of the directories outside the test session root.
2+
3+
Also, ``pytest.Package.name`` is now just the name of the directory containing the package's
4+
``__init__.py`` file, instead of the full path. This is consistent with how the other nodes
5+
are named, and also one of the reasons why ``-k`` would match against any directory containing
6+
the test suite.

src/_pytest/mark/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ def from_item(cls, item: "Item") -> "KeywordMatcher":
136136
import pytest
137137

138138
for item in item.listchain():
139-
if not isinstance(item, pytest.Instance):
139+
if not isinstance(item, (pytest.Instance, pytest.Session)):
140140
mapped_names.add(item.name)
141141

142142
# Add the names added as extra keywords to current or parent items

src/_pytest/python.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -571,8 +571,7 @@ def __init__(
571571
nodes.FSCollector.__init__(
572572
self, fspath, parent=parent, config=config, session=session, nodeid=nodeid
573573
)
574-
575-
self.name = fspath.dirname
574+
self.name = os.path.basename(str(fspath.dirname))
576575

577576
def setup(self):
578577
# not using fixtures to call setup_module here because autouse fixtures

testing/test_collection.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,7 +1004,7 @@ def test_collect_init_tests(testdir):
10041004
result.stdout.fnmatch_lines(
10051005
[
10061006
"collected 2 items",
1007-
"<Package *",
1007+
"<Package tests>",
10081008
" <Module __init__.py>",
10091009
" <Function test_init>",
10101010
" <Module test_foo.py>",
@@ -1015,7 +1015,7 @@ def test_collect_init_tests(testdir):
10151015
result.stdout.fnmatch_lines(
10161016
[
10171017
"collected 2 items",
1018-
"<Package *",
1018+
"<Package tests>",
10191019
" <Module __init__.py>",
10201020
" <Function test_init>",
10211021
" <Module test_foo.py>",
@@ -1027,7 +1027,7 @@ def test_collect_init_tests(testdir):
10271027
result.stdout.fnmatch_lines(
10281028
[
10291029
"collected 2 items",
1030-
"<Package */tests>",
1030+
"<Package tests>",
10311031
" <Module __init__.py>",
10321032
" <Function test_init>",
10331033
" <Module test_foo.py>",
@@ -1039,7 +1039,7 @@ def test_collect_init_tests(testdir):
10391039
result.stdout.fnmatch_lines(
10401040
[
10411041
"collected 2 items",
1042-
"<Package */tests>",
1042+
"<Package tests>",
10431043
" <Module __init__.py>",
10441044
" <Function test_init>",
10451045
" <Module test_foo.py>",
@@ -1048,12 +1048,12 @@ def test_collect_init_tests(testdir):
10481048
)
10491049
result = testdir.runpytest("./tests/test_foo.py", "--collect-only")
10501050
result.stdout.fnmatch_lines(
1051-
["<Package */tests>", " <Module test_foo.py>", " <Function test_foo>"]
1051+
["<Package tests>", " <Module test_foo.py>", " <Function test_foo>"]
10521052
)
10531053
result.stdout.no_fnmatch_line("*test_init*")
10541054
result = testdir.runpytest("./tests/__init__.py", "--collect-only")
10551055
result.stdout.fnmatch_lines(
1056-
["<Package */tests>", " <Module __init__.py>", " <Function test_init>"]
1056+
["<Package tests>", " <Module __init__.py>", " <Function test_init>"]
10571057
)
10581058
result.stdout.no_fnmatch_line("*test_foo*")
10591059

testing/test_mark.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -834,6 +834,36 @@ def test_one(): assert 1
834834
deselected_tests = dlist[0].items
835835
assert len(deselected_tests) == 1
836836

837+
def test_no_match_directories_outside_the_suite(self, testdir):
838+
"""
839+
-k should not match against directories containing the test suite (#7040).
840+
"""
841+
test_contents = """
842+
def test_aaa(): pass
843+
def test_ddd(): pass
844+
"""
845+
testdir.makepyfile(
846+
**{"ddd/tests/__init__.py": "", "ddd/tests/test_foo.py": test_contents}
847+
)
848+
849+
def get_collected_names(*args):
850+
_, rec = testdir.inline_genitems(*args)
851+
calls = rec.getcalls("pytest_collection_finish")
852+
assert len(calls) == 1
853+
return [x.name for x in calls[0].session.items]
854+
855+
# sanity check: collect both tests in normal runs
856+
assert get_collected_names() == ["test_aaa", "test_ddd"]
857+
858+
# do not collect anything based on names outside the collection tree
859+
assert get_collected_names("-k", testdir.tmpdir.basename) == []
860+
861+
# "-k ddd" should only collect "test_ddd", but not
862+
# 'test_aaa' just because one of its parent directories is named "ddd";
863+
# this was matched previously because Package.name would contain the full path
864+
# to the package
865+
assert get_collected_names("-k", "ddd") == ["test_ddd"]
866+
837867

838868
class TestMarkDecorator:
839869
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)