Where do the Python unit tests go?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In Python projects, unit tests usually go in a top-level tests/ directory that mirrors the structure of the application code. That is not the only valid layout, but it is the most common and easiest pattern for teams, tools, and CI systems to understand.
The Common Project Layout
A typical project looks like this:
This structure keeps production code and test code separate. It also makes it obvious which files are shipped as part of the application and which exist only for verification.
Mirroring the Package Structure
As the project grows, it helps to mirror the source tree inside tests/.
This makes navigation easier because a test for src/myapp/services/billing.py is predictably located near tests/services/test_billing.py.
Why a Separate tests/ Directory Is Popular
The top-level tests/ layout has a few practical advantages:
- test code does not get mixed with runtime package files
- packaging rules stay simpler
- test discovery tools such as
pytestwork naturally - large teams can scan the project structure quickly
With pytest, the common naming convention is:
- directories named
tests - files named
test_*.py - test functions named
test_*
Example:
In-Package Tests Are Also Possible
Some projects keep tests near the code they verify:
This can work, especially in small libraries, but it mixes production and test files. That tradeoff is sometimes acceptable, but many teams prefer the cleaner separation of a dedicated tests/ tree.
A Practical pytest Setup
With a pyproject.toml, you can make test discovery explicit:
Now pytest knows exactly where to look:
This is especially helpful once the repository contains scripts, notebooks, or examples that should not be treated as tests.
Keep Test Names Close to User Behavior
Directory placement answers where tests go physically, but naming answers whether they stay readable later. A useful pattern is to let the file mirror the module while the test functions describe behavior:
That combination gives you both kinds of clarity:
- file location tells you what module is under test
- function name tells you what behavior is expected
Common Pitfalls
The most common mistake is scattering tests randomly across the repository with no naming convention. Test tools can be configured to handle that, but the project becomes harder to navigate.
Another issue is importing application code incorrectly when using a src/ layout. If your package lives under src/, your environment or tool configuration must install or expose that package properly for tests.
A third pitfall is naming tests after implementation details that change frequently. It is better for the directory layout to mirror modules, but for the test cases themselves to reflect behavior.
Finally, do not confuse unit tests with integration tests or end-to-end tests. They can live under the same top-level tests/ directory, but keeping subdirectories or markers for different test types usually helps.
Summary
- The most common place for Python unit tests is a top-level
tests/directory. - Mirror the structure of the source tree as the project grows.
- Use standard file naming such as
test_*.pyfor easy discovery. - In-package tests are possible, but many teams prefer a separate test tree.
- Keep the layout predictable so humans and tools can find tests quickly.
Related reading
- Where does pip install its packages?
- Where does pip install its packages?
- Where is a complete example of logging.config.dictConfig?
- Where is the code for gradient descent?
- Where is mstest.exe located?
- Where is the Create Unit Tests selection?
- Where is the documentation for the values method of Enum?
- Where is the downloaded Keras dataset stored?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.