name: test-generator description: 根据规范、组件和API端点生成测试代码。创建单元测试、集成测试和E2E测试,遵循项目测试模式和约定。 allowed-tools: read, write, glob, search, codebase_search version: 1.0 best_practices:
- 分析现有测试模式
- 遵循项目测试约定
- 生成全面的测试覆盖
- 包括边缘情况和错误场景
- 使用适当的测试框架 error_handling: graceful streaming: supported templates: [unit-test, integration-test, e2e-test, api-test]
<identity> 测试生成器技能 - 根据规范、组件和API端点生成测试代码,遵循项目测试模式和约定。 </identity>
<capabilities>
- 为新组件生成测试
- 为API端点创建测试
- 为用户流程生成E2E测试
- 创建集成测试
- 为现有代码添加测试覆盖 </capabilities>
<instructions> <execution_process>
步骤1:识别测试类型
确定需要什么类型的测试:
- 单元测试: 组件/功能测试
- 集成测试: 服务/API集成测试
- E2E测试: 完整用户流程测试
- API测试: 端点测试
步骤2:分析目标代码
检查要测试的代码:
- 读取组件/功能代码
- 识别测试用例
- 理解依赖关系
- 注意边缘情况
步骤3:分析测试模式
回顾现有测试:
- 读取类似的测试文件
- 识别测试模式
- 注意测试框架使用
- 理解模拟策略
步骤4:生成测试代码
创建测试,遵循模式:
- 使用适当的测试框架
- 遵循项目约定
- 包括全面覆盖
- 添加边缘情况和错误场景
步骤5:覆盖分析
生成测试后,分析覆盖:
-
检查生成的测试覆盖所有需求:
- 验证所有函数/方法都有测试
- 检查所有分支都被覆盖(if/else, switch等)
- 确保所有边缘情况都被测试
- 验证错误场景被覆盖
-
验证测试可运行:
- 检查测试语法有效
- 验证导入正确
- 确保测试框架正确配置
- 验证测试设置/拆卸正确
-
报告覆盖百分比:
- 计算行覆盖(如果可能)
- 计算分支覆盖(如果可能)
- 报告未覆盖的代码路径
- 建议为未覆盖区域添加额外测试
-
覆盖验证清单:
- [ ] 所有公共函数/方法都有测试
- [ ] 所有错误路径都被测试
- [ ] 所有边缘情况都被覆盖
- [ ] 测试语法有效
- [ ] 测试可以成功执行
- [ ] 覆盖满足项目阈值(如果定义) </execution_process> </instructions>
<examples> <code_example> 单元测试(React组件)
import { render, screen, waitFor } from '@testing-library/react'
import { describe, it, expect, vi } from 'vitest'
import { UserProfile } from './user-profile'
describe('UserProfile', () => {
it('renders user information', async () => {
const mockUser = { id: '1', name: 'John', email: 'john@example.com' }
render(<UserProfile user={mockUser} />)
await waitFor(() => {
expect(screen.getByText('John')).toBeInTheDocument()
expect(screen.getByText('john@example.com')).toBeInTheDocument()
})
})
it('handles loading state', () => {
render(<UserProfile user={null} loading />)
expect(screen.getByTestId('loading')).toBeInTheDocument()
})
it('handles error state', () => {
render(<UserProfile user={null} error="Failed to load" />)
expect(screen.getByText('Failed to load')).toBeInTheDocument()
})
})
</code_example>
<code_example> 集成测试(API)
import { describe, it, expect, beforeAll, afterAll } from 'vitest';
import { createTestClient } from './test-client';
describe('Users API', () => {
let client: TestClient;
beforeAll(() => {
client = createTestClient();
});
afterAll(async () => {
await client.cleanup();
});
it('creates a user', async () => {
const response = await client.post('/api/users', {
email: 'test@example.com',
name: 'Test User',
});
expect(response.status).toBe(201);
expect(response.data).toHaveProperty('id');
expect(response.data.email).toBe('test@example.com');
});
it('validates required fields', async () => {
const response = await client.post('/api/users', {});
expect(response.status).toBe(400);
expect(response.data).toHaveProperty('errors');
});
});
</code_example>
<code_example> 端到端测试(Cypress)
describe('User Authentication Flow', () => {
beforeEach(() => {
cy.visit('/login');
});
it('allows user to login', () => {
cy.get('[data-testid="email-input"]').type('user@example.com');
cy.get('[data-testid="password-input"]').type('password123');
cy.get('[data-testid="login-button"]').click();
cy.url().should('include', '/dashboard');
cy.get('[data-testid="user-menu"]').should('be.visible');
});
it('shows error for invalid credentials', () => {
cy.get('[data-testid="email-input"]').type('invalid@example.com');
cy.get('[data-testid="password-input"]').type('wrong');
cy.get('[data-testid="login-button"]').click();
cy.get('[data-testid="error-message"]')
.should('be.visible')
.and('contain', 'Invalid credentials');
});
});
</code_example> </examples>
<instructions> <integration> 与开发人员代理集成:
- 在开发过程中生成测试
- 确保测试覆盖
- 验证实现
与QA代理集成:
- 创建全面的测试套件
- 生成测试计划
- 验证测试质量 </integration>
<best_practices>
- 遵循模式: 匹配现有测试结构
- 全面覆盖: 测试正常路径和边缘情况
- 清晰的测试名称: 描述性的测试描述
- 隔离测试: 每个测试应该独立
- 模拟依赖: 使用适当的模拟策略 </best_practices> </instructions>
<examples> <usage_example> 示例命令:
# 生成单元测试
为 components/user-profile 生成单元测试
# 生成API测试
为 app/api/users 生成API测试
# 生成E2E测试
为用户认证流程生成E2E测试
# 生成集成测试
为用户服务生成集成测试
</usage_example> </examples>