django-tdd
Learn test-driven development for Django applications using pytest and factory_boy. This skill covers the red-green-refactor workflow, test configuration, fixture setup, and testing Django models, views, and REST Framework APIs with practical examples.
django-tdd teaches test-driven development with pytest, factory_boy, and Django REST Framework using the red-green-refactor cycle.
AI-generated summary based on this skill's SKILL.md
Install
xu-xiang/everything-claude-code-zh/django-tdd · repository language: JavaScript
git clone https://github.com/xu-xiang/everything-claude-code-zh
cp -r everything-claude-code-zh/skills/django-tdd ~/.claude/skills/django-tddnpx skillfed install xu-xiang/everything-claude-code-zh/django-tddFrequently asked questions
AI-generated answers based on this skill's SKILL.md and metadata
What is django-tdd and what testing approach does it teach?
django-tdd is a skill for learning test-driven development in Django applications using pytest and factory_boy. It covers the red-green-refactor workflow, test configuration, fixture setup, and testing Django models, views, and REST Framework APIs with practical examples. The skill emphasizes writing tests first, then implementing code to pass those tests.
How do I set up pytest and factory_boy for Django model and view testing?
django-tdd teaches pytest and factory_boy setup for Django testing. You'll learn to configure pytest-django, create factories for generating test data, and write unit tests for models and views. The skill covers fixture setup, test organization, and best practices for isolating components during testing.
How can I test Django REST Framework API endpoints and serializers?
django-tdd covers testing Django REST Framework APIs with serializers and viewsets. You'll learn to test API endpoints, validate serializer behavior, test viewset actions, and verify authentication and permissions. The skill includes practical examples for testing complete API workflows and client authentication scenarios.
What does the TDD test driven development django workflow involve?
django-tdd teaches the red-green-refactor cycle: write a failing test (red), implement minimal code to pass it (green), then refactor for quality. This methodology ensures comprehensive test coverage from the start and drives better design decisions. The skill demonstrates this cycle across models, views, and API endpoints.
How do I mock external services and measure code coverage in django-tdd?
django-tdd covers mocking external services like payment processors and email systems, plus setting up code coverage measurement. You'll learn to isolate dependencies, verify mock interactions, and configure coverage tools to identify untested code paths in your Django applications.
Can django-tdd help me write integration tests for complete workflows?
Yes, django-tdd includes integration testing for complete Django application workflows. You'll learn to test end-to-end scenarios combining models, views, and APIs, verify data persistence across components, and test real user interactions. This complements unit tests for comprehensive application validation.
SKILL.md
rendered from the published skill — quoted content, verbatim
使用 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 配置
# pytest.ini
[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: 标记为慢速测试
integration: 标记为集成测试
(truncated - see the full file via the links below)
Read as markdown · JSON record · Browse the source repository
File tree — 1 file
skills/django-tdd/SKILL.md