Running a Subset of Tests

In the previous section, we used test classes to be able to run a subset of tests. Running just a small batch of tests is handy while debugging or if you want to limit the tests to a specific section of the code base you are working on at the time.

pytest allows you to run a subset of tests in several ways:

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

Tests by marker

Covered in Chapter 6, Markers.

We’ve used everything but pattern and marker ...

Get Python Testing with pytest now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.