测试微服务和分布式系统比测试单体应用更复杂——服务通过网络交互,具有独立部署和分布式故障模式。它需要采用合约测试、服务级别测试和谨慎的集成/E2E方法等策略。
挑战
Distributed systems are harder to test than monoliths:
→ many SERVICES interacting over the NETWORK (latency, failures, partial outages)
→ services deployed INDEPENDENTLY → integration/compatibility concerns
→ E2E testing across many services is SLOW, BRITTLE, and complex to set up
→ distributed failure modes (a service down, slow, returning errors)
测试策略
✓ Test each service in ISOLATION (its own unit + integration tests) — the foundation
✓ CONTRACT TESTING (e.g. Pact) → verify services agree on their API CONTRACTS without
full integration → catches breaking changes between services (provider/consumer) →
a KEY technique for microservices (avoids brittle full E2E for compatibility)
✓ Component/service-level tests → test a service with its dependencies MOCKED/stubbed
✓ LIMITED E2E → a few critical cross-service flows (not exhaustive — too brittle/slow)
✓ Test RESILIENCE → how services handle dependency failures (timeouts, retries,
circuit breakers); CHAOS testing for failure modes
