test-generator
Generates test code from specifications, components, and API endpoints. Creates unit tests, integration tests, and E2E tests following project testing patterns and conventions.
When & Why to Use This Skill
The Test Generator skill automates the creation of comprehensive test suites, including unit, integration, and E2E tests. By analyzing existing project patterns and codebases, it ensures high-quality test coverage, follows established conventions, and identifies edge cases to improve software reliability and development speed.
Use Cases
- Automated Unit Testing: Quickly generate unit tests for new UI components or business logic functions while adhering to project-specific mocking strategies and naming conventions.
- API Integration Validation: Create robust integration tests for API endpoints to verify response schemas, status codes, and data integrity across different services.
- End-to-End User Flow Simulation: Develop comprehensive E2E tests using frameworks like Cypress to validate critical user journeys such as authentication, navigation, and form submissions.
- Legacy Code Coverage: Analyze existing codebases to identify untested paths and automatically generate test cases to meet project coverage thresholds and prevent regressions.
- Edge Case Identification: Automatically generate test scenarios for error handling, null inputs, and boundary conditions that developers might overlook during manual coding.
| name | test-generator |
|---|---|
| description | Generates test code from specifications, components, and API endpoints. Creates unit tests, integration tests, and E2E tests following project testing patterns and conventions. |
| context | fork: true |
| model | sonnet |
| allowed-tools | read, write, glob, search, codebase_search |
| version | 1.0 |
| error_handling | graceful |
| streaming | supported |
| templates | [unit-test, integration-test, e2e-test, api-test] |
Step 1: Identify Test Type
Determine what type of test is needed:
- Unit Test: Component/function testing
- Integration Test: Service/API integration
- E2E Test: Full user flow testing
- API Test: Endpoint testing
Step 2: Analyze Target Code
Examine code to test:
- Read component/function code
- Identify test cases
- Understand dependencies
- Note edge cases
Step 3: Analyze Test Patterns
Review existing tests:
- Read similar test files
- Identify testing patterns
- Note testing framework usage
- Understand mocking strategies
Step 4: Generate Test Code
Create test following patterns:
- Use appropriate testing framework
- Follow project conventions
- Include comprehensive coverage
- Add edge cases and error scenarios
Step 5: Coverage Analysis
After generating tests, analyze coverage:
Check that generated tests cover all requirements:
- Verify all functions/methods are tested
- Check all branches are covered (if/else, switch, etc.)
- Ensure all edge cases are tested
- Validate error scenarios are covered
Validate tests are runnable:
- Check test syntax is valid
- Verify imports are correct
- Ensure test framework is properly configured
- Validate test setup/teardown is correct
Report coverage percentage:
- Calculate line coverage (if possible)
- Calculate branch coverage (if possible)
- Report uncovered code paths
- Suggest additional tests for uncovered areas
Coverage Validation Checklist:
- All public functions/methods have tests
- All error paths are tested
- All edge cases are covered
- Tests are syntactically valid
- Tests can be executed successfully
- Coverage meets project thresholds (if defined)
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()
})
})
**Integration Test (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');
});
});
**E2E Test (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');
});
});
**Integration with Developer Agent**:
- Generates tests during development
- Ensures test coverage
- Validates implementation
Integration with QA Agent:
- Creates comprehensive test suites
- Generates test plans
- Validates test quality
- Follow Patterns: Match existing test structure
- Comprehensive Coverage: Test happy paths and edge cases
- Clear Test Names: Descriptive test descriptions
- Isolate Tests: Each test should be independent
- Mock Dependencies: Use appropriate mocking strategies
# Generate unit tests
Generate unit tests for components/user-profile
# Generate API tests
Generate API tests for app/api/users
# Generate E2E tests
Generate E2E tests for user authentication flow
# Generate integration tests
Generate integration tests for user service