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

Make keyword expression matching case-insensitive #6316

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Expand Up @@ -53,8 +53,8 @@ repos:
- id: changelogs-rst
name: changelog filenames
language: fail
entry: 'changelog files must be named ####.(feature|bugfix|doc|deprecation|removal|vendor|trivial).rst'
exclude: changelog/(\d+\.(feature|improvement|bugfix|doc|deprecation|removal|vendor|trivial).rst|README.rst|_template.rst)
entry: 'changelog files must be named ####.(breaking|bugfix|deprecation|doc|feature|improvement|trivial|vendor).rst'
exclude: changelog/(\d+\.(breaking|bugfix|deprecation|doc|feature|improvement|trivial|vendor).rst|README.rst|_template.rst)
files: ^changelog/
- id: py-deprecated
name: py library is deprecated
Expand Down
5 changes: 3 additions & 2 deletions AUTHORS
Expand Up @@ -59,12 +59,12 @@ Christian Fetzer
Christian Neumüller
Christian Theunert
Christian Tismer
Christopher Gilling
Christoph Buelter
Christopher Dignam
Christopher Gilling
CrazyMerlyn
Cyrus Maden
Damian Skrzypczak
Dhiren Serai
Daniel Grana
Daniel Hahler
Daniel Nuri
Expand All @@ -79,6 +79,7 @@ David Szotten
David Vierra
Daw-Ran Liou
Denis Kirisov
Dhiren Serai
Diego Russo
Dmitry Dygalo
Dmitry Pribysh
Expand Down
1 change: 1 addition & 0 deletions changelog/6316.breaking.rst
@@ -0,0 +1 @@
Matching of ``-k EXPRESSION`` to test names is now case-insensitive.
2 changes: 1 addition & 1 deletion changelog/README.rst
Expand Up @@ -18,7 +18,7 @@ Each file should be named like ``<ISSUE>.<TYPE>.rst``, where
* ``bugfix``: fixes a reported bug.
* ``doc``: documentation improvement, like rewording an entire session or adding missing docs.
* ``deprecation``: feature deprecation.
* ``removal``: feature removal.
* ``breaking``: a change which may break existing suites, such as feature removal or behavior change.
* ``vendor``: changes in packages vendored in pytest.
* ``trivial``: fixing a small typo or internal change that might be noteworthy.

Expand Down
4 changes: 4 additions & 0 deletions doc/en/example/markers.rst
Expand Up @@ -148,6 +148,10 @@ which implements a substring match on the test names instead of the
exact match on markers that ``-m`` provides. This makes it easy to
select tests based on their names:

.. versionadded: 5.4

The expression matching is now case-insensitive.

.. code-block:: pytest

$ pytest -v -k http # running with the above defined example module
Expand Down
4 changes: 2 additions & 2 deletions doc/en/usage.rst
Expand Up @@ -94,8 +94,8 @@ Pytest supports several ways to run and select tests from the command-line.

pytest -k "MyClass and not method"

This will run tests which contain names that match the given *string expression*, which can
include Python operators that use filenames, class names and function names as variables.
This will run tests which contain names that match the given *string expression* (case-insensitive),
which can include Python operators that use filenames, class names and function names as variables.
The example above will run ``TestMyClass.test_something`` but not ``TestMyClass.test_method_simple``.

.. _nodeids:
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Expand Up @@ -16,8 +16,8 @@ title_format = "pytest {version} ({project_date})"
template = "changelog/_template.rst"

[[tool.towncrier.type]]
directory = "removal"
name = "Removals"
directory = "breaking"
name = "Breaking Changes"
showcontent = true

[[tool.towncrier.type]]
Expand Down
3 changes: 2 additions & 1 deletion src/_pytest/mark/__init__.py
Expand Up @@ -52,7 +52,8 @@ def pytest_addoption(parser):
"-k 'not test_method and not test_other' will eliminate the matches. "
"Additionally keywords are matched to classes and functions "
"containing extra names in their 'extra_keyword_matches' set, "
"as well as functions which have names assigned directly to them.",
"as well as functions which have names assigned directly to them. "
"The matching is case-insensitive.",
)

group._addoption(
Expand Down
10 changes: 9 additions & 1 deletion src/_pytest/mark/legacy.py
Expand Up @@ -57,7 +57,15 @@ def from_item(cls, item):
return cls(mapped_names)

def __getitem__(self, subname):
for name in self._names:
"""Return whether subname is included within stored names.

The string inclusion check is case-insensitive.

"""
subname = subname.lower()
names = (name.lower() for name in self._names)

for name in names:
if subname in name:
return True
return False
Expand Down
37 changes: 37 additions & 0 deletions testing/test_collection.py
Expand Up @@ -809,6 +809,43 @@ def test___repr__():
reprec = testdir.inline_run("-k repr")
reprec.assertoutcome(passed=1, failed=0)

def test_keyword_matching_is_case_insensitive_by_default(self, testdir):
"""Check that selection via -k EXPRESSION is case-insensitive.

Since markers are also added to the node keywords, they too can
be matched without having to think about case sensitivity.

"""
testdir.makepyfile(
"""
import pytest

def test_sPeCiFiCToPiC_1():
assert True

class TestSpecificTopic_2:
def test(self):
assert True

@pytest.mark.sPeCiFiCToPic_3
def test():
assert True

@pytest.mark.sPeCiFiCToPic_4
class Test:
def test(self):
assert True

cb109 marked this conversation as resolved.
Show resolved Hide resolved
def test_failing_5():
assert False, "This should not match"

"""
)
num_matching_tests = 4
for expression in ("specifictopic", "SPECIFICTOPIC", "SpecificTopic"):
reprec = testdir.inline_run("-k " + expression)
reprec.assertoutcome(passed=num_matching_tests, failed=0)


COLLECTION_ERROR_PY_FILES = dict(
test_01_failure="""
Expand Down