Test Overview
This test checks that pytest only runs tests located in the specified testpaths directory. It verifies that tests outside this directory are not executed.
This test checks that pytest only runs tests located in the specified testpaths directory. It verifies that tests outside this directory are not executed.
import pytest def test_included_path(): assert True # pytest.ini content: # [pytest] # testpaths = tests # Directory structure: # /project # /tests # test_included.py (contains test_included_path) # /other # test_excluded.py (contains test_excluded_path) # Run command: # pytest # Expected: Only test_included_path runs and passes, test_excluded_path is not collected.
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Pytest starts test discovery | Pytest reads pytest.ini and finds testpaths = tests | — | PASS |
| 2 | Pytest searches only inside the 'tests' directory for test files | Only test_included.py is found and collected | — | PASS |
| 3 | Pytest runs test_included_path() | Test function executes and returns True | assert True passes | PASS |
| 4 | Pytest ignores test_excluded.py outside 'tests' directory | test_excluded_path() is not collected or run | No assertion for excluded test as it is not run | PASS |
| 5 | Pytest finishes test run and reports results | Report shows 1 test collected and passed | Test count matches expected collected tests | PASS |
testpaths setting in a pytest.ini file?testpathstestpaths setting tells pytest where to find test files by listing directories.testpaths.testpaths in pytest.ini to look in folders tests and integration?pytest.ini, testpaths lists folders separated by spaces without quotes or commas.pytest.ini content:[pytest] testpaths = tests integration
project/
tests/
test_alpha.py
integration/
test_beta.py
docs/
test_docs.pytestpaths setting includes tests and integration folders only.test_alpha.py in tests and test_beta.py in integration will be found. test_docs.py is in docs, which is not listed.testpaths = tests in pytest.ini but pytest still discovers tests in other folders. What is the most likely cause?tox.ini or setup.cfg that may override pytest.ini.tests run, likely another config file sets different testpaths or disables it.unit_tests and api_tests folders, but exclude api_tests/slow. How can you configure pytest.ini using testpaths and other settings to achieve this?testpaths = unit_tests api_tests to include both folders.norecursedirs = api_tests/slow to prevent pytest from searching inside that subfolder.ignore which is not a valid pytest.ini setting.Pass the test and this pattern moves unknown