Integration Testing
Testing how multiple components work together as a group, verifying that different parts of the system communicate and interact correctly.
Detailed Explanation
Integration tests verify that components work together: API endpoints connect to databases, services call each other correctly, and modules integrate as expected. They are slower than unit tests but catch issues that unit tests miss—data format mismatches, network errors, and configuration problems.
Integration tests use real (or test) databases, real HTTP connections, and real message queues. They test the "seams" between components. Common scenarios: database queries return expected results, API endpoints return correct responses, authentication flows work end-to-end, and data flows between services correctly.
Why It Matters
Integration tests catch the bugs that unit tests miss—issues that only appear when real components interact. They are essential for confidence in production deployments.
Real-World Example
An integration test verifies that creating a user via the API correctly stores the user in PostgreSQL, sends a welcome email via the email service, and creates a default workspace—all in one test.
When to Use
For testing component interactions: API-database integration, service-to-service communication, authentication flows, and data pipelines. Run integration tests in CI/CD pipelines.
Advantages
- Catches issues between components
- Tests real-world behavior
- Verifies configuration and wiring
- Catches database and network issues
- Higher confidence than unit tests alone
Disadvantages
- Slower than unit tests
- Harder to debug failures
- Requires test infrastructure (databases, services)
- Flaky if environment is not controlled
- More complex test setup and teardown
Related Terms
Frequently Asked Questions
What is the difference between unit and integration tests?
Unit tests verify individual functions in isolation (mocked dependencies). Integration tests verify that real components work together (real databases, real APIs). Unit tests are fast and focused; integration tests are slower but test real behavior.
How do I write integration tests for APIs?
Use tools like Supertest (Node.js), pytest (Python), or http4k (Java) to make real HTTP requests to your API. Use a test database, set up test data, and verify responses and database state.
Should I use a test database for integration tests?
Yes. Use a separate test database (or schema) that can be reset between tests. Never run integration tests against production or development databases. Use Docker to spin up test databases.
How do I handle flaky integration tests?
Ensure test isolation (reset state between tests), use deterministic data, avoid time-dependent tests, retry flaky tests, and invest in test infrastructure reliability. Flaky tests erode confidence.
When should I run integration tests?
In CI/CD pipelines on every pull request, nightly for comprehensive suites, and before deployments. They are too slow for local development—use unit tests for rapid feedback locally.