0
0
PyTesttesting~5 mins

Testpaths configuration in PyTest

Choose your learning style9 modes available
Introduction

Testpaths configuration helps pytest find your test files easily. It tells pytest where to look for tests.

You have test files in folders other than the default 'tests' folder.
You want to organize tests in multiple directories and still run them all.
You want to avoid pytest searching unnecessary folders for tests.
You want to speed up test discovery by limiting search paths.
You want to keep your project clean by specifying only test folders.
Syntax
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.

Examples
Look for tests only in the 'tests' folder.
PyTest
[pytest]
testpaths = tests
Look for tests in both 'tests' and 'integration_tests' folders.
PyTest
[pytest]
testpaths = tests integration_tests
Look for tests inside 'src/tests' folder.
PyTest
[pytest]
testpaths = src/tests
Sample Program

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
[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 ==============================
OutputSuccess
Important Notes

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.

Summary

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.