$ loading_
帮助开发者用 pytest-django 与 TDD 构建高质量 Django 测试体系。
复制安装指令,让 AI 自动完成配置 · 推荐新手
请帮我安装 askskill 上的 "django-tdd" 技能: 1. 下载 https://raw.githubusercontent.com/affaan-m/ECC/main/skills/django-tdd/SKILL.md 2. 保存为 ~/.claude/skills/django-tdd/SKILL.md 3. 装好后重载技能,告诉我可以用了
请用 pytest-django 为一个包含 Article 模型的 Django 应用设计 TDD 测试方案,覆盖创建、字段校验、唯一约束和自定义方法,并使用 factory_boy 生成测试数据。
输出一套清晰的模型测试思路、示例测试代码与工厂定义建议。
请为 Django REST Framework 的用户接口编写 pytest 测试,覆盖列表、详情、创建、权限校验、未登录访问失败和异常返回,并说明如何组织 APIClient fixture。
输出完整的接口测试用例结构、关键断言以及 fixture 组织方式。
我有一个 Django 服务会调用外部支付接口,请用 TDD 思路说明如何用 pytest mock 外部依赖,避免真实请求,并给出覆盖率配置建议与测试分层策略。
输出 mock 示例、测试分层建议,以及提升覆盖率的配置与实践清单。
Test-driven development for Django applications using pytest, factory_boy, and Django REST Framework.
# Step 1: RED - Write failing test
def test_user_creation():
user = User.objects.create_user(email='[email protected]', password='testpass123')
assert user.email == '[email protected]'
assert user.check_password('testpass123')
assert not user.is_staff
# Step 2: GREEN - Make test pass
# Create User model or factory
# Step 3: REFACTOR - Improve while keeping tests green
# 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: marks tests as slow
integration: marks tests as integration tests
# config/settings/test.py
from .base import *
DEBUG = True
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': ':memory:',
}
}
# Disable migrations for speed
class DisableMigrations:
def __contains__(self, item):
return True
def __getitem__(self, item):
return None
MIGRATION_MODULES = DisableMigrations()
# Faster password hashing
PASSWORD_HASHERS = [
'django.contrib.auth.hashers.MD5PasswordHasher',
]
# Email backend
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
# Celery always eager
CELERY_TASK_ALWAYS_EAGER = True
CELERY_TASK_EAGER_PROPAGATES = True
# tests/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):
"""Ensure consistent timezone."""
settings.TIME_ZONE = 'UTC'
@pytest.fixture
def user(db):
"""Create a test user."""
return User.objects.create_user(
email='[email protected]',
password='testpass123',
username='testuser'
)
@pytest.fixture
def admin_user(db):
"""Create an admin user."""
return User.objects.create_superuser(
email='[email protected]',
password='adminpass123',
username='admin'
)
@pytest.fixture
def authenticated_client(client, user):
"""Return authenticated client."""
client.force_login(user)
return client
@pytest.fixture
def api_client():
"""Return DRF API client."""
from rest_framework.test import APIClient
return APIClient()
@pytest.fixture
def authenticated_api_client(api_client, user):
"""Return authenticated API client."""
api_client.force_authenticate(user=user)
return api_client
# tests/factories.py
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):
"""Factory for User model."""
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):
"""Factory for Category model."""
class Meta:
model = Category
name = factory.Faker('word')
…
帮助开发者为代码代理配置性能优化、安全防护与研究优先工作流。
提供数据库迁移、回滚与零停机发布的最佳实践指导,适用于多种 ORM 与 SQL 数据库。
通过双评审智能体对结果进行对抗式校验,提升输出发布前的可靠性
帮助你掌握地道 Rust 模式、所有权与并发实践,编写安全高性能应用。
基于 C++ Core Guidelines 编写、审查并重构更安全现代的 C++ 代码。
为 Claude Code 会话提供系统化校验流程,帮助检查结果正确性与质量。
帮助用户用 pytest 与 TDD 建立高质量 Python 测试体系。
帮助你设计并实现基于pytest与TDD的高质量Python测试策略。
帮助你设计并实现 Python 自动化测试策略,覆盖 pytest、TDD、mock 与覆盖率要求。
帮助你用 TDD 为 Spring Boot 编写、验证并重构高质量后端代码。
帮助你为 Spring Boot 项目实施测试驱动开发、修复缺陷并安全重构。
帮助你为 Spring Boot 项目实施测试驱动开发,覆盖新增、修复与重构场景