[pytest-dev] -k EXPRESSION: How to allow for case-insensitive matching

Christoph Buelter buelter.christoph at gmail.com
Wed Dec 4 07:37:35 EST 2019


Greetings,

the -k EXPRESSION matching behaviour of the pytest CLI, which deselects
tests based on the given expression, is case-sensitive. The relevant code
of the KeywordMapping is just a few lines:

https://github.com/pytest-dev/pytest/blob/master/src/_pytest/mark/legacy.py#L59-L63

That means using

    pytest -k statistics

will match

    def test_statistics():
        ...

but it will not match:

    class TestStatistics:
        ...

I would have to do some shenanigans like

    pytest -k "atistics"

or

    pytest -k "statistics or Statistics"

to match both tests, just because one happens to be a class and the other a
function. Personally, I find that a bit strict and limiting, considering it
tests for string inclusion which is rather lax in comparison. It would be
very convenient to be able to match both with a single expression, e.g. to
filter tests by topics. I know I can mark tests or add
extra_keyword_matches, but that isn't very convenient. I also acknowledge
that the default behaviour can't be changed as it would break existing code.

I have not found a way to customize this with the existing CLI/hooks.

Do you think there is any possibility to add some CLI/config option like
"--case-insensitive-expression" to make this configurable, or do you
consider this to belong to userland? A hook to allow changing the behaviour
from a pytest plugin would be totally sufficient as well. I'd be willing to
provide a PR if there is any interest.

My current workaround is to monkeypatch the behaviour at runtime, which
does the job, but is kind of magical and will break when pytests internal
structure changes in the future:

https://gist.github.com/cb109/de0cf181aaaadf13539be9854c53586c

Thanks for pytest by the way :)

Cheers,
Christoph
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/pytest-dev/attachments/20191204/fe08bdf3/attachment.html>


More information about the pytest-dev mailing list