What is the purpose of the testpaths setting in pytest?
The testpaths setting tells pytest which directories or files to look into when searching for tests to run. It helps focus test discovery on specific locations.
Click to reveal answer
beginner
How do you specify multiple directories in testpaths?
You list them as a sequence in the pytest.ini file under testpaths, like:
[pytest]
testpaths = tests integration_tests
Click to reveal answer
beginner
Where do you configure testpaths in a pytest project?
You configure testpaths in the pytest.ini file or other pytest config files like tox.ini or setup.cfg under the [pytest] section.
Click to reveal answer
intermediate
What happens if you do not set testpaths in pytest?
Pytest will search for tests starting from the current directory and all its subdirectories by default, which might include unwanted files or slow down test discovery.
Click to reveal answer
intermediate
Can testpaths include files as well as directories?
Yes, testpaths can include specific test files or directories. Pytest will look for tests only in those specified paths.
Click to reveal answer
What is the main role of testpaths in pytest?
ATo specify where pytest looks for tests
BTo define test execution order
CTo set test timeout values
DTo configure test output format
✗ Incorrect
testpaths tells pytest which directories or files to search for tests.
How do you add multiple directories to testpaths in pytest.ini?
AUse a single string with spaces
BSeparate them with commas
CList each directory on a new line under <code>testpaths</code>
DUse a JSON array
✗ Incorrect
You use a single string with spaces to list directories under testpaths in the config file.
If testpaths is not set, where does pytest look for tests?
AOnly in the root directory
BIn the user's home directory
CIn the current directory and all subdirectories
DIn the system's temp folder
✗ Incorrect
By default, pytest searches the current directory and all subdirectories for tests.
Which file can you NOT use to configure testpaths?
A<code>pytest.ini</code>
B<code>tox.ini</code>
C<code>setup.cfg</code>
D<code>requirements.txt</code>
✗ Incorrect
requirements.txt is for dependencies, not pytest configuration.
Can testpaths include individual test files?
ANo, only directories are allowed
BYes, it can include files and directories
COnly files with .pyc extension
DOnly files in the root directory
✗ Incorrect
testpaths can include both files and directories to specify test locations.
Explain how to configure pytest to look for tests only in specific folders using testpaths.
Think about the config file and the syntax for listing paths.
You got /4 concepts.
What are the benefits of using testpaths in a pytest project?
Consider how limiting search paths helps test runs.
You got /4 concepts.
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
Step 1: Understand the role of testpaths
The testpaths setting tells pytest where to find test files by listing directories.
Step 2: Compare with other options
Other options like test order, timeout, or report format are configured differently, not with testpaths.
Final Answer:
To specify which folders pytest should look in for test files -> Option B
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
Step 1: Check pytest.ini syntax for testpaths
In pytest.ini, testpaths lists folders separated by spaces without quotes or commas.
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.
Final Answer:
[pytest]
testpaths = tests integration -> Option A
Quick Check:
Space-separated folder names, no quotes [OK]
Hint: Use space-separated folder names without quotes [OK]
The testpaths setting includes tests and integration folders only.
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.
Final Answer:
test_alpha.py and test_beta.py only -> Option D
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
Step 1: Understand pytest config precedence
pytest can read multiple config files like tox.ini or setup.cfg that may override pytest.ini.
Step 2: Check for conflicting configs
If tests outside tests run, likely another config file sets different testpaths or disables it.
Final Answer:
There is another config file overriding testpaths -> Option A
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
Step 1: Use testpaths to include desired folders
Set testpaths = unit_tests api_tests to include both folders.
Step 2: Exclude the slow subfolder with norecursedirs
Use norecursedirs = api_tests/slow to prevent pytest from searching inside that subfolder.
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.