Running a Subset of Tests

I’ve talked about how you can place markers on tests and run tests based on markers. You can run a subset of tests in several other ways. You can run all of the tests, or you can select a single directory, file, class within a file, or an individual test in a file or class. You haven’t seen test classes used yet, so you’ll look at one in this section. You can also use an expression to match test names. Let’s take a look at these.

A Single Directory

To run all the tests from one directory, use the directory as a parameter to pytest:

 $ ​​cd​​ ​​/path/to/code/ch2/tasks_proj
 $ ​​pytest​​ ​​tests/func​​ ​​--tb=no
 ===================== test session starts ======================
 collected 50 items
 
 tests/func/test_add.py ...

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.