关于 testing-patterns
testing-patterns 是面向开发者的测试编写技能,帮助构建易维护的测试。内容涵盖测试组织、模拟、夹具、集成测试和覆盖率目标。
主要功能
- 01AAA 测试结构
- 02模拟外部服务
- 03夹具与工厂
- 04集成测试模式
- 05覆盖率目标
适用场景
- 01组织单元测试
- 02模拟邮件等服务
- 03创建可复用测试数据
- 04设定测试覆盖率目标
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ langchain-ai/ skills-benchmarks --skill testing-patterns安装后在 Claude Code 中输入/testing-patterns确认已加载。
详细介绍
测试模式
使用现代模式编写高效、易维护的测试。
测试结构(AAA 模式)
def test_user_registration():
# Arrange
user_data = {"email": "[email protected]", "password": "secure123"}
# Act
result = register_user(user_data)
# Assert
assert result.success is True
assert result.user.email == "[email protected]"
模拟外部服务
from unittest.mock import Mock, patch
@patch('services.email.send_email')
def test_sends_welcome_email(mock_send):
mock_send.return_value = True
register_user({"email": "[email protected]"})
mock_send.assert_called_once_with(
to="[email protected]",
template="welcome"
)
固件与工厂
import pytest
from factories import UserFactory
@pytest.fixture
def user():
return UserFactory.create(role="admin")
@pytest.fixture
def authenticated_client(user):
client = TestClient(app)
client.login(user)
return client
def test_admin_dashboard(authenticated_client):
response = authenticated_client.get("/admin")
assert response.status_code == 200
集成测试
@pytest.mark.integration
def test_full_checkout_flow(db_session, stripe_mock):
# Create test data
user = create_user()
product = create_product(price=100)
# Execute flow
cart = add_to_cart(user, product)
order = checkout(cart, payment_method="card")
# Verify
assert order.status == "completed"
assert stripe_mock.charges.create.called
测试覆盖率目标
- 单元测试:覆盖率达到 80% 以上
- 集成测试:覆盖关键路径
- E2E 测试:仅覆盖正常流程
评论
支持 Markdown