Agent skill · software engineering · affaan-m

django-tdd

Django 测试策略,包括 pytest-django、TDD 方法、factory_boy、模拟、覆盖率以及测试 Django REST Framework API。

Why this skill is useful

Adds specific TDD workflows and pytest configurations for Django applications that the AI wouldn't reliably generate on its own.

What it needs

Requires django, factory_boy, pytest installed locally. About 7k tokens when loaded. Last updated 2026-08-06. 238,342 stars on the source repository.

What this skill does

使用 TDD 进行 Django 测试 使用 pytest、factory\boy 和 Django REST Framework 进行 Django 应用程序的测试驱动开发。 何时激活 编写新的 Django 应用程序时 实现 Django REST Framework API 时 测试 Django 模型、视图和序列化器时 为 Django 项目设置测试基础设施时 Django 的 TDD 工作流 红-绿-重构循环 设置 pytest 配置 测试设置 conftest.py Factory Boy 工厂设置 使用工厂 模型测试 模型测试 视图测试 Django 视图测试 DRF API 测试 序列化器测试 API ViewSet 测试 模拟与打补丁 模拟外部服务 模拟邮件发送 集成测试 完整流程测试 测试最佳实践 应该做 使用工厂:而不是手动创建对象 每个测试一个断言:保持测试聚焦 描述性测试名称:testusercannotdeleteotherspost 测试边界情况:空输入、None 值、边界条件 模拟外部服务:不要依赖外部 API 使用夹具:消除重复 测试权限:确保授权有效 保持测试快速:使用 --reuse-db 和 --nomigrations 不应该做 不要测试 Django 内部:相信 Django 能正常工作 不要测试第三方代码:相信库能正常工作 不要忽略失败的测试:所有测试必须通过 不要让测试产生依赖:测试应该能以任何顺序运行 不要过度模拟:只模拟外部依赖 不要测试私有方法:测试公共接口 不要使用生产数据库:始终使用测试数据库 覆盖率 覆盖率配置 覆盖率目标 组件 目标覆盖率 ----------- ----------------- 模型 90%+ 序列化器 85%+ 视图 80%+ 服务 90%+ 工具 80%+ 总体 80%+ 快速参考 模式 用途 --------- ------- @pytest.mark.djangodb 启用数据库访问 client Django 测试客户端 apiclient DRF API 客户端 factory.createbatch(n) 创建多个对象 patch('module.function') 模拟外部依赖 overridesettings 临时更改设置 forceauthenticate() 在测试中绕过身份验证 assertRedirects 检查重定向 assertTemplateUsed 验证模板使用 mail.outbox 检查已发送的邮件 记住:测试即文档。好的测试解释了你的代码应如何工作。保持测试简单、可读和可维护。

How to use it

Reference it in AdaL, Claude Code, Cursor or any coding agent — nothing to install:

@skills affaan-m/django-tdd--fafb7e

View the source on GitHub

Browse the @skills marketplace