Skip to content

Test Organization and Best Practices

Guidelines for writing maintainable and effective tests.

Test Organization

Structure tests logically.

# Project structure:
# project/
#   src/
#     my_module.py
#   tests/
#     test_my_module.py
#     conftest.py (shared fixtures)

import pytest

def function_to_test(x):
    return x * 2

class TestMyModule:
    def test_double(self):
        assert function_to_test(5) == 10

    def test_zero(self):
        assert function_to_test(0) == 0

# Run: pytest tests/ -v
print("Tests organized by module")
Tests organized by module

Naming Conventions and Best Practices

Follow testing best practices.

import pytest

# Good test names describe what is being tested
def test_add_positive_numbers():
    assert 2 + 3 == 5

def test_divide_by_zero_raises_error():
    with pytest.raises(ZeroDivisionError):
        1 / 0

def test_string_length():
    assert len("hello") == 5

# Arrange-Act-Assert pattern
def test_user_registration():
    # Arrange
    username = "alice"
    password = "secure"

    # Act
    registered = register_user(username, password)

    # Assert
    assert registered is True

def register_user(username, password):
    return True

print("Best practices applied")
Best practices applied