List available tests with py.test

PythonUnit TestingPytest

Python Problem Overview


I can't find a way to list the tests which I can call with py.test -k PATTERN

How can I see the list of the available tests?

Python Solutions


Solution 1 - Python

You can also use --collect-only, this will show a tree-like structure of the collected nodes. Usually one can simply -k on the names of the Function nodes.

Solution 2 - Python

You should use the flag --collect-only. If you are using pytest 5.3.0 or newer use --co.

pytest 5.3.0+

pytest --co

previous versions

pytest --collect-only

You can use this flag among other flags, so in your case pytest --co -k PATTERN.

Solution 3 - Python

Both --collect-only and --setup-plan will print out your test files and individual tests.

--collect-only (or --co) prints in a <[type] [name]> format

pytest --collect-only
# or
pytest --co

# <Module test_file.py>
#   <Function test__my_awesome_code_does_the_awesome_thing>

--setup-plan is more verbose and prints the entire test-run plan (including any setup, teardown, and fixtures used for each test). It also prints the entire path for each test.

pytest --setup-plan

# tests/test_file.py
#     SETUP    [...]
#     tests/test_file.py::test__my_awesome_code_does_the_awesome_thing (fixtures used: [...])
#     TEARDOWN [...]
    

Solution 4 - Python

-v verbose tells you which test cases are run, i.e. which did match your PATTERN.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionguettliView Question on Stackoverflow
Solution 1 - PythonflubView Answer on Stackoverflow
Solution 2 - PythonlmiguelvargasfView Answer on Stackoverflow
Solution 3 - PythonMandi.AlexanderView Answer on Stackoverflow
Solution 4 - PythonZdenek MaxaView Answer on Stackoverflow