0
0
PyTesttesting~10 mins

Testpaths configuration in PyTest - Test Execution Trace

Choose your learning style9 modes available
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.

Test Code - pytest
PyTest
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.
Execution Trace - 5 Steps
StepActionSystem StateAssertionResult
1Pytest starts test discoveryPytest reads pytest.ini and finds testpaths = tests-PASS
2Pytest searches only inside the 'tests' directory for test filesOnly test_included.py is found and collected-PASS
3Pytest runs test_included_path()Test function executes and returns Trueassert True passesPASS
4Pytest ignores test_excluded.py outside 'tests' directorytest_excluded_path() is not collected or runNo assertion for excluded test as it is not runPASS
5Pytest finishes test run and reports resultsReport shows 1 test collected and passedTest count matches expected collected testsPASS
Failure Scenario
Failing Condition: pytest.ini missing or testpaths not set correctly, causing tests outside 'tests' directory to run
Execution Trace Quiz - 3 Questions
Test your understanding
What does the 'testpaths' setting in pytest.ini control?
AThe timeout for each test
BThe order in which tests run
CWhich directories pytest searches for tests
DThe test report format
Key Result
Using 'testpaths' in pytest.ini helps focus test runs on specific directories, improving test speed and organization by excluding unrelated tests.