SKILL

django-tdd

Django testing strategies covering pytest-django, TDD methodology, factory_boy, mocking, coverage, and testing Django REST Framework APIs.

Translation reviewed
Read the original Chinese text

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

Target: Software development & testing. Automatically collected · unverified.

Excerpt from the author’s documentation

Extracted from a fixed version of SKILL.md in its original language. This is the author’s documentation; installation, compatibility and runtime results have not been verified by this site.

Read the original Chinese text

Topics in the source

  • 使用 TDD 进行 Django 测试
  • 何时激活
  • Django 的 TDD 工作流
  • 红-绿-重构循环
  • 设置
  • pytest 配置
  • 测试设置
  • conftest.py

使用 pytest、factory\boy 和 Django REST Framework 进行 Django 应用程序的测试驱动开发。 编写新的 Django 应用程序时 实现 Django REST Framework API 时 测试 Django 模型、视图和序列化器时 为 Django 项目设置测试基础设施时 使用工厂:而不是手动创建对象 每个测试一个断言:保持测试聚焦 描述性测试名称: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 | 检查已发送的邮件 | 记住:测试即文档。好的测试解释了你的代码应如何工作。保持测试简单、可读和可维护。

Source repository
affaan-m/ECC
Source file
docs/zh-CN/skills/django-tdd/SKILL.md
Fixed revision
d2ef2680594f9a17526a6e1a2e18b3d242c5d17b
Read the complete original documentation ↗

Sources and evidence

This record was collected automatically. Its documentation, suitability and runtime behavior have not been manually verified.

Author: affaan-m/ECC · original repository metadata. Documentation review: Not reviewed. Last source check: 2026-11-09T14:01:50.335Z.

Categories are inferred from names, descriptions and paths. They do not establish compatibility.

License and attribution

MIT. Third-party content and copyright remain with their authors. Refer to the original license and terms.

Original license ↗

Synchronization, copying and user-reported success do not mark a resource as runtime-tested.

Limitations and notes

  • Collecting metadata does not mean this site has installed, adapted, runtime-tested or security-reviewed the resource.