CodingNic

Testing and Best Practices

Test Organization

Testing and Best Practices 32 min read

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

text
project/
    app.py
    test_app.py

This works for small projects.

Better Structure for Bigger Projects

text
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:

text
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

python
# math_tools.py
def add(a, b):
    return a + b

Matching Test File

python
# 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:

python
def test_add(self):

Less clear:

python
def test_everything(self):
python
class TestMath(unittest.TestCase):

Use separate classes for different features when needed.

Code Along

Create:

text
tests/test_square.py

Write tests for a square() function.

Mini Challenge

Create this structure:

text
shop/
    products.py
tests/
    test_products.py

Tasks:

  • Add function get_total(price, qty)
  • Write two tests

Expected output:

text
..
OK

Real World Use Case

Professional projects use organized test folders for APIs, web apps, data systems, and automation tools.

Quiz

  1. Why keep tests in a separate folder?
  2. Why should files start with test_?
  3. Why use one test file per feature?
  4. 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.