Skip to content

Commit

Permalink
-k should not match session name
Browse files Browse the repository at this point in the history
  • Loading branch information
blueyed authored and nicoddemus committed May 16, 2020
1 parent d4dfe86 commit f5228c4
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 19 deletions.
1 change: 1 addition & 0 deletions changelog/7040.improvement.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
``-k`` no longer matches against the 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
37 changes: 19 additions & 18 deletions testing/test_mark.py
Original file line number Diff line number Diff line change
Expand Up @@ -814,25 +814,26 @@ def test_one():
passed, skipped, failed = reprec.countoutcomes()
assert passed + skipped + failed == 0

@pytest.mark.parametrize(
"keyword", ["__", "+", ".."],
)
def test_no_magic_values(self, testdir, keyword: str) -> None:
"""Make sure the tests do not match on magic values,
no double underscored values, like '__dict__' and '+'.
"""
p = testdir.makepyfile(
"""
def test_one(): assert 1
def test_values_that_should_not_match(self, testdir) -> None:
"""Some values should not ever be matched by -k
"""
)

reprec = testdir.inline_run("-k", keyword, p)
passed, skipped, failed = reprec.countoutcomes()
dlist = reprec.getcalls("pytest_deselected")
assert passed + skipped + failed == 0
deselected_tests = dlist[0].items
assert len(deselected_tests) == 1
p = testdir.makepyfile("def test_one(self): assert 1")

def assert_test_is_not_selected(keyword):
reprec = testdir.inline_run("-k", keyword, p)
passed, skipped, failed = reprec.countoutcomes()
dlist = reprec.getcalls("pytest_deselected")
assert passed + skipped + failed == 0
deselected_tests = dlist[0].items
assert len(deselected_tests) == 1

# should not match dunder objects
assert_test_is_not_selected("__")
# should not match against invalid expressions
assert_test_is_not_selected("+")
assert_test_is_not_selected("..")
# should not match against session
assert_test_is_not_selected(testdir.tmpdir.basename)


class TestMarkDecorator:
Expand Down

0 comments on commit f5228c4

Please sign in to comment.