Cursor AI Testing Patterns
Effective test automation patterns with Cursor AI IDE including Composer for test suite generation, Cmd+K for inline test edits, Chat for test debugging, codebase-aware test generation, and rules configuration for testing conventions.
git clone --depth 1 https://github.com/PramodDutta/qaskills /tmp/cursor-ai-testing-patterns && cp -r /tmp/cursor-ai-testing-patterns/seed-skills/cursor-testing-patterns ~/.claude/skills/cursor-ai-testing-patternsSKILL.md
# Cursor AI Testing Patterns Skill
You are an expert in using Cursor AI IDE for test automation. When the user asks you to generate tests with Cursor, configure Cursor rules for testing, use Composer for multi-file test generation, or optimize Cursor workflows for QA, follow these detailed instructions.
## Core Principles
1. **Codebase-aware generation** -- Cursor indexes your entire codebase. Reference files, functions, and patterns using @mentions to give the AI full context for test generation.
2. **Rules for consistent quality** -- Configure .cursorrules to enforce testing conventions, naming patterns, and framework preferences across all AI-generated tests.
3. **Composer for multi-file operations** -- Use Cursor Composer for generating test suites that span multiple files: page objects, fixtures, helpers, and test specs.
4. **Cmd+K for surgical edits** -- Use inline editing (Cmd+K) for targeted changes: adding a test case, fixing an assertion, or refactoring a single test function.
5. **Chat for investigation** -- Use Cursor Chat with @codebase to understand existing test patterns before generating new ones. Ask it to explain failures.
6. **Apply from Chat** -- When Chat suggests code, use the Apply button to directly insert or replace code in your editor rather than copy-pasting.
7. **Iterative test development** -- Generate a basic test suite first, run it, then use Cursor to fix failures and add edge cases based on execution results.
## Project Structure
```
project/
.cursorrules # Testing conventions
.cursor/
rules/
testing.mdc # Testing-specific rules
src/
services/
user-service.ts
user-service.test.ts
components/
LoginForm.tsx
LoginForm.test.tsx
tests/
e2e/
login.spec.ts
checkout.spec.ts
fixtures/
test-data.ts
helpers/
test-utils.ts
```
## Cursor Rules for Testing
```markdown
<!-- .cursorrules -->
# Testing Rules
## Test Framework
- Use vitest for all unit and integration tests
- Use Playwright for E2E tests
- TypeScript strict mode in all test files
## Test Conventions
- Follow Arrange-Act-Assert (AAA) pattern
- Name tests: "should [expected behavior] when [condition]"
- Group related tests in describe blocks
- One assertion focus per test (multiple asserts allowed if testing same behavior)
- Always include edge cases: null, undefined, empty values, boundary conditions
- Always include error/failure test cases
## Mocking
- Use vi.mock() for module mocking
- Use vi.fn() for function mocking
- Use vi.spyOn() for spying on existing methods
- Reset mocks in beforeEach with vi.clearAllMocks()
- Never mock the module under test
## Assertions
- Prefer specific assertions: toBe, toEqual, toContain over toBeTruthy
- Use toThrow for error testing
- Use resolves/rejects for async assertions
- Include meaningful error messages in assertions
## File Organization
- Colocate tests with source files: foo.ts -> foo.test.ts
- E2E tests in tests/e2e/
- Shared test utilities in tests/helpers/
- Test fixtures in tests/fixtures/
## Do Not
- Use any type in test files
- Use setTimeout for async waiting
- Write tests that depend on execution order
- Hard-code API URLs or credentials
- Skip tests without a TODO comment explaining why
```
## Composer Workflows for Test Generation
```typescript
// Workflow 1: Generate comprehensive test suite with Composer
// Prompt: "Generate a complete test suite for @user-service.ts with vitest.
// Include unit tests for all public methods, mock the database dependency,
// test both success and error paths, and follow the patterns in @user-service.test.ts"
// Result: Cursor generates the full test file considering both files
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { UserService } from './user-service';
// Cursor reads the actual implementation to create accurate mocks
vi.mock('../db/connection', () => ({
db: {
select: vi.fn(),
insert: vi.fn(),
update: vi.fn(),
delete: vi.fn(),
},
}));
import { db } from '../db/connection';
describe('UserService', () => {
let service: UserService;
beforeEach(() => {
service = new UserService();
vi.clearAllMocks();
});
// Cursor generates tests for each public method found in the source
describe('findById', () => {
it('should return user when found', async () => {
const mockUser = { id: '1', name: 'Alice', email: 'alice@test.com' };
vi.mocked(db.select).mockResolvedValueOnce([mockUser]);
const result = await service.findById('1');
expect(result).toEqual(mockUser);
});
it('should return null when user not found', async () => {
vi.mocked(db.select).mockResolvedValueOnce([]);
const result = await service.findById('nonexistent');
expect(result).toBeNull();
});
it('should throw when database error occurs', async () => {
vi.mocked(db.select).mockRejectedValueOnce(new Error('DB connection failed'));
await expect(service.findById('1')).rejects.toThrow('DB connection failed');
});
});
// ... Cursor continues for all public methods
});
```
## Cmd+K Inline Editing Patterns
```typescript
// Pattern 1: Add a test case inline
// Select empty space inside describe block, press Cmd+K
// Prompt: "Add a test case that verifies createUser rejects duplicate emails"
// Cursor generates and inserts:
it('should reject duplicate emails', async () => {
vi.mocked(db.insert).mockRejectedValueOnce(
new Error('UNIQUE constraint failed: users.email')
);
await expect(
service.createUser({ name: 'Bob', email: 'existing@test.com' })
).rejects.toThrow('Email already exists');
});
// Pattern 2: Strengthen an assertion
// Select a weak assertion, press Cmd+K
// Prompt: "Make this assertion more specific"
// Before:
expect(result).toBeTruthy();
// After:
expect(result).toEqual({
id: expect.any(String),
name: 'Alice',
email: 'alice@test.com',Automated accessibility testing with axe-core integrated into CI pipelines, including custom rule configuration, issue prioritization, and remediation guidance.
Validating A/B test implementations including traffic splitting accuracy, statistical significance calculation, metric tracking, and experiment cleanup.
Comprehensive WCAG compliance and accessibility testing covering ARIA, keyboard navigation, screen readers, color contrast, and automated a11y validation.
Comprehensive WCAG 2.1 AA compliance testing combining automated axe-core scans with manual keyboard navigation, screen reader compatibility, and focus management verification
American Fuzzy Lop Plus Plus mutation-based fuzz testing for finding crashes, hangs, and security vulnerabilities in binary programs.
Fast Rust-based headless browser automation CLI with Node.js fallback for AI agents, featuring navigation, clicking, typing, snapshots, and structured commands optimized for agent workflows.
AI-first testing methodology where autonomous agents plan, generate, execute, and maintain test suites with minimal human intervention, covering agent orchestration, feedback loops, and intelligent test prioritization.
Comprehensive evaluation patterns for AI agents including multi-turn conversation testing, LLM-as-judge frameworks, benchmark suites, regression detection, and systematic eval pipelines for measuring agent quality and safety.