testing

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Testing

测试

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

技能能力范围

  1. Test Strategy: Designs comprehensive testing strategies (unit, integration, E2E)
  2. Test Automation: Sets up test frameworks and automation tools
  3. TDD Methodology: Implements Test-Driven Development workflows (Red-Green-Refactor)
  4. Test Writing: Writes focused, maintainable tests with proper patterns
  5. Coverage Analysis: Analyzes and improves test coverage
  6. CI/CD Integration: Integrates tests into continuous integration pipelines
  7. Web App Testing: Tests web applications using Playwright
  8. Test Quality: Improves test reliability and maintainability
  1. 测试策略:设计全面的测试策略(单元、集成、端到端)
  2. 测试自动化:搭建测试框架与自动化工具
  3. TDD方法论:落地测试驱动开发工作流(红-绿-重构循环)
  4. 测试编写:遵循规范编写聚焦、可维护的测试用例
  5. 覆盖率分析:分析并优化测试覆盖率
  6. CI/CD集成:将测试集成到持续集成流水线
  7. Web应用测试:使用Playwright测试Web应用
  8. 测试质量:提升测试的可靠性与可维护性

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)

三大铁律(绝不能违反)

  1. Write NO production code without a failing test first
  2. Write only enough test to demonstrate one failure
  3. Write only enough code to pass that test
  1. 没有失败的测试,绝不写生产代码
  2. 只编写足够的测试代码,刚好能触发一个失败
  3. 只编写足够的生产代码,刚好能让测试通过

Red-Green-Refactor Cycle

红-绿-重构循环

Phase 1: RED - Write Failing Test
  1. Write ONE test that defines desired behavior
  2. Run test - verify it FAILS
  3. Verify it fails for the RIGHT reason (not syntax error)
  4. DO NOT write implementation yet
Phase 2: GREEN - Minimal Implementation
  1. Write MINIMAL code to make test pass
  2. Resist urge to add extra features
  3. Run test - verify it PASSES
  4. If test still fails, fix implementation (not test)
Phase 3: REFACTOR - Clean Code
  1. Remove code duplication (DRY)
  2. Improve naming for clarity
  3. Extract complex logic into functions
  4. Run ALL tests - must stay green throughout
  5. Check test coverage on changed lines
After REFACTOR, start new RED phase for next behavior.
阶段1:红 - 编写失败的测试
  1. 编写一个测试,定义期望的功能行为
  2. 运行测试 - 确认测试失败
  3. 验证失败原因正确(不是语法错误)
  4. 此时不要编写实现代码
阶段2:绿 - 最小化实现
  1. 编写最少的代码让测试通过
  2. 克制添加额外功能的冲动
  3. 运行测试 - 确认测试通过
  4. 如果测试仍失败,修复实现代码(而非修改测试)
阶段3:重构 - 优化代码
  1. 消除代码重复(遵循DRY原则)
  2. 优化命名以提升可读性
  3. 将复杂逻辑提取为独立函数
  4. 运行所有测试 - 全程必须保持测试通过
  5. 检查变更代码的测试覆盖率
完成重构后,进入新的“红”阶段开发下一个功能。

Test Writing Patterns

测试编写模式

Arrange-Act-Assert (AAA)

准备-执行-断言(AAA)

Structure:
  1. Arrange: Set up test data and conditions
  2. Act: Execute the code being tested
  3. 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);
  });
});
结构:
  1. 准备(Arrange):设置测试数据与前置条件
  2. 执行(Act):运行待测试的代码
  3. 断言(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:
  1. Given: Initial context/preconditions
  2. When: Action/event that triggers behavior
  3. Then: Expected outcome
结构:
  1. 给定(Given):初始上下文/前置条件
  2. 当(When):触发行为的动作/事件
  3. 则(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.js

Coverage 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:
  1. Unit Tests: Fast feedback, run on every commit
  2. Integration Tests: Run on pull requests
  3. E2E Tests: Run before merging to main
  4. Performance Tests: Run on main branch
Quality Gates:
  • All tests must pass
  • Coverage must meet threshold
  • No critical security issues
  • Performance benchmarks met
阶段:
  1. 单元测试:反馈速度快,每次提交都运行
  2. 集成测试:在拉取请求时运行
  3. 端到端测试:合并到主分支前运行
  4. 性能测试:在主分支上运行
质量门禁:
  • 所有测试必须通过
  • 覆盖率必须达到阈值
  • 无严重安全问题
  • 满足性能基准要求

Web Application Testing with Playwright

使用Playwright进行Web应用测试

Helper Scripts

辅助脚本

This skill includes Python helper scripts in
scripts/
:
  • with_server.py
    - Manages server lifecycle (supports multiple servers). Always run with
    --help
    first to see usage.
    bash
    # 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
本技能在
scripts/
目录下提供Python辅助脚本:
  • with_server.py
    - 管理服务生命周期(支持多服务)。使用前请先运行
    --help
    查看使用说明。
    bash
    # 单服务场景
    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
    --help
    to see usage, then invoke directly
  • Use
    sync_playwright()
    for synchronous scripts
  • Always close the browser when done
  • Use descriptive selectors:
    text=
    ,
    role=
    , CSS selectors, or IDs
  • Add appropriate waits:
    page.wait_for_selector()
    or
    page.wait_for_timeout()
  • CRITICAL: Wait for
    page.wait_for_load_state('networkidle')
    before inspection on dynamic apps
  • 将捆绑脚本视为黑盒 - 使用
    --help
    查看用法后直接调用
  • 同步脚本使用
    sync_playwright()
  • 测试完成后始终关闭浏览器
  • 使用语义化选择器:
    text=
    role=
    、CSS选择器或ID
  • 添加合适的等待:
    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
examples/
directory for:
  • element_discovery.py
    - Discovering buttons, links, and inputs on a page
  • static_html_automation.py
    - Using file:// URLs for local HTML
  • console_logging.py
    - Capturing console logs during automation
请查看
examples/
目录下的示例:
  • element_discovery.py
    - 发现页面上的按钮、链接和输入框
  • static_html_automation.py
    - 使用file:// URL测试本地HTML文件
  • console_logging.py
    - 自动化过程中捕获控制台日志

Reference Files

参考文件

For detailed testing patterns and workflows, load reference files as needed:
  • references/framework_workflows.md
    - Framework-specific TDD workflows and examples for Python (pytest), JavaScript (Jest, Vitest), Java (JUnit), Go, Rust
  • references/test_patterns.md
    - Common test patterns, test organization, naming conventions, test doubles (mocks, stubs, spies), parametrization, and anti-patterns
  • references/webapp_testing.md
    - Web application testing patterns, Playwright best practices, and E2E testing strategies
  • references/TESTING_REPORT.template.md
    - Test quality report template with coverage metrics, audit findings, and recommendations
When working with specific frameworks or need detailed patterns, load the appropriate reference file.
如需了解详细的测试模式与工作流,可按需加载以下参考文件:
  • references/framework_workflows.md
    - 特定框架的TDD工作流与示例,涵盖Python(pytest)、JavaScript(Jest、Vitest)、Java(JUnit)、Go、Rust
  • references/test_patterns.md
    - 通用测试模式、测试组织方式、命名规范、测试替身(模拟桩、存根、间谍)、参数化测试及反模式
  • references/webapp_testing.md
    - Web应用测试模式、Playwright最佳实践以及端到端测试策略
  • references/TESTING_REPORT.template.md
    - 测试质量报告模板,包含覆盖率指标、审计结果与优化建议
当使用特定框架或需要详细模式指导时,加载对应的参考文件即可。

Best Practices

最佳实践

Test Quality

测试质量

  1. Isolation: Tests should be independent and runnable in any order
  2. Deterministic: Tests should produce consistent results
  3. Fast: Unit tests should run quickly (< 100ms each)
  4. Clear: Test names should describe what they test
  5. Maintainable: Tests should be easy to update when code changes
  1. 隔离性:测试用例应相互独立,可按任意顺序运行
  2. 确定性:测试应产生一致的结果
  3. 高效性:单元测试应快速执行(每个测试耗时<100ms)
  4. 清晰性:测试名称应准确描述其测试的行为
  5. 可维护性:当代码变更时,测试应易于更新

TDD Best Practices

TDD最佳实践

  1. One Behavior Per Test: Each test verifies ONE behavior
  2. Descriptive Names: Test names describe the behavior being tested
  3. Independent Tests: Tests don't depend on each other
  4. Fast Tests: Mock external dependencies to keep tests fast
  5. Clear Assertions: Assertions clearly show what's being verified
  1. 一个测试对应一个行为:每个测试仅验证一个行为
  2. 命名描述性:测试名称需清晰描述所测试的行为
  3. 测试独立性:测试用例之间无依赖
  4. 测试高效性:通过模拟外部依赖保持测试速度
  5. 断言清晰性:断言需明确展示验证的内容

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是一种设计技术,而非单纯的测试技术。

循环流程始终不变:红 → 绿 → 重构 → 重复

先写测试会迫使你思考:
- 我需要实现什么行为?
- 如何验证功能正常工作?
- 最简单的实现方式是什么?

这会产出设计更优、可维护性更强的代码。