Bird
Raised Fist0
PyTesttesting~20 mins

Testpaths configuration in PyTest - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of the testpaths setting in a pytest.ini file?
easy
A. To configure the test report format
B. To specify which folders pytest should look in for test files
C. To set the timeout for each test
D. To define the order in which tests run

Solution

  1. Step 1: Understand the role of testpaths

    The testpaths setting tells pytest where to find test files by listing directories.
  2. Step 2: Compare with other options

    Other options like test order, timeout, or report format are configured differently, not with testpaths.
  3. Final Answer:

    To specify which folders pytest should look in for test files -> Option B
  4. Quick Check:

    testpaths = test folders [OK]
Hint: testpaths lists folders to find tests [OK]
Common Mistakes:
  • Confusing testpaths with test order settings
  • Thinking testpaths sets test timeouts
  • Assuming testpaths changes report style
2. Which of the following is the correct syntax to set testpaths in pytest.ini to look in folders tests and integration?
easy
A. [pytest] testpaths = tests integration
B. [pytest] testpaths = ['tests', 'integration']
C. [pytest] testpaths = tests,integration
D. [pytest] testpaths = "tests integration"

Solution

  1. Step 1: Check pytest.ini syntax for testpaths

    In pytest.ini, testpaths lists folders separated by spaces without quotes or commas.
  2. Step 2: Validate each option

    [pytest] testpaths = tests integration uses space-separated folder names correctly. Options A and D use quotes or brackets incorrectly. [pytest] testpaths = tests,integration uses commas which pytest does not accept here.
  3. Final Answer:

    [pytest] testpaths = tests integration -> Option A
  4. Quick Check:

    Space-separated folder names, no quotes [OK]
Hint: Use space-separated folder names without quotes [OK]
Common Mistakes:
  • Using commas instead of spaces
  • Adding quotes or brackets around folder names
  • Placing testpaths outside [pytest] section
3. Given this pytest.ini content:
[pytest]
testpaths = tests integration

And this folder structure:
project/
  tests/
    test_alpha.py
  integration/
    test_beta.py
  docs/
    test_docs.py

Which test files will pytest discover and run?
medium
A. Only test_docs.py
B. test_alpha.py, test_beta.py, and test_docs.py
C. Only test_alpha.py
D. test_alpha.py and test_beta.py only

Solution

  1. Step 1: Identify folders listed in testpaths

    The testpaths setting includes tests and integration folders only.
  2. Step 2: Match test files in those folders

    Files test_alpha.py in tests and test_beta.py in integration will be found. test_docs.py is in docs, which is not listed.
  3. Final Answer:

    test_alpha.py and test_beta.py only -> Option D
  4. Quick Check:

    Only testpaths folders' tests run [OK]
Hint: Only tests in testpaths folders run [OK]
Common Mistakes:
  • Assuming all test files in project run
  • Including tests from folders not in testpaths
  • Confusing folder names with test file names
4. You set testpaths = tests in pytest.ini but pytest still discovers tests in other folders. What is the most likely cause?
medium
A. There is another config file overriding testpaths
B. You forgot to restart pytest after editing pytest.ini
C. testpaths only filters test names, not folders
D. pytest ignores testpaths by default

Solution

  1. Step 1: Understand pytest config precedence

    pytest can read multiple config files like tox.ini or setup.cfg that may override pytest.ini.
  2. Step 2: Check for conflicting configs

    If tests outside tests run, likely another config file sets different testpaths or disables it.
  3. Final Answer:

    There is another config file overriding testpaths -> Option A
  4. Quick Check:

    Multiple configs can override testpaths [OK]
Hint: Check for multiple config files overriding settings [OK]
Common Mistakes:
  • Assuming pytest.ini changes apply without reload
  • Thinking testpaths filters test names only
  • Believing pytest ignores testpaths by default
5. You want pytest to only run tests in 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?
hard
A. [pytest] testpaths = unit_tests norecursedirs = api_tests/slow
B. [pytest] testpaths = unit_tests api_tests/slow norecursedirs = api_tests
C. [pytest] testpaths = unit_tests api_tests norecursedirs = api_tests/slow
D. [pytest] testpaths = unit_tests api_tests ignore = api_tests/slow

Solution

  1. Step 1: Use testpaths to include desired folders

    Set testpaths = unit_tests api_tests to include both folders.
  2. Step 2: Exclude the slow subfolder with norecursedirs

    Use norecursedirs = api_tests/slow to prevent pytest from searching inside that subfolder.
  3. Step 3: Verify other options are incorrect

    [pytest] testpaths = unit_tests api_tests/slow norecursedirs = api_tests includes the slow folder in testpaths, which is wrong. [pytest] testpaths = unit_tests norecursedirs = api_tests/slow excludes api_tests entirely. [pytest] testpaths = unit_tests api_tests ignore = api_tests/slow uses ignore which is not a valid pytest.ini setting.
  4. Final Answer:

    [pytest] testpaths = unit_tests api_tests norecursedirs = api_tests/slow -> Option C
  5. Quick Check:

    Include folders with testpaths, exclude subfolders with norecursedirs [OK]
Hint: Use testpaths to include, norecursedirs to exclude subfolders [OK]
Common Mistakes:
  • Including excluded folders in testpaths
  • Using invalid config keys like ignore
  • Excluding whole folders instead of subfolders
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard