testing
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseTesting
测试
This skill provides comprehensive testing capabilities including test strategy, automation setup, Test-Driven Development (TDD), test writing best practices, coverage analysis, CI/CD integration, and web application testing with Playwright.
本技能提供全面的测试能力,包括测试策略制定、自动化环境搭建、测试驱动开发(TDD)、测试用例编写最佳实践、覆盖率分析、CI/CD集成,以及使用Playwright进行Web应用测试。
When to Use This Skill
适用场景
- When setting up test infrastructure for a project
- When creating test strategies and test plans
- When writing unit, integration, or E2E tests
- When implementing TDD/test-first development
- When analyzing test coverage and quality
- When integrating tests into CI/CD pipelines
- When testing web applications with Playwright
- When debugging test failures or improving test reliability
- When writing test fixtures, mock data, or factory functions
- When mocking external dependencies (APIs, databases, file systems)
- When organizing test file structure and test suites
- When testing async code, Promises, or event-driven behavior
- When implementing snapshot tests for UI components
- When configuring test coverage thresholds
- 为项目搭建测试基础设施时
- 制定测试策略与测试计划时
- 编写单元测试、集成测试或端到端(E2E)测试时
- 落地测试驱动开发(TDD)/测试先行开发模式时
- 分析测试覆盖率与测试质量时
- 将测试集成到CI/CD流水线时
- 使用Playwright测试Web应用时
- 调试测试失败问题或提升测试可靠性时
- 编写测试夹具、模拟数据或工厂函数时
- 模拟外部依赖(API、数据库、文件系统)时
- 规划测试文件结构与测试套件时
- 测试异步代码、Promises或事件驱动行为时
- 为UI组件实现快照测试时
- 配置测试覆盖率阈值时
What This Skill Does
技能能力范围
- Test Strategy: Designs comprehensive testing strategies (unit, integration, E2E)
- Test Automation: Sets up test frameworks and automation tools
- TDD Methodology: Implements Test-Driven Development workflows (Red-Green-Refactor)
- Test Writing: Writes focused, maintainable tests with proper patterns
- Coverage Analysis: Analyzes and improves test coverage
- CI/CD Integration: Integrates tests into continuous integration pipelines
- Web App Testing: Tests web applications using Playwright
- Test Quality: Improves test reliability and maintainability
- 测试策略:设计全面的测试策略(单元、集成、端到端)
- 测试自动化:搭建测试框架与自动化工具
- TDD方法论:落地测试驱动开发工作流(红-绿-重构循环)
- 测试编写:遵循规范编写聚焦、可维护的测试用例
- 覆盖率分析:分析并优化测试覆盖率
- CI/CD集成:将测试集成到持续集成流水线
- Web应用测试:使用Playwright测试Web应用
- 测试质量:提升测试的可靠性与可维护性
Test Strategy
测试策略
Test Pyramid
测试金字塔
Recommended Distribution:
- Unit Tests: 70% - Fast, isolated, test individual functions
- Integration Tests: 20% - Test component interactions
- E2E Tests: 10% - Test complete user workflows
Test Types:
- Functional tests (happy path, edge cases, error handling)
- Non-functional tests (performance, security, accessibility)
- Regression tests (prevent breaking changes)
- Smoke tests (critical path verification)
推荐分布比例:
- 单元测试:70% - 执行速度快、隔离性强,测试独立函数
- 集成测试:20% - 测试组件间的交互逻辑
- 端到端测试:10% - 测试完整的用户流程
测试类型:
- 功能测试(正常流程、边缘场景、错误处理)
- 非功能测试(性能、安全、可访问性)
- 回归测试(防止代码变更引入问题)
- 冒烟测试(验证核心流程可用性)
Framework Selection
框架选择
JavaScript/TypeScript:
- Jest, Vitest, Mocha for unit/integration
- Playwright, Cypress for E2E
- React Testing Library for component testing
Python:
- pytest for unit/integration
- Selenium, Playwright for E2E
- unittest for standard library testing
Java:
- JUnit for unit tests
- TestNG for integration
- Selenium for E2E
Go:
- Built-in testing package
- Testify for assertions
Rust:
- Built-in test framework
- Cargo test for running tests
JavaScript/TypeScript:
- Jest、Vitest、Mocha 用于单元/集成测试
- Playwright、Cypress 用于端到端测试
- React Testing Library 用于组件测试
Python:
- pytest 用于单元/集成测试
- Selenium、Playwright 用于端到端测试
- unittest 用于标准库测试
Java:
- JUnit 用于单元测试
- TestNG 用于集成测试
- Selenium 用于端到端测试
Go:
- 内置测试包
- Testify 用于断言
Rust:
- 内置测试框架
- 使用Cargo test 运行测试
Test-Driven Development (TDD)
测试驱动开发(TDD)
TDD is a design technique, not just a testing technique. It produces better-designed, more maintainable code through small, disciplined steps.
TDD是一种设计技术,而非单纯的测试技术。它通过小步、规范的流程产出设计更优、可维护性更强的代码。
Core Principle
核心原则
Write tests before code. Always. TDD forces you to think about:
- What behavior do I need?
- How will I know it works?
- What's the simplest implementation?
永远先写测试,再写生产代码。 TDD会促使你思考:
- 我需要实现什么功能?
- 如何验证功能正常工作?
- 最简单的实现方式是什么?
The Three Laws (Never Violate)
三大铁律(绝不能违反)
- Write NO production code without a failing test first
- Write only enough test to demonstrate one failure
- Write only enough code to pass that test
- 没有失败的测试,绝不写生产代码
- 只编写足够的测试代码,刚好能触发一个失败
- 只编写足够的生产代码,刚好能让测试通过
Red-Green-Refactor Cycle
红-绿-重构循环
Phase 1: RED - Write Failing Test
- Write ONE test that defines desired behavior
- Run test - verify it FAILS
- Verify it fails for the RIGHT reason (not syntax error)
- DO NOT write implementation yet
Phase 2: GREEN - Minimal Implementation
- Write MINIMAL code to make test pass
- Resist urge to add extra features
- Run test - verify it PASSES
- If test still fails, fix implementation (not test)
Phase 3: REFACTOR - Clean Code
- Remove code duplication (DRY)
- Improve naming for clarity
- Extract complex logic into functions
- Run ALL tests - must stay green throughout
- Check test coverage on changed lines
After REFACTOR, start new RED phase for next behavior.
阶段1:红 - 编写失败的测试
- 编写一个测试,定义期望的功能行为
- 运行测试 - 确认测试失败
- 验证失败原因正确(不是语法错误)
- 此时不要编写实现代码
阶段2:绿 - 最小化实现
- 编写最少的代码让测试通过
- 克制添加额外功能的冲动
- 运行测试 - 确认测试通过
- 如果测试仍失败,修复实现代码(而非修改测试)
阶段3:重构 - 优化代码
- 消除代码重复(遵循DRY原则)
- 优化命名以提升可读性
- 将复杂逻辑提取为独立函数
- 运行所有测试 - 全程必须保持测试通过
- 检查变更代码的测试覆盖率
完成重构后,进入新的“红”阶段开发下一个功能。
Test Writing Patterns
测试编写模式
Arrange-Act-Assert (AAA)
准备-执行-断言(AAA)
Structure:
- Arrange: Set up test data and conditions
- Act: Execute the code being tested
- Assert: Verify the expected outcome
Example:
javascript
describe('UserService', () => {
it('should create user with valid data', async () => {
// Arrange
const userData = { email: 'test@example.com', name: 'Test User' };
// Act
const result = await userService.createUser(userData);
// Assert
expect(result).toHaveProperty('id');
expect(result.email).toBe(userData.email);
});
});结构:
- 准备(Arrange):设置测试数据与前置条件
- 执行(Act):运行待测试的代码
- 断言(Assert):验证预期结果
示例:
javascript
describe('UserService', () => {
it('should create user with valid data', async () => {
// Arrange
const userData = { email: 'test@example.com', name: 'Test User' };
// Act
const result = await userService.createUser(userData);
// Assert
expect(result).toHaveProperty('id');
expect(result.email).toBe(userData.email);
});
});Given-When-Then (BDD Style)
给定-当-则(BDD风格)
Structure:
- Given: Initial context/preconditions
- When: Action/event that triggers behavior
- Then: Expected outcome
结构:
- 给定(Given):初始上下文/前置条件
- 当(When):触发行为的动作/事件
- 则(Then):预期的结果
Test Organization
测试组织
File Structure:
project/
├── src/
│ └── components/
│ └── User.jsx
├── tests/
│ ├── unit/
│ │ └── User.test.jsx
│ ├── integration/
│ │ └── UserAPI.test.js
│ └── e2e/
│ └── user-flow.spec.js
├── jest.config.js
└── playwright.config.js文件结构:
project/
├── src/
│ └── components/
│ └── User.jsx
├── tests/
│ ├── unit/
│ │ └── User.test.jsx
│ ├── integration/
│ │ └── UserAPI.test.js
│ └── e2e/
│ └── user-flow.spec.js
├── jest.config.js
└── playwright.config.jsCoverage Analysis
覆盖率分析
Coverage Goals
覆盖率目标
Recommended Thresholds:
- Lines: 80%+
- Functions: 80%+
- Branches: 80%+
- Statements: 80%+
Critical Paths:
- Always aim for 100% coverage on critical business logic
- Authentication and authorization
- Payment processing
- Data validation
推荐阈值:
- 代码行:80%+
- 函数:80%+
- 分支:80%+
- 语句:80%+
核心路径:
- 核心业务逻辑始终追求100%覆盖率
- 认证与授权模块
- 支付处理流程
- 数据验证逻辑
Coverage Gaps
覆盖率缺口
Common Gaps:
- Error handling paths
- Edge cases
- Boundary conditions
- Integration points
Improvement Strategies:
- Identify untested code paths
- Add tests for error scenarios
- Test edge cases and boundaries
- Increase integration test coverage
常见缺口:
- 错误处理路径
- 边缘场景
- 边界条件
- 集成点
优化策略:
- 识别未测试的代码路径
- 为错误场景添加测试用例
- 测试边缘场景与边界条件
- 提升集成测试覆盖率
CI/CD Integration
CI/CD集成
Test Pipeline
测试流水线
Stages:
- Unit Tests: Fast feedback, run on every commit
- Integration Tests: Run on pull requests
- E2E Tests: Run before merging to main
- Performance Tests: Run on main branch
Quality Gates:
- All tests must pass
- Coverage must meet threshold
- No critical security issues
- Performance benchmarks met
阶段:
- 单元测试:反馈速度快,每次提交都运行
- 集成测试:在拉取请求时运行
- 端到端测试:合并到主分支前运行
- 性能测试:在主分支上运行
质量门禁:
- 所有测试必须通过
- 覆盖率必须达到阈值
- 无严重安全问题
- 满足性能基准要求
Web Application Testing with Playwright
使用Playwright进行Web应用测试
Helper Scripts
辅助脚本
This skill includes Python helper scripts in :
scripts/-
- Manages server lifecycle (supports multiple servers). Always run with
with_server.pyfirst to see usage.--helpbash# Single server python scripts/with_server.py --server "npm run dev" --port 5173 -- python your_automation.py # Multiple servers (e.g., backend + frontend) python scripts/with_server.py \ --server "cd backend && python server.py" --port 3000 \ --server "cd frontend && npm run dev" --port 5173 \ -- python your_automation.py
本技能在目录下提供Python辅助脚本:
scripts/-
- 管理服务生命周期(支持多服务)。使用前请先运行
with_server.py查看使用说明。--helpbash# 单服务场景 python scripts/with_server.py --server "npm run dev" --port 5173 -- python your_automation.py # 多服务场景(如后端+前端) python scripts/with_server.py \ --server "cd backend && python server.py" --port 3000 \ --server "cd frontend && npm run dev" --port 5173 \ -- python your_automation.py
Decision Tree: Choosing Your Approach
决策树:选择测试方案
User task → Is it static HTML?
├─ Yes → Read HTML file directly to identify selectors
│ ├─ Success → Write Playwright script using selectors
│ └─ Fails/Incomplete → Treat as dynamic (below)
│
└─ No (dynamic webapp) → Is the server already running?
├─ No → Run: python scripts/with_server.py --help
│ Then use the helper + write simplified Playwright script
│
└─ Yes → Reconnaissance-then-action:
1. Navigate and wait for networkidle
2. Take screenshot or inspect DOM
3. Identify selectors from rendered state
4. Execute actions with discovered selectors用户任务 → 是否为静态HTML?
├─ 是 → 直接读取HTML文件定位选择器
│ ├─ 成功 → 使用选择器编写Playwright脚本
│ └─ 失败/不完整 → 按动态应用处理(见下方)
│
└─ 否(动态Web应用) → 服务是否已启动?
├─ 否 → 运行:python scripts/with_server.py --help
│ 然后使用辅助脚本+编写简化的Playwright脚本
│
└─ 是 → 先侦察再执行:
1. 导航到页面并等待networkidle状态
2. 截图或检查DOM结构
3. 从渲染状态中定位选择器
4. 使用找到的选择器执行操作Playwright Best Practices
Playwright最佳实践
- Use bundled scripts as black boxes - Use to see usage, then invoke directly
--help - Use for synchronous scripts
sync_playwright() - Always close the browser when done
- Use descriptive selectors: ,
text=, CSS selectors, or IDsrole= - Add appropriate waits: or
page.wait_for_selector()page.wait_for_timeout() - CRITICAL: Wait for before inspection on dynamic apps
page.wait_for_load_state('networkidle')
- 将捆绑脚本视为黑盒 - 使用查看用法后直接调用
--help - 同步脚本使用
sync_playwright() - 测试完成后始终关闭浏览器
- 使用语义化选择器:、
text=、CSS选择器或IDrole= - 添加合适的等待:或
page.wait_for_selector()page.wait_for_timeout() - 关键注意事项:在动态应用中,检查DOM前需等待
page.wait_for_load_state('networkidle')
Example: Basic Playwright Script
示例:基础Playwright脚本
python
from playwright.sync_api import sync_playwright
with sync_playwright() as p:
browser = p.chromium.launch(headless=True)
page = browser.new_page()
page.goto('http://localhost:5173')
page.wait_for_load_state('networkidle') # CRITICAL: Wait for JS to execute
# ... your automation logic
browser.close()python
from playwright.sync_api import sync_playwright
with sync_playwright() as p:
browser = p.chromium.launch(headless=True)
page = browser.new_page()
page.goto('http://localhost:5173')
page.wait_for_load_state('networkidle') # 关键:等待JS执行完成
# ... 你的自动化逻辑
browser.close()Examples
示例代码
See directory for:
examples/- - Discovering buttons, links, and inputs on a page
element_discovery.py - - Using file:// URLs for local HTML
static_html_automation.py - - Capturing console logs during automation
console_logging.py
请查看目录下的示例:
examples/- - 发现页面上的按钮、链接和输入框
element_discovery.py - - 使用file:// URL测试本地HTML文件
static_html_automation.py - - 自动化过程中捕获控制台日志
console_logging.py
Reference Files
参考文件
For detailed testing patterns and workflows, load reference files as needed:
- - Framework-specific TDD workflows and examples for Python (pytest), JavaScript (Jest, Vitest), Java (JUnit), Go, Rust
references/framework_workflows.md - - Common test patterns, test organization, naming conventions, test doubles (mocks, stubs, spies), parametrization, and anti-patterns
references/test_patterns.md - - Web application testing patterns, Playwright best practices, and E2E testing strategies
references/webapp_testing.md - - Test quality report template with coverage metrics, audit findings, and recommendations
references/TESTING_REPORT.template.md
When working with specific frameworks or need detailed patterns, load the appropriate reference file.
如需了解详细的测试模式与工作流,可按需加载以下参考文件:
- - 特定框架的TDD工作流与示例,涵盖Python(pytest)、JavaScript(Jest、Vitest)、Java(JUnit)、Go、Rust
references/framework_workflows.md - - 通用测试模式、测试组织方式、命名规范、测试替身(模拟桩、存根、间谍)、参数化测试及反模式
references/test_patterns.md - - Web应用测试模式、Playwright最佳实践以及端到端测试策略
references/webapp_testing.md - - 测试质量报告模板,包含覆盖率指标、审计结果与优化建议
references/TESTING_REPORT.template.md
当使用特定框架或需要详细模式指导时,加载对应的参考文件即可。
Best Practices
最佳实践
Test Quality
测试质量
- Isolation: Tests should be independent and runnable in any order
- Deterministic: Tests should produce consistent results
- Fast: Unit tests should run quickly (< 100ms each)
- Clear: Test names should describe what they test
- Maintainable: Tests should be easy to update when code changes
- 隔离性:测试用例应相互独立,可按任意顺序运行
- 确定性:测试应产生一致的结果
- 高效性:单元测试应快速执行(每个测试耗时<100ms)
- 清晰性:测试名称应准确描述其测试的行为
- 可维护性:当代码变更时,测试应易于更新
TDD Best Practices
TDD最佳实践
- One Behavior Per Test: Each test verifies ONE behavior
- Descriptive Names: Test names describe the behavior being tested
- Independent Tests: Tests don't depend on each other
- Fast Tests: Mock external dependencies to keep tests fast
- Clear Assertions: Assertions clearly show what's being verified
- 一个测试对应一个行为:每个测试仅验证一个行为
- 命名描述性:测试名称需清晰描述所测试的行为
- 测试独立性:测试用例之间无依赖
- 测试高效性:通过模拟外部依赖保持测试速度
- 断言清晰性:断言需明确展示验证的内容
Common Mistakes to Avoid
常见错误规避
- ❌ Writing multiple tests at once (write one test at a time)
- ❌ Skipping refactor phase (always refactor after green)
- ❌ Implementation before test (delete code and start with test)
- ❌ Over-engineering in GREEN (simplest thing that passes)
- ❌ Writing test that passes immediately (must fail first)
- ❌ 同时编写多个测试(一次只写一个测试)
- ❌ 跳过重构阶段(测试通过后务必重构)
- ❌ 先写实现代码再写测试(删除代码,从测试开始)
- ❌ 在绿阶段过度设计(只写刚好能通过测试的最简代码)
- ❌ 编写一开始就通过的测试(测试必须先失败)
Test Maintenance
测试维护
- Review and update tests when requirements change
- Remove obsolete tests
- Refactor tests to reduce duplication
- Keep test data factories up to date
- Monitor test execution time
- 需求变更时,同步更新测试用例
- 删除过时的测试用例
- 重构测试以减少重复代码
- 保持测试数据工厂的更新
- 监控测试执行时间
Integration with Other Skills
与其他技能的集成
- debugging: Use when tests fail unexpectedly
- code-review: TDD produces code that's easier to review
- dead-code-removal: Tests help identify unused code
- performance: Use for performance testing strategies
- debugging:当测试意外失败时使用
- code-review:TDD产出的代码更易于评审
- dead-code-removal:测试可帮助识别未使用的代码
- performance:用于制定性能测试策略
Meta-Principle
核心原则
TDD is a DESIGN technique, not a testing technique.
The cycle never changes: RED → GREEN → REFACTOR → Repeat
Writing tests first forces you to think about:
- What behavior do I need?
- How will I know it works?
- What's the simplest implementation?
This produces better-designed, more maintainable code.TDD是一种设计技术,而非单纯的测试技术。
循环流程始终不变:红 → 绿 → 重构 → 重复
先写测试会迫使你思考:
- 我需要实现什么行为?
- 如何验证功能正常工作?
- 最简单的实现方式是什么?
这会产出设计更优、可维护性更强的代码。