Why is it important to organize test files in a separate directory like tests/?
Keeping test files in a separate tests/ folder helps keep the project clean and makes it easy to find and run tests without mixing them with application code.
Click to reveal answer
beginner
What naming convention should test files and test functions follow in pytest?
Test files should start with test_ or end with _test.py. Test functions inside should start with test_. This helps pytest automatically find and run them.
Click to reveal answer
intermediate
What is the purpose of a conftest.py file in a pytest project?
conftest.py is used to share fixtures and setup code across multiple test files in the same directory or subdirectories, avoiding repetition.
Click to reveal answer
intermediate
How can you structure tests for different parts of your application in pytest?
You can create subfolders inside tests/ for different modules or features, like tests/api/ or tests/ui/, to keep tests organized and easy to maintain.
Click to reveal answer
intermediate
What file should you include to make your test directory a Python package and why?
Including an empty __init__.py file in the tests/ folder makes it a Python package, which can help with imports and test discovery in some cases.
Click to reveal answer
Where should you place your test files in a pytest project?
AIn a separate <code>tests/</code> directory
BMixed with application code
CIn the root directory only
DInside <code>__pycache__</code> folder
✗ Incorrect
Placing tests in a separate tests/ directory keeps the project organized and makes it easier to run tests.
What prefix should test functions have in pytest?
Acheck_
Btest_
Crun_
Dfunc_
✗ Incorrect
Test functions should start with test_ so pytest can automatically find and run them.
What is the role of conftest.py in pytest?
ATo configure the Python interpreter
BTo store test data files
CTo share fixtures and setup code across tests
DTo list all test files manually
✗ Incorrect
conftest.py allows sharing fixtures and setup code without importing them explicitly.
How can you organize tests for different features in pytest?
AUse subfolders inside <code>tests/</code> for each feature
BPut all tests in one file
CName test files randomly
DPlace tests in the <code>src/</code> folder
✗ Incorrect
Using subfolders helps keep tests organized by feature or module.
Why add an empty __init__.py file in the tests/ folder?
ATo store test results
BTo run tests faster
CTo hide tests from pytest
DTo make the folder a Python package for imports
✗ Incorrect
An __init__.py file makes the folder a package, helping with imports and test discovery.
Describe a good project structure for organizing pytest tests in a medium-sized project.
Think about how to keep tests easy to find, run, and maintain.
You got /5 concepts.
Explain the purpose and benefits of using a conftest.py file in pytest projects.
Consider how to reuse setup code efficiently.
You got /4 concepts.
Practice
(1/5)
1. Why should you place your test files inside a tests/ folder in a pytest project?
easy
A. To avoid writing test functions starting with test_
B. To make the tests run faster automatically
C. Because pytest only runs tests in the root folder
D. To keep test code separate and organized from application code
Solution
Step 1: Understand project organization
Keeping tests in a separate tests/ folder helps keep the project clean and organized.
Step 2: Recognize pytest behavior
pytest can find tests anywhere but organizing them in tests/ is a best practice for clarity and maintenance.
Final Answer:
To keep test code separate and organized from application code -> Option D
Quick Check:
Organized tests = tests/ folder [OK]
Hint: Put tests in a separate folder named tests for clarity [OK]
Common Mistakes:
Thinking pytest only runs tests in root folder
Believing tests run faster in tests/ folder
Ignoring naming conventions for test functions
2. Which of the following is the correct way to name a test file so pytest can discover it automatically?
easy
A. test_example.py
B. mytests.py
C. example.py
D. TestExample.py
Solution
Step 1: Recall pytest naming rules
pytest discovers test files that start with test_ or end with _test.py.
Step 2: Check each option
test_example.py starts with test_, so pytest will find it automatically.
Final Answer:
test_example.py -> Option A
Quick Check:
File starts with test_ = discovered [OK]
Hint: Name test files starting with test_ for pytest discovery [OK]