Is there a way to specify which pytest tests to run from a file? Is there a way to specify which pytest tests to run from a file? python python

Is there a way to specify which pytest tests to run from a file?

You can use -k option to run test cases with different patterns:

py.test tests_directory/ tests_directory/ -k 'test_001 or test_some_other_test'

This will run test cases with name test_001 and test_some_other_test deselecting the rest of the test cases.

Note: This will select any test case starting with test_001 or test_some_other_test. For example, if you have test case test_0012 it will also be selected.

Specifying tests / selecting tests

Pytest supports several ways to run and select tests from the command-line.

Run tests in a module


Run tests in a directory

pytest testing/

Run tests by keyword expressions

pytest -k "MyClass and not method"

This will run tests which contain names that match the given string expression, which can include Python operators that use filenames, class names and function names as variables. The example above will run TestMyClass.test_something but not TestMyClass.test_method_simple.

Run tests by node ids

Each collected test is assigned a unique nodeid which consist of the module filename followed by specifiers like class names, function names and parameters from parametrization, separated by :: characters.

To run a specific test within a module:


Another example specifying a test method in the command line:


Run tests by marker expressions

pytest -m slow

Will run all tests which are decorated with the @pytest.mark.slow decorator.

For more information see marks.

Run tests from packages

pytest --pyargs pkg.testing

This will import pkg.testing and use its filesystem location to find and run tests from.


My answer provides a ways to run a subset of test in different scenarios.

Run all tests in a project


Run tests in a Single Directory

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

pytest tests/my-directory

Run tests in a Single Test File/Module

To run a file full of tests, list the file with the relative path as a parameter to pytest:

pytest tests/my-directory/

Run a Single Test Function

To run a single test function, add :: and the test function name:

pytest -v tests/my-directory/

-v is used so you can see which function was run.

Run a Single Test Class

To run just a class, do like we did with functions and add ::, then the class name to the file parameter:

pytest -v tests/my-directory/

Run a Single Test Method of a Test Class

If you don't want to run all of a test class, just one method, just addanother :: and the method name:

pytest -v tests/my-directory/

Run a Set of Tests Based on Test Name

The -k option enables you to pass in an expression to run tests that havecertain names specified by the expression as a substring of the test name.It is possible to use and, or, and not to create complex expressions.

For example, to run all of the functions that have _raises in their name:

pytest -v -k _raises