Running tests
Pytest can run your tests in a number of ways. Let's quickly get into the basics now and, later on in the chapter, we will move on to more advanced options.
You can start by just simply executing the pytest command:
λ pytest
This will find all of the test_*.py and *_test.py modules in the current directory and below recursively, and will run all of the tests found in those files:
- You can reduce the search to specific directories:
λ pytest tests/core tests/contrib
- You can also mix any number of files and directories:
λ pytest tests/core tests/contrib/test_text_plugin.py
- You can execute specific tests by using the syntax <test-file>::<test-function-name>:
λ pytest tests/core/test_core.py::test_regex_matching
- You can execute all of the test methods of a test class:
λ pytest tests/contrib/test_text_plugin.py::TestPluginHooks
- You can execute a specific test method of a test class using the syntax <test-file>::<test-class>::<test-method-name>:
λ pytest tests/contrib/
test_text_plugin.py::TestPluginHooks::test_registration
The syntax used above is created internally by pytest, is unique to each test collected, and is called a node id or item id. It basically consists of the filename of the testing module, class, and functions joined together by the :: characters.
Pytest will show a more verbose output, which includes node IDs, with the -v flag:
λ pytest tests/core -v
======================== test session starts ========================
...
collected 6 items
tests\core\test_core.py::test_regex_matching PASSED [ 16%]
tests\core\test_core.py::test_check_options FAILED [ 33%]
tests\core\test_core.py::test_type_checking FAILED [ 50%]
tests\core\test_parser.py::test_parse_expr PASSED [ 66%]
tests\core\test_parser.py::test_parse_num PASSED [ 83%]
tests\core\test_parser.py::test_parse_add PASSED [100%]
To see which tests there are without running them, use the --collect-only flag:
λ pytest tests/core --collect-only
======================== test session starts ========================
...
collected 6 items
<Module 'tests/core/test_core.py'>
<Function 'test_regex_matching'>
<Function 'test_check_options'>
<Function 'test_type_checking'>
<Module 'tests/core/test_parser.py'>
<Function 'test_parse_expr'>
<Function 'test_parse_num'>
<Function 'test_parse_add'>
=================== no tests ran in 0.01 seconds ====================
--collect-only is especially useful if you want to execute a specific test but can't remember its exact name.