- Run tests in a module
pytest test_mod.py
- 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:
pytest test_mod.py::test_func
Another example specifying a test method in the command line:
pytest test_mod.py::TestClass::test_method
- Run tests with print statements
pytest -s test_mod.py::TestClass::test_method
- Run tests with breakpoints
pytest -pdb test_mod.py::TestClass::test_method
This will allow you to step through the code with pdb.
- 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.