Skip to content

pytest -k doesn't work with "\"? #8983

@nguydavi

Description

@nguydavi

Discussed in #8982

Originally posted by nguydavi August 7, 2021
Hey!

I've been trying to use pytest -k passing the name I got by parametrizing my test. For example,

$ pytest -vk 'test_solution[foo.py-5\n10\n-16\n]' validate.py
=========================================================================================================== test session starts ============================================================================================================platform linux -- Python 3.8.10, pytest-6.2.4, py-1.10.0, pluggy-0.13.1 -- /usr/bin/python3
cachedir: .pytest_cache
rootdir: /home/david/foo
collected 4 items

========================================================================================================== no tests ran in 0.01s ===========================================================================================================ERROR: Wrong expression passed to '-k': test_solution[foo.py-5\n10\n-16\n]: at column 23: unexpected character "\"

Note the error message

ERROR: Wrong expression passed to '-k': test_solution[foo.py-5\n10\n-16\n]: at column 23: unexpected character "\"

I tried escaping the \ but that didn't work, the only way I can make it work is to remove the backslashes completely,

$ pytest -vk 'test_solution[foo.py-5 and 10' validate.py

Is \ just not supported by -k ? Or am I missing something ?

Thanks!

EDIT:
A possible test case

@pytest.mark.parametrize(
    "param1, param2",
    [
        pytest.param(
            '5\n10\n', '16\n'
        ),
    ],
)
def test_solution(param1, param2):
  pass

Which is then referred by pytest as test_solution[5\n10\n-16\n] . Essentially a new line character \n in the string brings the issue (or any escaped character probably)

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: selectionrelated to test selection from the command line

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions