Testpaths configuration helps pytest find your test files easily. It tells pytest where to look for tests.
Testpaths configuration in PyTest
[pytest] testpaths = folder1 folder2
This goes in a pytest.ini file in your project root.
List folders separated by spaces where your test files are located.
[pytest] testpaths = tests
[pytest] testpaths = tests integration_tests
[pytest] testpaths = src/tests
This example shows a pytest.ini file with testpaths set to 'tests'. Pytest will only look inside the 'tests' folder for test files.
The sample test file has two simple tests that both pass.
Running pytest will find and run these tests successfully.
[pytest] testpaths = tests # Folder structure: # project_root/ # ├── pytest.ini # ├── tests/ # │ └── test_sample.py # Content of tests/test_sample.py: import pytest def test_add(): assert 1 + 1 == 2 def test_subtract(): assert 5 - 3 == 2 # Run command: # pytest # Expected output: # ============================= test session starts ============================= # collected 2 items # # tests/test_sample.py .. [100%] # # ============================== 2 passed in 0.01s ==============================
Make sure the folders listed in testpaths actually exist, or pytest will find no tests.
Test files should be named starting with test_ or ending with _test.py for pytest to recognize them.
You can combine testpaths with other pytest.ini settings like python_files to customize test discovery.
Testpaths configuration tells pytest where to find your test files.
It helps organize tests and speeds up test discovery.
Set it in pytest.ini by listing folders with tests.