AgentSkillsCN

testing

在编写测试用例、设计测试策略,或验证 AI 生成的代码时,此功能同样不可或缺。

SKILL.md
--- frontmatter
name: testing
description: Use when writing tests, designing test strategy, or verifying AI-generated code

Testing

Announce at start: "Following the testing skill for test design."

Core Rule

Test behavior, not implementation. Tests must survive refactoring.

Test Pyramid

Write tests in this ratio:

LayerQuantitySpeedFocus
UnitMany (1000s)Fast (ms)Individual functions/classes
IntegrationSome (100s)Medium (s)Component interactions
E2EFew (10s)Slow (min)Full user flows

Writing Tests

1. Structure: Arrange-Act-Assert

Every test follows AAA:

python
def test_order_total_includes_tax():
    # Arrange
    order = Order(items=[Item(price=100)])
    tax = TaxCalculator(rate=0.08)

    # Act
    total = order.calculate_total(tax)

    # Assert
    assert total == 108.00

2. Naming: Describe Behavior

PatternExample
test_<unit>_<scenario>test_login_with_invalid_password
test_<unit>_<scenario>_<expected>test_discount_exceeds_max_caps_at_max

3. Data: Explicit, Not Magic

  • Use explicit test data in each test
  • Avoid shared fixtures with hidden setup
  • Use builder patterns for complex objects

Coverage Priorities

PriorityWhat to Test
HighBusiness logic, data mutations, security/auth, payments
MediumUtility functions, error handling, integration points
LowGenerated code, configuration, UI styling

Critical paths (payments, auth, data mutations) always require 100% coverage.

AI-Specific Rules

  • Verify AI-generated tests actually assert something meaningful
  • Check that tests fail when the code is wrong (not just pass when right)
  • Use fakes over mocks where possible — more realistic behavior
  • Never test private methods directly
  • Never share mutable state between tests

Bug Fix Tests

Every bug fix must include a regression test that:

  1. Fails before the fix (proves the bug)
  2. Passes after the fix (proves the fix)
  3. Prevents recurrence

Related Skills

WhenInvoke
Test reveals a bugdebugging
Need E2E tests specificallye2e-testing
Tests pass, ready to submitpr-writing
Code needs review before testscode-review

Deep Reference

For principles, rationale, anti-patterns, and examples:

  • guides/testing-strategy/testing-strategy.md
  • guides/testing-ai-code/testing-ai-code.md