Organizing test files helps keep your tests easy to find and run. It makes your work neat and saves time.
Project structure for tests in PyTest
Introduction
Syntax
PyTest
project_root/ ├── src/ │ └── your_code.py ├── tests/ │ ├── __init__.py │ ├── test_feature1.py │ └── test_feature2.py ├── pytest.ini └── requirements.txt
The tests/ folder holds all test files.
Test files start with test_ so pytest finds them automatically.
Examples
PyTest
tests/ ├── test_login.py ├── test_signup.py └── test_logout.py
PyTest
tests/
├── unit/
│ ├── test_utils.py
│ └── test_models.py
└── integration/
└── test_api.pyPyTest
pytest.ini
[pytest]
minversion = 6.0
addopts = -ra -q
testpaths = testsSample Program
This example shows a simple function and two tests in a separate file inside tests/. Pytest will find and run these tests.
PyTest
def add(a, b): return a + b # tests/test_add.py import pytest from src.your_code import add def test_add_positive(): assert add(2, 3) == 5 def test_add_negative(): assert add(-1, -1) == -2
Important Notes
Always name test files starting with test_ and test functions the same way.
Keep test code separate from application code to avoid confusion.
Use folders inside tests/ to organize large projects.
Summary
Organize tests in a tests/ folder for clarity.
Name test files and functions starting with test_ for pytest to find them.
Use subfolders and config files to manage bigger projects easily.
Practice
1. Why should you place your test files inside a
tests/ folder in a pytest project?easy
Solution
Step 1: Understand project organization
Keeping tests in a separatetests/folder helps keep the project clean and organized.Step 2: Recognize pytest behavior
pytest can find tests anywhere but organizing them intests/is a best practice for clarity and maintenance.Final Answer:
To keep test code separate and organized from application code -> Option DQuick 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
Solution
Step 1: Recall pytest naming rules
pytest discovers test files that start withtest_or end with_test.py.Step 2: Check each option
test_example.pystarts withtest_, so pytest will find it automatically.Final Answer:
test_example.py -> Option AQuick Check:
File starts with test_ = discovered [OK]
Hint: Name test files starting with test_ for pytest discovery [OK]
Common Mistakes:
- Naming files without test_ prefix
- Using uppercase letters at start
- Assuming pytest finds any .py file
3. Given this project structure:
project/
app.py
tests/
test_math.py
utils/
test_utils.py
Which files will pytest run by default when you run pytest in the project root?medium
Solution
Step 1: Understand pytest recursive discovery
pytest looks inside thetests/folder and all its subfolders for files starting withtest_.Step 2: Identify test files
Bothtest_math.pyandtest_utils.pymatch naming rules and are insidetests/or its subfolder.Final Answer:
Both tests/test_math.py and tests/utils/test_utils.py -> Option CQuick Check:
pytest finds tests recursively in tests/ [OK]
Hint: pytest finds test files recursively inside tests/ folder [OK]
Common Mistakes:
- Thinking pytest ignores subfolders
- Assuming only root tests/ files run
- Believing subfolders need special config
4. You have this test file named
test_sample.py inside tests/ folder:
def sample_test():
assert 1 + 1 == 2
When you run pytest, no tests are found. Why?medium
Solution
Step 1: Check function naming convention
pytest discovers test functions only if their names start withtest_.Step 2: Analyze given function name
The function is namedsample_test, which does not start withtest_, so pytest ignores it.Final Answer:
Function name does not start with test_ -> Option AQuick Check:
Test functions must start with test_ [OK]
Hint: Name test functions starting with test_ to be discovered [OK]
Common Mistakes:
- Assuming file name alone is enough
- Thinking pytest needs imports in test files
- Believing tests run only in root folder
5. In a large pytest project, how can you organize tests to keep them manageable and easy to run selectively?
hard
Solution
Step 1: Understand project scaling needs
Large projects benefit from organizing tests in subfolders to separate features or modules.Step 2: Use configuration files
Adding apytest.inifile helps customize test discovery and running specific groups of tests easily.Final Answer:
Use subfolders inside tests/ and add pytest.ini to configure test discovery -> Option BQuick Check:
Subfolders + pytest.ini = manageable large projects [OK]
Hint: Organize tests in subfolders and use pytest.ini config [OK]
Common Mistakes:
- Putting all tests in one file causing confusion
- Ignoring naming conventions for test functions
- Avoiding config files limits flexibility
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
