| name | django-tdd |
| description | Django testing strategies with pytest-django, TDD methodology, factory_boy, mocking, coverage, and testing Django REST Framework APIs. |
Django 测试驱动开发 (TDD)
使用 pytest、factory_boy 和 Django REST Framework 进行 Django 应用程序的测试驱动开发。
何时启用
- 编写新的 Django 应用程序时
- 实现 Django REST Framework API 时
- 测试 Django 模型(Models)、视图(Views)、序列化器(Serializers)时
- 配置 Django 项目的测试基础设施时
Django 的 TDD 工作流
红-绿-重构(Red-Green-Refactor)循环
def test_user_creation():
user = User.objects.create_user(email='test@example.com', password='testpass123')
assert user.email == 'test@example.com'
assert user.check_password('testpass123')
assert not user.is_staff
设置
pytest 配置
[pytest]
DJANGO_SETTINGS_MODULE = config.settings.test
testpaths = tests
python_files = test_*.py
python_classes = Test*
python_functions = test_*
addopts =
--reuse-db
--nomigrations
--cov=apps
--cov-report=html
--cov-report=term-missing
--strict-markers
markers =
slow: marks tests as slow
integration: marks tests as integration tests
测试设置
from .base import *
DEBUG = True
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': ':memory:',
}
}
class DisableMigrations:
def __contains__(self, item):
return True
def __getitem__(self, item):
return None
MIGRATION_MODULES = DisableMigrations()
PASSWORD_HASHERS = [
'django.contrib.auth.hashers.MD5PasswordHasher',
]
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
CELERY_TASK_ALWAYS_EAGER = True
CELERY_TASK_EAGER_PROPAGATES = True
conftest.py
import pytest
from django.utils import timezone
from django.contrib.auth import get_user_model
User = get_user_model()
@pytest.fixture(autouse=True)
def timezone_settings(settings):
"""确保时区一致。"""
settings.TIME_ZONE = 'UTC'
@pytest.fixture
def user(db):
"""创建测试用户。"""
return User.objects.create_user(
email='test@example.com',
password='testpass123',
username='testuser'
)
@pytest.fixture
def admin_user(db):
"""创建管理员用户。"""
return User.objects.create_superuser(
email='admin@example.com',
password='adminpass123',
username='admin'
)
@pytest.fixture
def authenticated_client(client, user):
"""返回已认证的客户端。"""
client.force_login(user)
return client
@pytest.fixture
def api_client():
"""返回 DRF API 客户端。"""
from rest_framework.test import APIClient
return APIClient()
@pytest.fixture
def ():
api_client.force_authenticate(user=user)
api_client
Factory Boy
Factory 设置
import factory
from factory import fuzzy
from datetime import datetime, timedelta
from django.contrib.auth import get_user_model
from apps.products.models import Product, Category
User = get_user_model()
class UserFactory(factory.django.DjangoModelFactory):
"""User 模型的 Factory。"""
class Meta:
model = User
email = factory.Sequence(lambda n: f"user{n}@example.com")
username = factory.Sequence(lambda n: f"user{n}")
password = factory.PostGenerationMethodCall('set_password', 'testpass123')
first_name = factory.Faker('first_name')
last_name = factory.Faker('last_name')
is_active = True
class CategoryFactory(factory.django.DjangoModelFactory):
"""Category 模型的 Factory。"""
class Meta:
model = Category
name = factory.Faker('word')
slug = factory.LazyAttribute(lambda obj: obj.name.lower())
description = factory.Faker('text')
class ProductFactory(factory.django.DjangoModelFactory):
"""Product 模型的 Factory。"""
class Meta:
model = Product
name = factory.Faker('sentence', nb_words=3)
slug = factory.LazyAttribute( obj: obj.name.lower().replace(, ))
description = factory.Faker()
price = fuzzy.FuzzyDecimal(, , )
stock = fuzzy.FuzzyInteger(, )
is_active =
category = factory.SubFactory(CategoryFactory)
created_by = factory.SubFactory(UserFactory)
():
create:
extracted:
tag extracted:
.tags.add(tag)
使用 Factory
import pytest
from tests.factories import ProductFactory, UserFactory
def test_product_creation():
"""测试使用 Factory 创建产品。"""
product = ProductFactory(price=100.00, stock=50)
assert product.price == 100.00
assert product.stock == 50
assert product.is_active is True
def test_product_with_tags():
"""测试带有标签的产品。"""
tags = [TagFactory(name='electronics'), TagFactory(name='new')]
product = ProductFactory(tags=tags)
assert product.tags.count() == 2
def test_multiple_products():
"""测试创建多个产品。"""
products = ProductFactory.create_batch(10)
assert len(products) == 10
模型测试
模型测试
import pytest
from django.core.exceptions import ValidationError
from tests.factories import UserFactory, ProductFactory
class TestUserModel:
"""测试 User 模型。"""
def test_create_user(self, db):
"""测试普通用户创建。"""
user = UserFactory(email='test@example.com')
assert user.email == 'test@example.com'
assert user.check_password('testpass123')
assert not user.is_staff
assert not user.is_superuser
def test_create_superuser(self, db):
"""测试超级用户创建。"""
user = UserFactory(
email='admin@example.com',
is_staff=True,
is_superuser=True
)
assert user.is_staff
assert user.is_superuser
def test_user_str(self, db):
"""测试用户的字符串表示。"""
user = UserFactory(email='test@example.com')
assert str(user) == 'test@example.com'
class TestProductModel:
"""测试 Product 模型。"""
def test_product_creation(self, db):
product = ProductFactory()
product.
product.is_active
product.created_at
():
product = ProductFactory(name=)
product.slug ==
():
product = ProductFactory(price=-)
pytest.raises(ValidationError):
product.full_clean()
():
ProductFactory.create_batch(, is_active=)
ProductFactory.create_batch(, is_active=)
active_count = Product.objects.active().count()
active_count ==
():
product = ProductFactory(stock=)
product.reduce_stock()
product.refresh_from_db()
product.stock ==
pytest.raises(ValueError):
product.reduce_stock()
视图测试
Django 视图测试
import pytest
from django.urls import reverse
from tests.factories import ProductFactory, UserFactory
class TestProductViews:
"""测试产品视图。"""
def test_product_list(self, client, db):
"""测试产品列表视图。"""
ProductFactory.create_batch(10)
response = client.get(reverse('products:list'))
assert response.status_code == 200
assert len(response.context['products']) == 10
def test_product_detail(self, client, db):
"""测试产品详情视图。"""
product = ProductFactory()
response = client.get(reverse('products:detail', kwargs={'slug': product.slug}))
assert response.status_code == 200
assert response.context['product'] == product
def test_product_create_requires_login(self, client, db):
"""测试创建产品需要登录。"""
response = client.get(reverse('products:create'))
assert response.status_code == 302
assert response.url.startswith('/accounts/login/')
def test_product_create_authenticated(self, authenticated_client, db):
"""测试作为已认证用户的产品创建。"""
response = authenticated_client.get(reverse())
response.status_code ==
():
data = {
: ,
: ,
: ,
: ,
: category.,
}
response = authenticated_client.post(reverse(), data)
response.status_code ==
Product.objects.(name=).exists()
DRF API 测试
序列化器测试
import pytest
from rest_framework.exceptions import ValidationError
from apps.products.serializers import ProductSerializer
from tests.factories import ProductFactory
class TestProductSerializer:
"""测试 ProductSerializer。"""
def test_serialize_product(self, db):
"""测试产品序列化。"""
product = ProductFactory()
serializer = ProductSerializer(product)
data = serializer.data
assert data['id'] == product.id
assert data['name'] == product.name
assert data['price'] == str(product.price)
def test_deserialize_product(self, db):
"""测试产品数据反序列化。"""
data = {
'name': 'Test Product',
'description': 'Test description',
'price': '99.99',
'stock': 10,
'category': 1,
}
serializer = ProductSerializer(data=data)
assert serializer.is_valid()
product = serializer.save()
assert product.name == 'Test Product'
assert float(product.price) == 99.99
def ():
data = {
: ,
: ,
: ,
}
serializer = ProductSerializer(data=data)
serializer.is_valid()
serializer.errors
():
data = {
: ,
: ,
: -,
}
serializer = ProductSerializer(data=data)
serializer.is_valid()
serializer.errors
API ViewSet 测试
import pytest
from rest_framework.test import APIClient
from rest_framework import status
from django.urls import reverse
from tests.factories import ProductFactory, UserFactory
class TestProductAPI:
"""测试 Product API 端点。"""
@pytest.fixture
def api_client(self):
"""返回 API 客户端。"""
return APIClient()
def test_list_products(self, api_client, db):
"""测试产品列表。"""
ProductFactory.create_batch(10)
url = reverse('api:product-list')
response = api_client.get(url)
assert response.status_code == status.HTTP_200_OK
assert response.data['count'] == 10
def test_retrieve_product(self, api_client, db):
"""测试获取产品。"""
product = ProductFactory()
url = reverse('api:product-detail', kwargs={'pk': product.id})
response = api_client.get(url)
assert response.status_code == status.HTTP_200_OK
assert response.data['id'] == product.id
def test_create_product_unauthorized(self, api_client, db):
"""测试未经身份验证的产品创建。"""
url = reverse()
data = {: , : }
response = api_client.post(url, data)
response.status_code == status.HTTP_401_UNAUTHORIZED
():
url = reverse()
data = {
: ,
: ,
: ,
: ,
}
response = authenticated_api_client.post(url, data)
response.status_code == status.HTTP_201_CREATED
response.data[] ==
():
product = ProductFactory(created_by=authenticated_api_client.user)
url = reverse(, kwargs={: product.})
data = {: }
response = authenticated_api_client.patch(url, data)
response.status_code == status.HTTP_200_OK
response.data[] ==
():
product = ProductFactory(created_by=authenticated_api_client.user)
url = reverse(, kwargs={: product.})
response = authenticated_api_client.delete(url)
response.status_code == status.HTTP_204_NO_CONTENT
():
ProductFactory(price=)
ProductFactory(price=)
url = reverse()
response = api_client.get(url, {: })
response.status_code == status.HTTP_200_OK
response.data[] ==
():
ProductFactory(name=)
ProductFactory(name=)
url = reverse()
response = api_client.get(url, {: })
response.status_code == status.HTTP_200_OK
response.data[] ==
Mocking 与 Patching
外部服务 Mock
from unittest.mock import patch, Mock
import pytest
class TestPaymentView:
"""使用 Mock 的支付网关测试支付视图。"""
@patch('apps.payments.services.stripe')
def test_successful_payment(self, mock_stripe, client, user, product):
"""使用 Mock 的 Stripe 测试成功的支付。"""
mock_stripe.Charge.create.return_value = {
'id': 'ch_123',
'status': 'succeeded',
'amount': 9999,
}
client.force_login(user)
response = client.post(reverse('payments:process'), {
'product_id': product.id,
'token': 'tok_visa',
})
assert response.status_code == 302
mock_stripe.Charge.create.assert_called_once()
@patch('apps.payments.services.stripe')
def test_failed_payment(self, mock_stripe, client, user, product):
"""测试失败的支付。"""
mock_stripe.Charge.create.side_effect = Exception('Card declined')
client.force_login(user)
response = client.post(reverse('payments:process'), {
'product_id': product.id,
'token': 'tok_visa',
})
assert response.status_code ==
response.url
邮件发送 Mock
from django.core import mail
from django.test import override_settings
@override_settings(EMAIL_BACKEND='django.core.mail.backends.locmem.EmailBackend')
def test_order_confirmation_email(db, order):
"""测试订单确认邮件。"""
order.send_confirmation_email()
assert len(mail.outbox) == 1
assert order.user.email in mail.outbox[0].to
assert 'Order Confirmation' in mail.outbox[0].subject
集成测试
完整流程测试
import pytest
from django.urls import reverse
from tests.factories import UserFactory, ProductFactory
class TestCheckoutFlow:
"""测试完整的结账流程。"""
def test_guest_to_purchase_flow(self, client, db):
"""测试从访客到购买的完整流程。"""
response = client.post(reverse('users:register'), {
'email': 'test@example.com',
'password': 'testpass123',
'password_confirm': 'testpass123',
})
assert response.status_code == 302
response = client.post(reverse('users:login'), {
'email': 'test@example.com',
'password': 'testpass123',
})
assert response.status_code == 302
product = ProductFactory(price=100)
response = client.get(reverse('products:detail', kwargs={'slug': product.slug}))
assert response.status_code == 200
response = client.post(reverse('cart:add'), {
'product_id': product.id,
'quantity': 1,
})
assert response.status_code ==
response = client.get(reverse())
response.status_code ==
product.name response.content.decode()
patch() mock_payment:
mock_payment.return_value =
response = client.post(reverse())
response.status_code ==
Order.objects.(user__email=).exists()
测试最佳实践
推荐做法
- 使用 Factory: 代替手动创建对象
- 每个测试仅一个断言(Assertion): 使测试更聚焦
- 描述性的测试名称:
test_user_cannot_delete_others_post
- 测试边界情况: 空输入、None 值、边界条件
- Mock 外部服务: 不要依赖外部 API
- 使用 Fixtures: 消除重复
- 测试权限: 确保授权功能正常运行
- 保持测试高速运行: 使用
--reuse-db 和 --nomigrations
禁忌做法
- 不要测试 Django 内部机制: 相信 Django 自身的功能
- 不要测试第三方代码: 相信第三方库的功能
- 不要忽略失败的测试: 必须通过所有测试
- 不要让测试产生依赖: 测试应该能以任意顺序运行
- 不要过度使用 Mock: 仅 Mock 外部依赖
- 不要测试私有方法: 测试公共接口
- 不要使用生产数据库: 始终使用测试数据库
覆盖率
覆盖率配置
pytest --cov=apps --cov-report=html --cov-report=term-missing
open htmlcov/index.html
覆盖率目标
| 组件 | 目标覆盖率 |
|---|
| 模型 | 90%+ |
| 序列化器 | 85%+ |
| 视图 | 80%+ |
| 服务 | 90%+ |
| 实用程序 | 80%+ |
| 整体 | 80%+ |
快速参考
| 模式 | 用法 |
|---|
@pytest.mark.django_db | 启用数据库访问 |
client | Django 测试客户端 |
api_client | DRF API 客户端 |
factory.create_batch(n) | 创建多个对象 |
patch('module.function') | Mock 外部依赖 |
override_settings | 临时更改设置 |
force_authenticate() | 在测试中绕过身份验证 |
assertRedirects | 检查重定向 |
assertTemplateUsed | 验证使用的模板 |
mail.outbox | 检查发送的邮件 |
请记住: 测试即文档。优秀的测试可以说明代码应该如何工作。请保持其简单、易读且可维护。