AgentSkillsCN

testing

采用 TDD(红-绿-重构)模式与 AAA 测试范式编写测试用例。无论是新增功能、变更行为,还是修复缺陷,都应全面覆盖单元测试、集成测试以及端到端测试。

SKILL.md
--- frontmatter
name: testing
description: Write tests using TDD (Red-Green-Refactor) and AAA pattern. Use for every new feature, behavior change, or bug fix. Covers unit, integration, and E2E test selection.

Applicability Rubric

ConditionPassFail
New feature implementationAdding new functionalityNo new behavior
Behavior changeModifying existing behaviorNo behavior change
Bug fixFixing a defectNon-bug change
Pre-refactoringEnsuring tests exist before refactorTests already sufficient

Apply when: Any condition passes

Core Principles

Integration-First Philosophy

Ensure components work together first, then verify individual component details

ScenarioAction
Starting new featureWrite integration/E2E test first
Integration test passesAdd unit tests only for uncovered edge cases
Edge case not covered by integrationAdd unit test for specific case
Component works alone but fails togetherMissing integration test coverage

TDD Cycle (Red-Green-Refactor)

code
┌─────────┐
│   RED   │ ← Write failing test
└────┬────┘
     ↓
┌─────────┐
│  GREEN  │ ← Write minimal code to pass
└────┬────┘
     ↓
┌─────────┐
│REFACTOR │ ← Improve code, keep tests green
└────┬────┘
     ↓
   (repeat)

Testing Pyramid

code
        ╱╲
       ╱  ╲
      ╱ E2E╲        Few, slow, high confidence
     ╱──────╲
    ╱Integration╲   Some, medium speed
   ╱────────────╲
  ╱    Unit       ╲  Many, fast, focused
 ╱─────────────────╲

Test Types

TypeScopeSpeedUse For
UnitSingle function/classFastLogic, calculations
IntegrationMultiple componentsMediumComponent interaction
E2EFull systemSlowCritical user flows

Test Type Selection

Testing TargetUnitIntegrationE2E
Pure functions
Business logic
Edge cases
Error handling
Database operations
API endpoints
Service interactions
External dependencies
Critical user journeys
Smoke tests
Regression prevention

Test Structure (AAA Pattern)

code
test("should calculate total with discount", () => {
  // Arrange - Set up test data
  const cart = new Cart();
  cart.addItem({ price: 100 });

  // Act - Execute the behavior
  const total = cart.calculateTotal(0.1); // 10% discount

  // Assert - Verify the result
  expect(total).toBe(90);
});

Completion Rubric

Before Writing Tests

CriterionPassFail
Requirement understandingRequirements clearUnclear what to test
Test case identificationHappy path, edge cases, errors identifiedMissing test scenarios
Test type selectionAppropriate type chosenWrong test level
Environment setupTest environment readySetup issues

Writing Tests

CriterionPassFail
Descriptive namingName describes behaviorName describes implementation
Single assertionOne assertion per test (when practical)Multiple unrelated assertions
IndependenceTests have no shared stateTests depend on each other
DeterminismTests always produce same resultFlaky tests

Test Quality

CriterionPassFail
Correct failureTests fail for right reasonFalse positives/negatives
Behavior focusTests behavior, not implementationTests internal details
Documentation valueTests serve as documentationTests are cryptic
Fast feedbackTests run quickly enoughSlow test suite

After Writing Tests

CriterionPassFail
All passingAll tests passFailing tests
Adequate coverageMeaningful coverage achievedCritical paths untested
MaintainabilityTests easy to maintainFragile or complex tests
No test smellsClean test codeTest code smells present

Test Naming Convention

Pattern: should [expected behavior] when [condition]

Examples:

  • should return empty array when no items exist
  • should throw error when input is invalid
  • should calculate discount when coupon is applied

Common Test Smells

SmellProblemSolution
Flaky TestSometimes passes, sometimes failsRemove randomness, fix timing issues
Slow TestTakes too long to runMock external deps, use unit tests
Brittle TestBreaks with unrelated changesTest behavior, not implementation
Mystery GuestUses external data/filesMake test self-contained
Test DuplicationSame setup repeatedUse test fixtures/factories

Mocking Decision Table

TargetMock?Reason
External services (APIs, DB)YesIsolation, speed
Time/datesYesDeterminism
Random valuesYesReproducibility
File system (unit tests)YesSpeed, isolation
The thing being testedNoDefeats purpose
Value objectsNoSimple, no side effects
Simple collaboratorsNoOver-isolation
EverythingNoOver-mocking hides bugs