Testing Best Practices
Test-driven development, mocking patterns, and test organization.
Overview
Master testing best practices.
Test-Driven Development
# Step 1: Write failing test
def test_add_positive_numbers():
assert add(2, 3) == 5
# Step 2: Write minimal code to pass
def add(a, b):
return a + b
# Step 3: Refactor if needed
# Step 4: Repeat
Mocking Patterns
from unittest.mock import Mock, patch, MagicMock
from datetime import datetime
# Mock external service
@patch('myapp.services.external_api')
def test_api_call(mock_api):
mock_api.get.return_value.json.return_value = {"status": "ok"}
result = fetch_data()
assert result["status"] == "ok"
mock_api.get.assert_called_once()
# Mock time-dependent code
@patch('myapp.utils.datetime')
def test_timestamp(mock_dt):
mock_dt.now.return_value = datetime(2024, 1, 1)
timestamp = get_timestamp()
assert timestamp == "2024-01-01"
Test Organization
# tests/test_user_service.py
import pytest
from myapp.services import UserService
from myapp.models import User
class TestUserService:
@pytest.fixture
def service(self):
return UserService()
@pytest.fixture
def sample_user(self):
return User(name="Alice", email="alice@test.com")
def test_create_user(self, service, sample_user):
result = service.create(sample_user)
assert result.id is not None
def test_find_user(self, service, sample_user):
service.create(sample_user)
found = service.find_by_email("alice@test.com")
assert found.name == "Alice"
def test_user_not_found(self, service):
with pytest.raises(UserNotFoundError):
service.find_by_email("unknown@test.com")
Practice
Write tests for a complex service with dependencies.