Agent sinh test cases từ code
Sử dụng AI Agent để tự động generate unit tests, integration tests, và edge case scenarios từ source code.
Vấn đề
Writing tests thủ công tốn thời gian và dev thường chỉ test happy path. Edge cases, boundary conditions, và error scenarios bị bỏ qua. Test coverage thấp dẫn đến bugs lọt vào production.
Kết quả mong đợi
Bộ test cases bao gồm: unit tests cho mỗi function/method, integration tests cho API endpoints, edge case scenarios, và error handling tests. Coverage target: > 80%.
Yêu cầu trước khi bắt đầu
- ☐ Source code cần test
- ☐ Testing framework đã setup (Jest, Vitest, pytest, etc.)
- ☐ AI tool hỗ trợ code generation
Prompt Pack
System Prompt
You are a QA Engineer who writes comprehensive test suites. You always cover: happy path, edge cases, boundary values, error scenarios, and integration points. You follow testing best practices: AAA pattern, descriptive names, no test interdependency. Task Prompt
Write comprehensive tests for this code:
```
[PASTE CODE HERE]
```
Requirements:
1. Framework: [Jest/Vitest/pytest]
2. Cover: happy path, edge cases, error handling
3. Use AAA pattern (Arrange-Act-Assert)
4. Descriptive test names: 'should [expected behavior] when [condition]'
5. Mock external dependencies
6. Include at least 3 edge case scenarios Guardrails
- Do NOT test implementation details, test behavior
- Do NOT create tests that depend on each other
- Every test must be independently runnable
- Include setup/teardown if needed
- Flag untestable code as 'NEEDS REFACTOR' Workflow từng bước
Select code to test
Chọn module/function cần test. Ưu tiên business logic phức tạp.
Paste code + context
Paste code cùng với type definitions và external dependencies.
Review generated tests
Đọc tests, check logic assertions, verify edge cases reasonable.
Run & fix
Chạy tests, fix failures, thêm missing edge cases.
Check coverage
Run coverage report, thêm tests cho uncovered branches.
Demo Input / Output
📥 Input
📤 Output
```js
describe('calculateDiscount', () => {
it('should apply 20% for VIP with no coupon', () => {
expect(calculateDiscount(100, 'vip', '')).toBe(80);
});
it('should cap total discount at 30%', () => {
expect(calculateDiscount(100, 'vip', 'SAVE10')).toBe(70);
});
it('should throw for zero price', () => {
expect(() => calculateDiscount(0, 'regular', '')).toThrow('Invalid price');
});
// ... more edge cases
});
``` Failure Cases & Cách Fix
Production Checklist
- ☐ Tất cả tests pass
- ☐ Coverage > 80%
- ☐ Edge cases documented
- ☐ Mocks realistic (không quá simplified)
- ☐ Tests chạy < 30 giây
- ☐ No flaky tests
Metrics theo dõi hiệu quả
| Metric | Target | Cách đo |
|---|---|---|
| Code coverage | > 80% | Coverage report từ testing framework |
| Bug escape rate | ≤ 1 bug/sprint lọt qua | Track bugs found in production post-deploy |
| Test generation time | < 30 phút / module | Timestamp từ paste code → tests pass |
Supporting Skill: Wireframe
Khi viết tests cho UI components, wireframe giúp xác định visual states cần test:
1. **Identify UI states**: Từ wireframe, list states mỗi component: default, hover, active, disabled, loading, error
2. **Snapshot tests**: Tạo snapshot test cho mỗi state quan trọng
> Prompt: 'Based on this component wireframe, list all visual states that need snapshot tests.'