0
0
PyTesttesting~20 mins

Testpaths configuration in PyTest - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Testpaths Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of pytest testpaths effect
Given the pytest configuration below, what tests will pytest discover and run?
PyTest
[pytest]
testpaths = tests/integration

# Directory structure:
# tests/
# ├── integration/
# │   └── test_api.py
# └── unit/
#     └── test_utils.py

# Both test_api.py and test_utils.py contain valid test functions.
ANo tests will run because testpaths is misconfigured.
BBoth tests/integration/test_api.py and tests/unit/test_utils.py will run.
COnly tests in tests/integration/test_api.py will run.
DOnly tests in tests/unit/test_utils.py will run.
Attempts:
2 left
💡 Hint
The testpaths setting limits pytest to look only inside specified directories.
assertion
intermediate
2:00remaining
Assertion to verify testpaths filtering
You want to write a pytest test that verifies pytest only collects tests from the 'tests/functional' directory as set in testpaths. Which assertion correctly checks this behavior?
PyTest
import pytest

collected = pytest.main(['--collect-only'])
# Assume collected_tests is a list of test node ids collected by pytest
Aassert 'tests/functional' not in collected_tests
Bassert any('tests/functional' in test for test in collected_tests)
Cassert all('tests/unit' in test for test in collected_tests)
Dassert all('tests/functional' in test for test in collected_tests)
Attempts:
2 left
💡 Hint
To confirm filtering, all collected tests should be inside the specified directory.
🔧 Debug
advanced
2:00remaining
Why are tests outside testpaths not running?
You configured pytest with testpaths = ['tests/api'] but tests in 'tests/api' and 'tests/db' exist. Only tests in 'tests/api' run. You want tests in both directories to run. What is the cause?
PyTest
[pytest]
testpaths = tests/api
Atestpaths restricts pytest to only the specified directory; 'tests/db' is excluded because it is not listed.
Btestpaths only accepts one directory; multiple directories must be comma-separated in one string.
Cpytest ignores testpaths if multiple test directories exist.
Dtestpaths must be set in pytest.ini, not in pyproject.toml.
Attempts:
2 left
💡 Hint
Check how testpaths limits test discovery.
framework
advanced
2:00remaining
Configuring multiple testpaths in pytest.ini
How should you correctly configure pytest.ini to include both 'tests/unit' and 'tests/integration' directories in testpaths?
A
[pytest]
testpaths = tests/unit, tests/integration
B
[pytest]
testpaths =
    tests/unit
    tests/integration
C
[pytest]
testpaths = tests/unit tests/integration
D
[pytest]
testpaths = ['tests/unit', 'tests/integration']
Attempts:
2 left
💡 Hint
Check pytest.ini syntax for multi-line values.
🧠 Conceptual
expert
2:00remaining
Effect of testpaths on pytest test collection order
Which statement about the effect of the testpaths configuration on pytest test collection order is true?
Atestpaths only filters directories but does not guarantee the order in which tests are collected or run.
Btestpaths causes pytest to run tests in alphabetical order regardless of directory order.
Ctestpaths enforces tests to run in the order directories are listed.
Dtestpaths disables test collection order and runs tests randomly.
Attempts:
2 left
💡 Hint
Consider what testpaths controls versus test order.