Pytest is a software testing framework - it’s a CLI tool that can automatically find tests, run then and report the results.
Can be extended with your own or 3rd-party plugins.
Different ways to call pytest
Subset | Syntax |
---|---|
Single test method | pytest path/test_module.py::TestClass::test_method |
All tests in a class | pytest path/test_module.py::TestClass |
Single test function | pytest path/test_module.py::test_function |
All tests in a module | pytest path/test_module.py |
All tests in a directory | pytest path |
Tests matching a name pattern | pytest -k pattern |
Notable CLI flags
-v|vv|vvv
- verbosity
Assertion helper functions
Match exception message
Fixtures
pytest treats exceptions differently during fixtures compared to during a test function. An exception (or assert failure or call to pytest.fail()) that happens during the test code proper results in a “Fail” result. However, during a fixture, the test function is reported as “Error.” This distinction is helpful when debugging why a test didn’t pass. If a test results in “Fail,” the failure is somewhere in the test function (or something the function called). If a test results in “Error,” the failure is somewhere in a fixture.