test-quality-analysis by secondsky/claude-skills
npx skills add https://github.com/secondsky/claude-skills --skill test-quality-analysis用于分析和改进测试质量的专家知识——检测测试异味、过度模拟、覆盖率不足和测试反模式。
问题 : 模拟过多依赖项会使测试变得脆弱。
// ❌ 错误: 过度模拟
test('calculate total', () => {
const mockAdd = vi.fn(() => 10)
const mockMultiply = vi.fn(() => 20)
// Testing implementation, not behavior
})
// ✅ 正确: 仅模拟外部依赖
test('calculate order total', () => {
const mockPricingAPI = vi.fn(() => ({ tax: 0.1 }))
const total = calculateTotal(order, mockPricingAPI)
expect(total).toBe(38)
})
检测 : 超过 3-4 个模拟、模拟纯函数、复杂的模拟设置。
修复 : 仅模拟 I/O 边界(API、数据库、文件系统)。
问题 : 测试因不相关的代码更改而失败。
// ❌ 错误: 测试实现细节
await page.locator('.form-container > div:nth-child(2) > button').click()
// ✅ 正确: 语义化选择器
await page.getByRole('button', { name: 'Submit' }).click()
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
问题 : 测试非确定性地通过或失败。
// ❌ 错误: 竞态条件
test('loads data', async () => {
fetchData()
await new Promise(resolve => setTimeout(resolve, 1000))
expect(data).toBeDefined()
})
// ✅ 正确: 正确的异步处理
test('loads data', async () => {
const data = await fetchData()
expect(data).toBeDefined()
})
// ❌ 错误: 弱断言
test('returns users', async () => {
const users = await getUsers()
expect(users).toBeDefined() // 太模糊了!
})
// ✅ 正确: 强而具体的断言
test('creates user with correct attributes', async () => {
const user = await createUser({ name: 'John' })
expect(user).toMatchObject({
id: expect.any(Number),
name: 'John',
})
})
# Vitest coverage (prefer bun)
bun test --coverage
open coverage/index.html
# Check thresholds
bun test --coverage --coverage.thresholds.lines=80
# pytest-cov (Python)
uv run pytest --cov --cov-report=html
open htmlcov/index.html
test('user registration', async () => {
// 准备
const userData = { email: 'user@example.com' }
// 执行
const user = await registerUser(userData)
// 断言
expect(user.email).toBe('user@example.com')
})
// ❌ 错误
const spy = vi.spyOn(Math, 'sqrt')
calculateDistance()
expect(spy).toHaveBeenCalled() // 测试如何做,而非做什么
// ✅ 正确
const distance = calculateDistance({ x: 0, y: 0 }, { x: 3, y: 4 })
expect(distance).toBe(5) // 测试输出
// ❌ 错误
const mockAdd = vi.fn((a, b) => a + b)
// ✅ 正确: 使用真实实现
import { add } from './utils'
// 仅模拟外部服务
const mockPaymentGateway = vi.fn()
vitest-testing - TypeScript/JavaScript 测试playwright-testing - E2E 测试mutation-testing - 验证测试有效性每周安装次数
81
仓库
GitHub 星标数
93
首次出现
Jan 25, 2026
安全审计
安装于
claude-code70
codex67
gemini-cli66
opencode66
cursor66
github-copilot63
Expert knowledge for analyzing and improving test quality - detecting test smells, overmocking, insufficient coverage, and testing anti-patterns.
Problem : Mocking too many dependencies makes tests fragile.
// ❌ BAD: Overmocked
test('calculate total', () => {
const mockAdd = vi.fn(() => 10)
const mockMultiply = vi.fn(() => 20)
// Testing implementation, not behavior
})
// ✅ GOOD: Mock only external dependencies
test('calculate order total', () => {
const mockPricingAPI = vi.fn(() => ({ tax: 0.1 }))
const total = calculateTotal(order, mockPricingAPI)
expect(total).toBe(38)
})
Detection : More than 3-4 mocks, mocking pure functions, complex mock setup.
Fix : Mock only I/O boundaries (APIs, databases, filesystem).
Problem : Tests break with unrelated code changes.
// ❌ BAD: Tests implementation details
await page.locator('.form-container > div:nth-child(2) > button').click()
// ✅ GOOD: Semantic selector
await page.getByRole('button', { name: 'Submit' }).click()
Problem : Tests pass or fail non-deterministically.
// ❌ BAD: Race condition
test('loads data', async () => {
fetchData()
await new Promise(resolve => setTimeout(resolve, 1000))
expect(data).toBeDefined()
})
// ✅ GOOD: Proper async handling
test('loads data', async () => {
const data = await fetchData()
expect(data).toBeDefined()
})
// ❌ BAD: Weak assertion
test('returns users', async () => {
const users = await getUsers()
expect(users).toBeDefined() // Too vague!
})
// ✅ GOOD: Strong, specific assertions
test('creates user with correct attributes', async () => {
const user = await createUser({ name: 'John' })
expect(user).toMatchObject({
id: expect.any(Number),
name: 'John',
})
})
# Vitest coverage (prefer bun)
bun test --coverage
open coverage/index.html
# Check thresholds
bun test --coverage --coverage.thresholds.lines=80
# pytest-cov (Python)
uv run pytest --cov --cov-report=html
open htmlcov/index.html
test('user registration', async () => {
// Arrange
const userData = { email: 'user@example.com' }
// Act
const user = await registerUser(userData)
// Assert
expect(user.email).toBe('user@example.com')
})
// ❌ BAD
const spy = vi.spyOn(Math, 'sqrt')
calculateDistance()
expect(spy).toHaveBeenCalled() // Testing how, not what
// ✅ GOOD
const distance = calculateDistance({ x: 0, y: 0 }, { x: 3, y: 4 })
expect(distance).toBe(5) // Testing output
// ❌ BAD
const mockAdd = vi.fn((a, b) => a + b)
// ✅ GOOD: Use real implementations
import { add } from './utils'
// Only mock external services
const mockPaymentGateway = vi.fn()
vitest-testing - TypeScript/JavaScript testingplaywright-testing - E2E testingmutation-testing - Validate test effectivenessWeekly Installs
81
Repository
GitHub Stars
93
First Seen
Jan 25, 2026
Security Audits
Gen Agent Trust HubFailSocketPassSnykPass
Installed on
claude-code70
codex67
gemini-cli66
opencode66
cursor66
github-copilot63
后端测试指南:API端点、业务逻辑与数据库测试最佳实践
11,800 周安装