API testing
Testing pyramid
End-to-end tests
Test complete user workflows across multiple APIs and services. Fewer but critical scenarios. Use Postman, Playwright, or Cypress.
Integration tests
Test API endpoints with real dependencies (database, external APIs). Use TestServer or Docker containers for isolated test environments.
Unit tests
Test individual components (controllers, services, validators) in isolation. Use mocking for dependencies. Aim for 70-80% code coverage.
Additional testing types
- Contract testing: Consumer-driven contracts (Pact) and conformance to the OpenAPI spec (Dredd, Schemathesis)
- Load testing: Measure performance under stress (k6, JMeter, Azure Load Testing)
- Security testing: OWASP ZAP, penetration testing, vulnerability scanning
- Chaos testing: Test resilience by injecting faults
Monitoring & observability
When something slows down at 3 a.m., a correlated trace shows which dependency is to blame in seconds instead of hours.
One request, end to end
Transaction trace4bf92f35…
- POST /api/v1/orders214 ms
- Validate JWT
- SQL · INSERT Orders
- Redis · SET order:7731
- Dataverse · Create salesorder
- Service Bus · order-created
{ "level": "Information", "operationId": "4bf92f35…", "orderId": 7731, "elapsedMs": 214 }Illustrative example
Application Insights
Comprehensive monitoring for APIs with Application Insights:
- Request tracking: Response times, success rates, failure analysis
- Dependency monitoring: Track calls to databases, external APIs, queues
- Distributed tracing: Correlation across microservices
- Custom events: Track business metrics and KPIs
- Live metrics: Real-time monitoring dashboard
- Alerts: Proactive notifications for anomalies
Logging best practices
- Structured logging: JSON format with consistent schema
- Correlation IDs: Track requests across services
- Log levels: Trace, Debug, Information, Warning, Error, Critical
- Sensitive data: Never log passwords, tokens, PII
- Centralized logging: Azure Monitor, ELK stack, Datadog
Health checks
Implement health check endpoints for orchestrators and load balancers:
- /health/live: Liveness probe (is process running?)
- /health/ready: Readiness probe (can accept traffic?)
- Dependency checks: Validate database, cache, queue connectivity
- Response format: Return status code and optional details JSON