How to Run Pytest Tests from a List of Test Paths

Ofcourse! To achieve what you’re asking, you can use pytest with the --ignore and --maxfail options. This is one way to manually control which tests to run by specifying the paths you want to include.

First, create a file tests_to_run.txt containing the list of test paths like this:


tests_directory/foo.py::test_001

tests_directory/bar.py::test_some_other_test

Then, use the following command to feed the test paths from the file directly into pytest:


pytest $(cat tests_to_run.txt)

This simple method allows you to pytest run specific test cases listed in tests_to_run.txt. It’s straightforward and avoids the need for complex pattern matching.