Writing tests is tedious but necessary. AI generates comprehensive test cases automatically.
The Prompt:
Generate unit tests for this code: [Paste your function/class] Requirements: 1. Use [testing framework - e.g., Jest, xUnit, pytest] 2. Test happy path 3. Test edge cases (null, empty, invalid input) 4. Test error handling 5. Include setup/teardown if needed 6. Add descriptive test names 7. Aim for 80%+ code coverage Explain what each test validates.
Example – JavaScript Function:
function calculateDiscount(price, percentage) {
if (price < 0 || percentage < 0 || percentage > 100) {
throw new Error('Invalid input');
}
return price * (1 - percentage / 100);
}
AI Generates: 8-10 test cases covering valid inputs, boundaries, errors, edge cases
