testing-strategies
13
总安装量
8
周安装量
#25146
全站排名
安装命令
npx skills add https://github.com/miles990/claude-software-skills --skill testing-strategies
Agent 安装分布
antigravity
8
gemini-cli
7
claude-code
7
windsurf
6
codex
6
Skill 文档
Testing Strategies
Overview
Testing pyramid, patterns, and practices for building reliable software.
Testing Pyramid
/\
/ \
/ E2E\ Few, slow, expensive
/ââââââ\
/ \
/Integration\ Some, medium speed
/ââââââââââââââ\
/ \
/ Unit Tests \ Many, fast, cheap
/____________________\
| Level | Speed | Scope | Quantity |
|---|---|---|---|
| Unit | Fast (ms) | Single function/class | Many (70%) |
| Integration | Medium (s) | Multiple components | Some (20%) |
| E2E | Slow (min) | Full system | Few (10%) |
Unit Testing
Structure: Arrange-Act-Assert
describe('calculateDiscount', () => {
it('applies 10% discount for orders over $100', () => {
// Arrange
const order = { items: [{ price: 150 }] };
const discountService = new DiscountService();
// Act
const result = discountService.calculateDiscount(order);
// Assert
expect(result).toBe(15);
});
it('returns 0 for orders under $100', () => {
// Arrange
const order = { items: [{ price: 50 }] };
const discountService = new DiscountService();
// Act
const result = discountService.calculateDiscount(order);
// Assert
expect(result).toBe(0);
});
});
Mocking
// Mock dependencies
const mockEmailService = {
send: jest.fn().mockResolvedValue({ success: true })
};
const mockUserRepo = {
findById: jest.fn().mockResolvedValue({ id: '1', email: 'test@example.com' })
};
describe('NotificationService', () => {
let service: NotificationService;
beforeEach(() => {
jest.clearAllMocks();
service = new NotificationService(mockEmailService, mockUserRepo);
});
it('sends email to user', async () => {
await service.notifyUser('1', 'Hello!');
expect(mockUserRepo.findById).toHaveBeenCalledWith('1');
expect(mockEmailService.send).toHaveBeenCalledWith(
'test@example.com',
'Hello!'
);
});
it('throws when user not found', async () => {
mockUserRepo.findById.mockResolvedValue(null);
await expect(service.notifyUser('999', 'Hello!'))
.rejects.toThrow('User not found');
});
});
Testing Edge Cases
describe('parseAge', () => {
// Happy path
it('parses valid age string', () => {
expect(parseAge('25')).toBe(25);
});
// Edge cases
it('handles zero', () => {
expect(parseAge('0')).toBe(0);
});
it('handles boundary values', () => {
expect(parseAge('1')).toBe(1);
expect(parseAge('150')).toBe(150);
});
// Error cases
it('throws on negative numbers', () => {
expect(() => parseAge('-5')).toThrow('Age cannot be negative');
});
it('throws on non-numeric input', () => {
expect(() => parseAge('abc')).toThrow('Invalid age format');
});
it('throws on empty string', () => {
expect(() => parseAge('')).toThrow('Age is required');
});
// Null/undefined
it('throws on null', () => {
expect(() => parseAge(null as any)).toThrow();
});
});
Integration Testing
API Testing
import request from 'supertest';
import { app } from '../app';
import { db } from '../database';
describe('POST /api/users', () => {
beforeEach(async () => {
await db.users.deleteMany({});
});
afterAll(async () => {
await db.disconnect();
});
it('creates a new user', async () => {
const response = await request(app)
.post('/api/users')
.send({
email: 'test@example.com',
name: 'Test User'
})
.expect(201);
expect(response.body).toMatchObject({
id: expect.any(String),
email: 'test@example.com',
name: 'Test User'
});
// Verify in database
const user = await db.users.findOne({ email: 'test@example.com' });
expect(user).not.toBeNull();
});
it('returns 400 for invalid email', async () => {
const response = await request(app)
.post('/api/users')
.send({
email: 'invalid-email',
name: 'Test User'
})
.expect(400);
expect(response.body.error).toBe('Invalid email format');
});
it('returns 409 for duplicate email', async () => {
// Create first user
await request(app)
.post('/api/users')
.send({ email: 'test@example.com', name: 'First' });
// Try to create duplicate
const response = await request(app)
.post('/api/users')
.send({ email: 'test@example.com', name: 'Second' })
.expect(409);
expect(response.body.error).toBe('Email already exists');
});
});
Database Testing with Testcontainers
import { PostgreSqlContainer } from '@testcontainers/postgresql';
import { Pool } from 'pg';
describe('UserRepository', () => {
let container: StartedPostgreSqlContainer;
let pool: Pool;
let repo: UserRepository;
beforeAll(async () => {
container = await new PostgreSqlContainer().start();
pool = new Pool({ connectionString: container.getConnectionUri() });
await runMigrations(pool);
repo = new UserRepository(pool);
}, 60000);
afterAll(async () => {
await pool.end();
await container.stop();
});
beforeEach(async () => {
await pool.query('TRUNCATE users CASCADE');
});
it('creates and retrieves user', async () => {
const created = await repo.create({
email: 'test@example.com',
name: 'Test'
});
const found = await repo.findById(created.id);
expect(found).toEqual(created);
});
});
E2E Testing
Playwright
import { test, expect } from '@playwright/test';
test.describe('User Authentication', () => {
test('successful login flow', async ({ page }) => {
await page.goto('/login');
// Fill form
await page.fill('[data-testid="email-input"]', 'user@example.com');
await page.fill('[data-testid="password-input"]', 'password123');
// Submit
await page.click('[data-testid="login-button"]');
// Verify redirect to dashboard
await expect(page).toHaveURL('/dashboard');
await expect(page.locator('[data-testid="welcome-message"]'))
.toContainText('Welcome, user@example.com');
});
test('shows error for invalid credentials', async ({ page }) => {
await page.goto('/login');
await page.fill('[data-testid="email-input"]', 'wrong@example.com');
await page.fill('[data-testid="password-input"]', 'wrongpassword');
await page.click('[data-testid="login-button"]');
await expect(page.locator('[data-testid="error-message"]'))
.toBeVisible()
.toContainText('Invalid credentials');
});
});
test.describe('Shopping Cart', () => {
test('add item and checkout', async ({ page }) => {
// Setup - login
await page.goto('/login');
await page.fill('[data-testid="email-input"]', 'buyer@example.com');
await page.fill('[data-testid="password-input"]', 'password');
await page.click('[data-testid="login-button"]');
// Browse products
await page.goto('/products');
await page.click('[data-testid="product-1"] [data-testid="add-to-cart"]');
// Verify cart
await expect(page.locator('[data-testid="cart-count"]')).toHaveText('1');
// Checkout
await page.click('[data-testid="cart-icon"]');
await page.click('[data-testid="checkout-button"]');
// Fill shipping
await page.fill('[data-testid="address"]', '123 Test St');
await page.click('[data-testid="place-order"]');
// Verify success
await expect(page).toHaveURL(/\/orders\/\d+/);
await expect(page.locator('[data-testid="order-status"]'))
.toContainText('Order Confirmed');
});
});
Visual Regression Testing
import { test, expect } from '@playwright/test';
test('homepage visual regression', async ({ page }) => {
await page.goto('/');
// Wait for dynamic content
await page.waitForSelector('[data-testid="hero-section"]');
// Take screenshot and compare
await expect(page).toHaveScreenshot('homepage.png', {
maxDiffPixels: 100,
threshold: 0.2
});
});
test('responsive design', async ({ page }) => {
await page.setViewportSize({ width: 375, height: 667 }); // iPhone SE
await page.goto('/');
await expect(page).toHaveScreenshot('homepage-mobile.png');
});
Test-Driven Development (TDD)
Red-Green-Refactor Cycle
// 1. RED - Write failing test first
test('passwordValidator rejects passwords without numbers', () => {
const result = validatePassword('NoNumbers!');
expect(result.valid).toBe(false);
expect(result.errors).toContain('Must contain at least one number');
});
// 2. GREEN - Write minimal code to pass
function validatePassword(password: string): ValidationResult {
const errors: string[] = [];
if (!/\d/.test(password)) {
errors.push('Must contain at least one number');
}
return { valid: errors.length === 0, errors };
}
// 3. REFACTOR - Improve code quality
const VALIDATION_RULES = [
{ pattern: /\d/, message: 'Must contain at least one number' },
{ pattern: /[A-Z]/, message: 'Must contain at least one uppercase letter' },
{ pattern: /[a-z]/, message: 'Must contain at least one lowercase letter' },
{ pattern: /.{8,}/, message: 'Must be at least 8 characters' }
];
function validatePassword(password: string): ValidationResult {
const errors = VALIDATION_RULES
.filter(rule => !rule.pattern.test(password))
.map(rule => rule.message);
return { valid: errors.length === 0, errors };
}
Testing Patterns
Test Fixtures
// fixtures/users.ts
export const validUser = {
email: 'test@example.com',
name: 'Test User',
role: 'user'
};
export const adminUser = {
...validUser,
role: 'admin',
email: 'admin@example.com'
};
// In tests
import { validUser, adminUser } from '../fixtures/users';
describe('UserService', () => {
it('creates user with valid data', async () => {
const result = await service.create(validUser);
expect(result.email).toBe(validUser.email);
});
});
Factory Functions
// factories/user.factory.ts
import { faker } from '@faker-js/faker';
export function createUser(overrides: Partial<User> = {}): User {
return {
id: faker.string.uuid(),
email: faker.internet.email(),
name: faker.person.fullName(),
createdAt: faker.date.past(),
...overrides
};
}
// In tests
it('handles users with long names', () => {
const user = createUser({ name: 'A'.repeat(100) });
const result = formatUserCard(user);
expect(result.displayName).toHaveLength(50); // Truncated
});
Testing Async Code
// Async/await
it('fetches user data', async () => {
const user = await userService.getById('123');
expect(user.name).toBe('John');
});
// Promises
it('fetches user data', () => {
return userService.getById('123').then(user => {
expect(user.name).toBe('John');
});
});
// Testing rejected promises
it('throws on invalid id', async () => {
await expect(userService.getById('invalid'))
.rejects.toThrow('User not found');
});
// Waiting for side effects
it('debounces search input', async () => {
const onSearch = jest.fn();
render(<SearchBox onSearch={onSearch} debounceMs={300} />);
await userEvent.type(screen.getByRole('textbox'), 'test');
// Should not have called yet
expect(onSearch).not.toHaveBeenCalled();
// Wait for debounce
await waitFor(() => {
expect(onSearch).toHaveBeenCalledWith('test');
}, { timeout: 500 });
});
Code Coverage
Coverage Metrics
| Metric | What It Measures |
|---|---|
| Line | Percentage of lines executed |
| Branch | Percentage of if/else branches taken |
| Function | Percentage of functions called |
| Statement | Percentage of statements executed |
Jest Configuration
// jest.config.js
module.exports = {
collectCoverageFrom: [
'src/**/*.{ts,tsx}',
'!src/**/*.d.ts',
'!src/**/*.stories.tsx',
'!src/test/**'
],
coverageThreshold: {
global: {
branches: 80,
functions: 80,
lines: 80,
statements: 80
}
}
};
Related Skills
- [[code-quality]] – Writing testable code
- [[devops-cicd]] – CI integration
- [[performance-optimization]] – Performance testing
Sharp Edgesï¼å¸¸è¦é·é±ï¼
éäºæ¯æ¸¬è©¦ä¸æå¸¸è¦ä¸ä»£å¹æé«çé¯èª¤
SE-1: 測試實ä½èéè¡çº
- å´é度: high
- æ å¢: 測試é度è¦åå §é¨å¯¦ä½ï¼éæ§ææ¸¬è©¦å ¨é¨å£æ
- åå : æ¸¬è©¦ç§ææ¹æ³ãmock 太細ãé©èå §é¨çæ
- çç:
- æ¹äºä¸è¡ç¨å¼ç¢¼ï¼10 忏¬è©¦å¤±æ
- æ¸¬è©¦æªæ¡æ¯ç¨å¼ç¢¼éé·
- éæ§æè±æ´å¤æé修測試
- 檢測:
expect.*\.toHaveBeenCalledTimes\(\d{2,}\)|mock.*private|spy.*internal - è§£æ³: æ¸¬è©¦å ¬é API/è¡çºãä½¿ç¨ black-box testingãæ¸å° mock æ¸é
SE-2: å齿§æ¸¬è©¦ (False Positive)
- å´é度: critical
- æ å¢: 測試永é ééï¼ä½å¯¦é䏿²æé©è任使±è¥¿
- åå : å¿è¨ awaitãexpect æ²æå·è¡ãæ¢ä»¶å¤æ·é¯èª¤
- çç:
- 測試ééä½ bug ä»ç¶åå¨
- åªææ¸¬è©¦ä¸çééµ assertion æ¸¬è©¦éæ¯éé
- Coverage é«ä½ä¿¡å¿ä½
- 檢測:
it\(.*\{\s*\}\)|expect\(.*\)(?!\.)|\.resolves(?!\.)|\.rejects(?!\.) - è§£æ³: TDDï¼å 寫失æç測試ï¼ãreview 測試ç¨å¼ç¢¼ãä½¿ç¨ ESLint no-floating-promises
SE-3: Flaky Testsï¼ä¸ç©©å®æ¸¬è©¦ï¼
- å´é度: high
- æ å¢: 測試ææé鿿失æï¼æ²æç¨å¼ç¢¼è®æ´
- åå : ä¾è³´æéãä¾è³´å¤é¨æåãç«¶æ æ¢ä»¶ãå ±äº«çæ
- çç:
- CI éè¦ retry æè½éé
- æ¬å°ééä½ CI 失æ
- åééå§å¿½ç¥å¤±æç測試
- 檢測:
new Date\(\)|Date\.now\(\)|setTimeout.*\d{4,}|sleep\(\d+\) - è§£æ³: ä½¿ç¨ fake timersãé颿¸¬è©¦çæ ãé¿å hard-coded delaysãmock å¤é¨ä¾è³´
SE-4: 測試éåå¡åç½®
- å´é度: medium
- æ å¢: E2E 測試太å¤ï¼å®å 測試太å°ï¼CI è¶ æ ¢
- åå : ãE2E æ¸¬è©¦æ´æ¥è¿ç實ãç誤解ã䏿³å¯«å®å 測試
- çç:
- CI è· 30+ åé
- 測試失æé£ä»¥å®ä½åé¡
- E2E 測試ç¶å¸¸ flaky
- 檢測:
describe.*E2E|playwright.*test|cypress.*it(æ¸éé è¶ unit test) - è§£æ³: éµå¾ª 70% unit / 20% integration / 10% E2E æ¯ä¾ãE2E åªæ¸¬ééµè·¯å¾
SE-5: é度 Mocking
- å´é度: medium
- æ å¢: Mock 太å¤å°è´æ¸¬è©¦å¤±å»æç¾©ï¼åªæ¯å¨æ¸¬è©¦ mock
- åå : çºäºéé¢è mock ææä¾è³´ã測試å·è¡æéç¦æ ®
- çç:
- 測試éé使´åæå¤±æ
- Mock çè¡çºèç實è¡çºä¸ç¬¦
- æ´æ°ä¾è³´å¾ mock éæ
- 檢測:
jest\.mock.*jest\.mock.*jest\.mock|mock\(.*\).*mock\(.*\).*mock\( - è§£æ³: åª mock å¤é¨ä¾è³´ï¼ç¶²è·¯ãæªæ¡ç³»çµ±ï¼ã使ç¨ç實ç in-memory 實ä½ã寫æ´å¤æ´å測試
Validations
V-1: ç¦æ¢ç©ºç測試
- é¡å: regex
- å´é度: critical
- 模å¼:
(it|test)\s*\([^)]+,\s*(async\s*)?\(\)\s*=>\s*\{\s*\}\s*\) - è¨æ¯: Empty test detected – test has no assertions
- 修復建è°: Add meaningful assertions with expect()
- é©ç¨:
*.test.ts,*.test.js,*.spec.ts,*.spec.js
V-2: æ¸¬è©¦ç¼ºå° assertion
- é¡å: regex
- å´é度: high
- 模å¼:
(it|test)\s*\([^)]+,\s*(async\s*)?\(\)\s*=>\s*\{[^}]*\}(?![^}]*expect) - è¨æ¯: Test without expect() assertion may be a false positive
- 修復建è°: Add at least one expect() assertion
- é©ç¨:
*.test.ts,*.test.js,*.spec.ts,*.spec.js
V-3: ç¦æ¢ fit/fdescribe (focused tests)
- é¡å: regex
- å´é度: critical
- 模å¼:
\b(fit|fdescribe|it\.only|describe\.only|test\.only)\s*\( - è¨æ¯: Focused test will skip other tests in CI
- 修復建è°: Remove
fprefix or.onlybefore committing - é©ç¨:
*.test.ts,*.test.js,*.spec.ts,*.spec.js
V-4: ç¦æ¢ skip tests ç¡èªªæ
- é¡å: regex
- å´é度: medium
- 模å¼:
(xit|xdescribe|it\.skip|describe\.skip|test\.skip)\s*\([^)]+\) - è¨æ¯: Skipped test without documented reason
- 修復建è°: Add comment explaining why test is skipped and tracking issue
- é©ç¨:
*.test.ts,*.test.js,*.spec.ts,*.spec.js
V-5: 測試ä¸ä½¿ç¨ setTimeout
- é¡å: regex
- å´é度: high
- 模å¼:
setTimeout\s*\(\s*[^,]+,\s*\d{3,}\s*\) - è¨æ¯: Hard-coded delays in tests cause flakiness and slow tests
- 修復建è°: Use
jest.useFakeTimers()orwaitFor()from testing-library - é©ç¨:
*.test.ts,*.test.js,*.spec.ts,*.spec.js