Documentation
README
使用 TDD 进行 Django 测试
使用 pytest、factory_boy 和 Django REST Framework 对 Django 应用程序进行测试驱动开发(TDD)。
何时激活
- 编写新的 Django 应用程序时
- 实现 Django REST Framework API 时
- 测试 Django 模型(Models)、视图(Views)和序列化器(Serializers)时
- 为 Django 项目搭建测试基础设施时
Django 的 TDD 工作流
红-绿-重构(Red-Green-Refactor)周期
# 步骤 1: 红(RED) - 编写失败的测试
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
# 步骤 2: 绿(GREEN) - 使测试通过
# 创建 User 模型或工厂
# 步骤 3: 重构(REFACTOR) - 在保持测试通过的同时改进代码
配置
pytest 配置
This is the opening of the README. Read the full README on GitHub.