用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
直接命令不会经过审查 Prompt;运行前请先检查来源。
npx skills add https://github.com/aaione/everything-claude-code-zh --skill django-tdd命令会保持在同一行。复制前请横向滚动并检查完整内容。
想先保存到本地?可下载 SkillsMP 当前能够提供的文件。
Kubernetes 工作负载模式、资源管理、RBAC、probes、autoscaling、ConfigMap/Secret 处理,以及面向生产级部署的 kubectl 调试。
完成任何非平凡任务后使用。智能体按 5 个维度自评输出——准确性、完整性、清晰度、可执行性、简洁性——每项都给出具体证据。生成结构化 1-5 评分卡和具体改进建议。
在 competitive-platform-analysis 产出分层竞品集合后使用。按九个加权维度(定位、声音、视觉工艺、offer packaging、证据、enterprise-readiness、thought leadership、定价、客户 strategic tension)为每个竞品评分,使用明确 1–5 rubrics 和 tension-plot。位于 competitive-report-structure 之前。
基于 SOC 职业分类
正在显示 SKILL.md
| name | django-tdd |
| description | 使用 pytest-django 的 Django 测试策略、TDD 方法论、factory_boy、mock、覆盖率,以及 Django REST Framework API 测试。 |
| origin | ECC |
使用 pytest、factory_boy 和 Django REST Framework 进行 Django 应用的测试驱动开发。
# 步骤 1:红 — 编写失败的测试
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:绿 — 让测试通过
# 创建 User 模型或工厂
# 步骤 3:重构 — 在保持测试通过的同时改进
# 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: 标记为集成测试
# config/settings/test.py
from .base import *
DEBUG = True
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': ':memory:',
}
}
# 禁用迁移以加速
class DisableMigrations:
def __contains__(self, item):
return True
def __getitem__(self, item):
return None
MIGRATION_MODULES = DisableMigrations()
# 更快的密码哈希
PASSWORD_HASHERS = [
'django.contrib.auth.hashers.MD5PasswordHasher',
]
# 邮件后端
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
# Celery 始终立即执行
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):
"""确保一致的时区。"""
settings.TIME_ZONE = 'UTC'
@pytest.fixture
def user(db):
"""创建测试用户。"""
return User.objects.create_user(
email='test@example.com',
password='testpass123',
username='testuser'
)
@pytest.fixture
def admin_user(db):
"""创建管理员用户。"""
return User.objects.create_superuser(
email='admin@example.com',
password='adminpass123',
username='admin'
)
@pytest.fixture
def authenticated_client(client, user):
"""返回已认证的客户端。"""
client.force_login(user)
return client
@pytest.fixture
def api_client():
"""返回 DRF API 客户端。"""
from rest_framework.test import APIClient
return APIClient()
@pytest.fixture
def ():
api_client.force_authenticate(user=user)
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):
"""User 模型的工厂。"""
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):
"""Category 模型的工厂。"""
class Meta:
model = Category
name = factory.Faker('word')
slug = factory.LazyAttribute(lambda obj: obj.name.lower())
description = factory.Faker('text')
class ProductFactory(factory.django.DjangoModelFactory):
"""Product 模型的工厂。"""
class Meta:
model = Product
name = factory.Faker('sentence', nb_words=3)
slug = factory.LazyAttribute( obj: obj.name.lower().replace(, ))
description = factory.Faker()
price = fuzzy.FuzzyDecimal(, , )
stock = fuzzy.FuzzyInteger(, )
is_active =
category = factory.SubFactory(CategoryFactory)
created_by = factory.SubFactory(UserFactory)
():
create:
extracted:
tag extracted:
.tags.add(tag)
# tests/test_models.py
import pytest
from tests.factories import ProductFactory, UserFactory
def test_product_creation():
"""使用工厂测试产品创建。"""
product = ProductFactory(price=100.00, stock=50)
assert product.price == 100.00
assert product.stock == 50
assert product.is_active is True
def test_product_with_tags():
"""测试带标签的产品。"""
tags = [TagFactory(name='electronics'), TagFactory(name='new')]
product = ProductFactory(tags=tags)
assert product.tags.count() == 2
def test_multiple_products():
"""测试创建多个产品。"""
products = ProductFactory.create_batch(10)
assert len(products) == 10
# tests/test_models.py
import pytest
from django.core.exceptions import ValidationError
from tests.factories import UserFactory, ProductFactory
class TestUserModel:
"""测试 User 模型。"""
def test_create_user(self, db):
"""测试创建普通用户。"""
user = UserFactory(email='test@example.com')
assert user.email == 'test@example.com'
assert user.check_password('testpass123')
assert not user.is_staff
assert not user.is_superuser
def test_create_superuser(self, db):
"""测试创建超级用户。"""
user = UserFactory(
email='admin@example.com',
is_staff=True,
is_superuser=True
)
assert user.is_staff
assert user.is_superuser
def test_user_str(self, db):
"""测试用户字符串表示。"""
user = UserFactory(email='test@example.com')
assert str(user) == 'test@example.com'
class TestProductModel:
"""测试 Product 模型。"""
def test_product_creation(self, db):
product = ProductFactory()
product.
product.is_active
product.created_at
():
product = ProductFactory(name=)
product.slug ==
():
product = ProductFactory(price=-)
pytest.raises(ValidationError):
product.full_clean()
():
ProductFactory.create_batch(, is_active=)
ProductFactory.create_batch(, is_active=)
active_count = Product.objects.active().count()
active_count ==
():
product = ProductFactory(stock=)
product.reduce_stock()
product.refresh_from_db()
product.stock ==
pytest.raises(ValueError):
product.reduce_stock()
# tests/test_views.py
import pytest
from django.urls import reverse
from tests.factories import ProductFactory, UserFactory
class TestProductViews:
"""测试产品视图。"""
def test_product_list(self, client, db):
"""测试产品列表视图。"""
ProductFactory.create_batch(10)
response = client.get(reverse('products:list'))
assert response.status_code == 200
assert len(response.context['products']) == 10
def test_product_detail(self, client, db):
"""测试产品详情视图。"""
product = ProductFactory()
response = client.get(reverse('products:detail', kwargs={'slug': product.slug}))
assert response.status_code == 200
assert response.context['product'] == product
def test_product_create_requires_login(self, client, db):
"""测试创建产品需要登录。"""
response = client.get(reverse('products:create'))
assert response.status_code == 302
assert response.url.startswith('/accounts/login/')
def test_product_create_authenticated(self, authenticated_client, db):
"""测试已认证用户创建产品。"""
response = authenticated_client.get(reverse())
response.status_code ==
():
data = {
: ,
: ,
: ,
: ,
: category.,
}
response = authenticated_client.post(reverse(), data)
response.status_code ==
Product.objects.(name=).exists()
# tests/test_serializers.py
import pytest
from rest_framework.exceptions import ValidationError
from apps.products.serializers import ProductSerializer
from tests.factories import ProductFactory
class TestProductSerializer:
"""测试 ProductSerializer。"""
def test_serialize_product(self, db):
"""测试序列化产品。"""
product = ProductFactory()
serializer = ProductSerializer(product)
data = serializer.data
assert data['id'] == product.id
assert data['name'] == product.name
assert data['price'] == str(product.price)
def test_deserialize_product(self, db):
"""测试反序列化产品数据。"""
data = {
'name': 'Test Product',
'description': 'Test description',
'price': '99.99',
'stock': 10,
'category': 1,
}
serializer = ProductSerializer(data=data)
assert serializer.is_valid()
product = serializer.save()
assert product.name == 'Test Product'
assert float(product.price) == 99.99
def ():
data = {
: ,
: ,
: ,
}
serializer = ProductSerializer(data=data)
serializer.is_valid()
serializer.errors
():
data = {
: ,
: ,
: -,
}
serializer = ProductSerializer(data=data)
serializer.is_valid()
serializer.errors
# tests/test_api.py
import pytest
from rest_framework.test import APIClient
from rest_framework import status
from django.urls import reverse
from tests.factories import ProductFactory, UserFactory
class TestProductAPI:
"""测试 Product API 端点。"""
@pytest.fixture
def api_client(self):
"""返回 API 客户端。"""
return APIClient()
def test_list_products(self, api_client, db):
"""测试列出产品。"""
ProductFactory.create_batch(10)
url = reverse('api:product-list')
response = api_client.get(url)
assert response.status_code == status.HTTP_200_OK
assert response.data['count'] == 10
def test_retrieve_product(self, api_client, db):
"""测试获取单个产品。"""
product = ProductFactory()
url = reverse('api:product-detail', kwargs={'pk': product.id})
response = api_client.get(url)
assert response.status_code == status.HTTP_200_OK
assert response.data['id'] == product.id
def test_create_product_unauthorized(self, api_client, db):
"""测试未认证创建产品。"""
url = reverse()
data = {: , : }
response = api_client.post(url, data)
response.status_code == status.HTTP_401_UNAUTHORIZED
():
url = reverse()
data = {
: ,
: ,
: ,
: ,
}
response = authenticated_api_client.post(url, data)
response.status_code == status.HTTP_201_CREATED
response.data[] ==
():
product = ProductFactory(created_by=authenticated_api_client.user)
url = reverse(, kwargs={: product.})
data = {: }
response = authenticated_api_client.patch(url, data)
response.status_code == status.HTTP_200_OK
response.data[] ==
():
product = ProductFactory(created_by=authenticated_api_client.user)
url = reverse(, kwargs={: product.})
response = authenticated_api_client.delete(url)
response.status_code == status.HTTP_204_NO_CONTENT
():
ProductFactory(price=)
ProductFactory(price=)
url = reverse()
response = api_client.get(url, {: })
response.status_code == status.HTTP_200_OK
response.data[] ==
():
ProductFactory(name=)
ProductFactory(name=)
url = reverse()
response = api_client.get(url, {: })
response.status_code == status.HTTP_200_OK
response.data[] ==
# tests/test_views.py
from unittest.mock import patch, Mock
import pytest
class TestPaymentView:
"""测试带 mock 支付网关的支付视图。"""
@patch('apps.payments.services.stripe')
def test_successful_payment(self, mock_stripe, client, user, product):
"""测试使用 mock Stripe 的成功支付。"""
# 配置 mock
mock_stripe.Charge.create.return_value = {
'id': 'ch_123',
'status': 'succeeded',
'amount': 9999,
}
client.force_login(user)
response = client.post(reverse('payments:process'), {
'product_id': product.id,
'token': 'tok_visa',
})
assert response.status_code == 302
mock_stripe.Charge.create.assert_called_once()
@patch('apps.payments.services.stripe')
def test_failed_payment(self, mock_stripe, client, user, product):
"""测试失败的支付。"""
mock_stripe.Charge.create.side_effect = Exception('Card declined')
client.force_login(user)
response = client.post(reverse('payments:process'), {
'product_id': product.id,
'token': 'tok_visa',
})
assert response.status_code ==
response.url
# tests/test_email.py
from django.core import mail
from django.test import override_settings
@override_settings(EMAIL_BACKEND='django.core.mail.backends.locmem.EmailBackend')
def test_order_confirmation_email(db, order):
"""测试订单确认邮件。"""
order.send_confirmation_email()
assert len(mail.outbox) == 1
assert order.user.email in mail.outbox[0].to
assert 'Order Confirmation' in mail.outbox[0].subject
# tests/test_integration.py
import pytest
from django.urls import reverse
from tests.factories import UserFactory, ProductFactory
class TestCheckoutFlow:
"""测试完整结账流程。"""
def test_guest_to_purchase_flow(self, client, db):
"""测试从访客到购买的完整流程。"""
# 步骤 1:注册
response = client.post(reverse('users:register'), {
'email': 'test@example.com',
'password': 'testpass123',
'password_confirm': 'testpass123',
})
assert response.status_code == 302
# 步骤 2:登录
response = client.post(reverse('users:login'), {
'email': 'test@example.com',
'password': 'testpass123',
})
assert response.status_code == 302
# 步骤 3:浏览产品
product = ProductFactory(price=100)
response = client.get(reverse('products:detail', kwargs={'slug': product.slug}))
assert response.status_code == 200
# 步骤 4:添加到购物车
response = client.post(reverse('cart:add'), {
'product_id': product.id,
'quantity': 1,
})
assert response.status_code ==
response = client.get(reverse())
response.status_code ==
product.name response.content.decode()
patch() mock_payment:
mock_payment.return_value =
response = client.post(reverse())
response.status_code ==
Order.objects.(user__email=).exists()
test_user_cannot_delete_others_post--reuse-db 和 --nomigrations# 运行带覆盖率的测试
pytest --cov=apps --cov-report=html --cov-report=term-missing
# 生成 HTML 报告
open htmlcov/index.html
| 组件 | 目标覆盖率 |
|---|---|
| 模型 | 90%+ |
| 序列化器 | 85%+ |
| 视图 | 80%+ |
| 服务 | 90%+ |
| 工具类 | 80%+ |
| 总体 | 80%+ |
| 模式 | 用途 |
|---|---|
@pytest.mark.django_db | 启用数据库访问 |
client | Django 测试客户端 |
api_client | DRF API 客户端 |
factory.create_batch(n) | 创建多个对象 |
patch('module.function') | Mock 外部依赖 |
override_settings | 临时更改设置 |
force_authenticate() | 在测试中绕过认证 |
assertRedirects | 检查重定向 |
assertTemplateUsed | 验证模板使用 |
mail.outbox | 检查已发送邮件 |
记住:测试就是文档。好的测试解释了你的代码应该如何工作。保持它们简单、可读和可维护。