💻 Coding
Automated Unit Test Generator for Any Codebase
Generate comprehensive unit tests with edge cases, mocking, and coverage targets for any function or module. Supports multiple testing frameworks.
0Reviews
Prompt
Act as a senior QA engineer and test automation specialist. I will provide you with a code snippet or module. Your task is to generate a complete suite of unit tests. Input Code: [Paste your code here] Language/Framework: [Language] with [Testing Framework] (e.g., Python/pytest, JavaScript/Jest, Java/JUnit, Go/testing) Follow these steps: Analyze the code: Identify all public functions/methods, their inputs, outputs, and side effects. Generate test cases for each function: Happy path (expected inputs and outputs) Edge cases (empty inputs, null values, boundary values, maximum/minimum) Error cases (invalid inputs, exceptions, timeout scenarios) Type-specific cases (negative numbers, special characters, Unicode, large datasets) Mocking: Where external dependencies exist (database calls, API requests, file I/O), create appropriate mocks or stubs. Test structure: Follow the Arrange-Act-Assert (AAA) pattern for each test. Naming: Use descriptive test names following the convention: test_[functionName]_[scenario]_[expectedResult]. Coverage target: Aim for 90%+ code coverage. List any lines that cannot be reasonably tested and explain why. Constraints: Tests must be runnable without modification. Include all necessary imports and setup/teardown blocks. Add brief comments explaining the purpose of non-obvious test cases.
Instructions
Paste your actual code: Replace the code block placeholder with the function or module you want tested. Specify your stack: State your language and testing framework for correctly formatted tests. Include dependencies: Mention external services your code calls so mocks are generated appropriately. Review edge cases: Check the generated edge cases against your domain logic and add any business-specific scenarios.
Examples
Example Input
Act as a senior QA engineer and test automation specialist. I will provide you with a code snippet or module. Your task is to generate a complete suite of unit tests.
Input Code:
[```javascript
function calculateDiscount(price, limit) {
if (price > limit) return price * 0.9;
return price;
}]
Language/Framework: [TypeScript] with [Jest] (e.g., Python/pytest, JavaScript/Jest, Java/JUnit, Go/testing)
Follow these steps:
Analyze the code: Identify all public functions/methods, their inputs, outputs, and side effects.
Generate test cases for each function:
Happy path (expected inputs and outputs)
Edge cases (empty inputs, null values, boundary values, maximum/minimum)
Error cases (invalid inputs, exceptions, timeout scenarios)
Type-specific cases (negative numbers, special characters, Unicode, large datasets)
Mocking: Where external dependencies exist (database calls, API requests, file I/O), create appropriate mocks or stubs.
Test structure: Follow the Arrange-Act-Assert (AAA) pattern for each test.
Naming: Use descriptive test names following the convention: test_[functionName]_[scenario]_[expectedResult].
Coverage target: Aim for 90%+ code coverage. List any lines that cannot be reasonably tested and explain why.
Constraints:
Tests must be runnable without modification.
Include all necessary imports and setup/teardown blocks.
Add brief comments explaining the purpose of non-obvious test cases.Example Output
Unit Test Suite for calculateDiscount
Language: TypeScript
Framework: Jest
1. Code Analysis
Function: calculateDiscount(price, limit)
Inputs
price: number
limit: number
Output
Returns number
Logic
If price > limit → apply 10% discount
Otherwise → return original price
Side Effects
None (pure function)
Branches to Test
price > limit
price <= limit
2. Jest Test Suite
calculateDiscount.test.ts
import { describe, test, expect } from '@jest/globals';
/* Function under test */
function calculateDiscount(price: number, limit: number): number {
if (price > limit) return price * 0.9;
return price;
}
describe("calculateDiscount", () => {
// Happy Path
test("test_calculateDiscount_priceAboveLimit_appliesDiscount", () => {
// Arrange
const price = 200;
const limit = 100;
// Act
const result = calculateDiscount(price, limit);
// Assert
expect(result).toBe(180);
});
test("test_calculateDiscount_priceEqualLimit_noDiscount", () => {
const result = calculateDiscount(100, 100);
expect(result).toBe(100);
});
test("test_calculateDiscount_priceBelowLimit_returnsOriginalPrice", () => {
const result = calculateDiscount(80, 100);
expect(result).toBe(80);
});
// Edge Cases
test("test_calculateDiscount_zeroPrice_returnsZero", () => {
const result = calculateDiscount(0, 100);
expect(result).toBe(0);
});
test("test_calculateDiscount_boundaryValueJustAboveLimit_appliesDiscount", () => {
const result = calculateDiscount(101, 100);
expect(result).toBe(90.9);
});
test("test_calculateDiscount_boundaryValueJustBelowLimit_noDiscount", () => {
const result = calculateDiscount(99.99, 100);
expect(result).toBe(99.99);
});
// Type-Specific Cases
test("test_calculateDiscount_negativePrice_returnsNegativePrice", () => {
const result = calculateDiscount(-50, 100);
expect(result).toBe(-50);
});
test("test_calculateDiscount_largeNumbers_handlesLargeValues", () => {
const result = calculateDiscount(1000000, 500000);
expect(result).toBe(900000);
});
// Error Cases
test("test_calculateDiscount_nullInputs_returnsNaN", () => {
const result = calculateDiscount(null as unknown as number, 100);
expect(result).toBeNaN();
});
test("test_calculateDiscount_undefinedInputs_returnsNaN", () => {
const result = calculateDiscount(undefined as unknown as number, 100);
expect(result).toBeNaN();
});
});
3. Mocking
No mocks required because:
The function is pure
No external dependencies
No database, API, or file system interactions
4. Coverage
Expected coverage:
Statements: 100%
Branches: 100%
Functions: 100%
Lines: 100%
Both logical branches are tested:
price > limit
price <= limit
5. Untestable Lines
None.
The function is deterministic and has no asynchronous behavior or external dependencies.
6. Run Tests
Install dependencies:
npm install --save-dev jest ts-jest @types/jest
Initialize config:
npx ts-jest config:init
Run tests:
npm test
If you want, I can also show you how senior QA engineers expand this simple test into mutation testing + property-based testing, which is how teams reach 99% bug detection coverage.