Skip to content

测试驱动 AI 开发 ​

在知识图谱中的位置:模块六 · 06_工程实践 · 第 4 节 难度:⭐⭐


1. AI + TDD 工作流 ​

Step 1: 用 PRD 写测试(BDD 格式)
Step 2: AI 生成测试代码
Step 3: 测试通过
Step 4: AI 生成实现代码
Step 5: 测试通过 → 完成
Step 6: 测试不通过 → AI 修复

示例 ​

markdown
# User Authentication Tests

Given a registered user "test@example.com" with password "pass123"
When the user sends POST /api/auth/login with valid credentials
Then the response status should be 200
And the response should include a JWT token

Given a registered user "test@example.com" with password "pass123"
When the user sends POST /api/auth/login with wrong password
Then the response status should be 401
And the response message should be "Invalid credentials"

2. AI 生成测试的最佳实践 ​

  • 让 AI 生成 单元测试 + 集成测试 + E2E 测试
  • 要求 AI 生成 边界情况测试
  • 要求 AI 生成 错误路径测试
  • 要求 测试覆盖率 > 80%

3. 参考资料 ​