Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not include the root session with -k #7046

Merged
merged 2 commits into from
May 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions changelog/7040.breaking.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
``-k`` no longer matches against the names of the directories outside the test session root.

Also, ``pytest.Package.name`` is now just the name of the directory containing the package's
``__init__.py`` file, instead of the full path. This is consistent with how the other nodes
are named, and also one of the reasons why ``-k`` would match against any directory containing
the test suite.
2 changes: 1 addition & 1 deletion src/_pytest/mark/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def from_item(cls, item: "Item") -> "KeywordMatcher":
import pytest

for item in item.listchain():
if not isinstance(item, pytest.Instance):
if not isinstance(item, (pytest.Instance, pytest.Session)):
mapped_names.add(item.name)

# Add the names added as extra keywords to current or parent items
Expand Down
3 changes: 1 addition & 2 deletions src/_pytest/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -568,8 +568,7 @@ def __init__(
nodes.FSCollector.__init__(
self, fspath, parent=parent, config=config, session=session, nodeid=nodeid
)

self.name = fspath.dirname
self.name = os.path.basename(str(fspath.dirname))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that one looks like a breaking change in isolation, does it bring us in line with other details or is it a new difference?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

crosschecked, it brings us in line


def setup(self):
# not using fixtures to call setup_module here because autouse fixtures
Expand Down
12 changes: 6 additions & 6 deletions testing/test_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -1004,7 +1004,7 @@ def test_collect_init_tests(testdir):
result.stdout.fnmatch_lines(
[
"collected 2 items",
"<Package *",
"<Package tests>",
" <Module __init__.py>",
" <Function test_init>",
" <Module test_foo.py>",
Expand All @@ -1015,7 +1015,7 @@ def test_collect_init_tests(testdir):
result.stdout.fnmatch_lines(
[
"collected 2 items",
"<Package *",
"<Package tests>",
" <Module __init__.py>",
" <Function test_init>",
" <Module test_foo.py>",
Expand All @@ -1027,7 +1027,7 @@ def test_collect_init_tests(testdir):
result.stdout.fnmatch_lines(
[
"collected 2 items",
"<Package */tests>",
"<Package tests>",
" <Module __init__.py>",
" <Function test_init>",
" <Module test_foo.py>",
Expand All @@ -1039,7 +1039,7 @@ def test_collect_init_tests(testdir):
result.stdout.fnmatch_lines(
[
"collected 2 items",
"<Package */tests>",
"<Package tests>",
" <Module __init__.py>",
" <Function test_init>",
" <Module test_foo.py>",
Expand All @@ -1048,12 +1048,12 @@ def test_collect_init_tests(testdir):
)
result = testdir.runpytest("./tests/test_foo.py", "--collect-only")
result.stdout.fnmatch_lines(
["<Package */tests>", " <Module test_foo.py>", " <Function test_foo>"]
["<Package tests>", " <Module test_foo.py>", " <Function test_foo>"]
)
result.stdout.no_fnmatch_line("*test_init*")
result = testdir.runpytest("./tests/__init__.py", "--collect-only")
result.stdout.fnmatch_lines(
["<Package */tests>", " <Module __init__.py>", " <Function test_init>"]
["<Package tests>", " <Module __init__.py>", " <Function test_init>"]
)
result.stdout.no_fnmatch_line("*test_foo*")

Expand Down
30 changes: 30 additions & 0 deletions testing/test_mark.py
Original file line number Diff line number Diff line change
Expand Up @@ -834,6 +834,36 @@ def test_one(): assert 1
deselected_tests = dlist[0].items
assert len(deselected_tests) == 1

def test_no_match_directories_outside_the_suite(self, testdir):
"""
-k should not match against directories containing the test suite (#7040).
"""
test_contents = """
def test_aaa(): pass
def test_ddd(): pass
"""
testdir.makepyfile(
**{"ddd/tests/__init__.py": "", "ddd/tests/test_foo.py": test_contents}
)

def get_collected_names(*args):
_, rec = testdir.inline_genitems(*args)
calls = rec.getcalls("pytest_collection_finish")
assert len(calls) == 1
return [x.name for x in calls[0].session.items]

# sanity check: collect both tests in normal runs
assert get_collected_names() == ["test_aaa", "test_ddd"]

# do not collect anything based on names outside the collection tree
assert get_collected_names("-k", testdir.tmpdir.basename) == []

# "-k ddd" should only collect "test_ddd", but not
# 'test_aaa' just because one of its parent directories is named "ddd";
# this was matched previously because Package.name would contain the full path
# to the package
assert get_collected_names("-k", "ddd") == ["test_ddd"]


class TestMarkDecorator:
@pytest.mark.parametrize(
Expand Down