Targeting Specific Tests
As your project grows, running the entire test suite every time can be slow and unnecessary. pytest gives you the flexibility to target specific tests.
- If you want to run only the tests inside a particular directory (e.g.,
tests/api/), use:
shell
pytest tests/api/- If you want to run only the tests in a single test file/module, use:
shell
pytest tests/api/test_auth.py- To target a single test function or a class within a file, specify the function or class name after
:::
shell
# Targeting a function
pytest tests/api/test_auth.py::test_login_success
# Targeting a class
pytest tests/api/test_auth.py::TestClass- To target a single test method within a class, specify the class name and method name after
:::
shell
pytest tests/api/test_auth.py::TestClass::test_bad_id- You can even mix and match directories, files, classes, methods and test functions in a single command:
shell
pytest tests/api/ tests/db/test_db.py tests/api/test_auth.py::test_login_success- You can also target specific tests by using the
-kexpression filter or by selecting tests via markers. Both approaches are explained below.
