AgentSkillsCN

testing-patterns

测试策略与测试模式

SKILL.md
--- frontmatter
name: testing-patterns
description: Test strategies and patterns

Testing Patterns

Test strategies for full-stack applications.


Test Pyramid

code
        /\
       /E2E\        Few, slow, critical paths
      /------\
     / Integ- \     Some, key integrations
    /--ration--\
   /    Unit    \   Many, fast, isolated
  /--------------\
TypeSpeedScope
UnitFastSingle function/class
IntegrationMediumMultiple components
E2ESlowFull user flow

AAA Pattern

csharp
[Fact]
public void Method_Scenario_Expected()
{
    // Arrange - Set up test data
    var input = CreateInput();
    
    // Act - Execute the code
    var result = Process(input);
    
    // Assert - Verify outcome
    Assert.Equal(expected, result);
}

Test Naming

Format: Method_Scenario_ExpectedResult

❌ Bad✅ Good
TestAddAdd_TwoPositive_ReturnsSum
Test1GetUser_InvalidId_ThrowsException

What to Test

✅ Test❌ Skip
Business logicFramework code
Edge casesGetters/setters
Error handlingThird-party libs
Public APIImplementation details

Mocking Guidelines

MockDon't Mock
External APIsThe code under test
DatabaseSimple objects
File systemPure functions
Time/RandomBusiness logic

Code Coverage

MetricTarget
Overall70-80%
Business logic90%+
Controllers60%+
InfrastructureLower priority

DO / DON'T

✅ Do❌ Don't
One concept per testTest multiple things
Descriptive namesCryptic test names
Independent testsTests that depend on order
Test edge casesOnly happy path