Test Organization
Test Organization
As projects grow, tests should be organized clearly.
Good structure makes tests easier to find, run, and maintain.
Why Test Organization Matters
Organized tests help you:
- find test files quickly
- separate app code from test code
- manage larger projects
- work with teams
- scale projects cleanly
Simple Project Structure
project/
app.py
test_app.py
This works for small projects.
Better Structure for Bigger Projects
project/
app/
math_tools.py
users.py
tests/
test_math_tools.py
test_users.py
Keep tests in a separate tests folder.
Naming Rules
Common style:
- Test files start with
test_ - Test methods start with
test_
Examples:
test_math.py
test_users.py
Why test_ Names Matter
Testing tools can automatically discover and run files that follow naming rules.
Example App File
# math_tools.py
def add(a, b):
return a + b
Matching Test File
# test_math_tools.py
import unittest
from math_tools import add
class TestMath(unittest.TestCase):
def test_add(self):
self.assertEqual(add(2, 3), 5)
if __name__ == "__main__":
unittest.main()
One Test File Per Feature
Examples:
- user features →
test_users.py - reports →
test_reports.py - payments →
test_payments.py
Keep Tests Focused
Each test should check one clear behavior.
Good:
def test_add(self):
Less clear:
def test_everything(self):
Group Related Tests
class TestMath(unittest.TestCase):
Use separate classes for different features when needed.
Code Along
Create:
tests/test_square.py
Write tests for a square() function.
Mini Challenge
Create this structure:
shop/
products.py
tests/
test_products.py
Tasks:
- Add function
get_total(price, qty) - Write two tests
Expected output:
..
OK
Real World Use Case
Professional projects use organized test folders for APIs, web apps, data systems, and automation tools.
Quiz
- Why keep tests in a separate folder?
- Why should files start with
test_? - Why use one test file per feature?
- Why keep tests focused?
Assignment
Reorganize one of your old projects by moving tests into a tests folder.
Summary
You learned how to organize tests using clear folders, names, and focused test files.