Integration Testing

Learn what integration tests are and how to write them. Find out how using fixtures might help you.

Unit tests are good, but they won’t tell us anything if we’re misusing the external services. We have integration tests to check that.

An external service could mean anything outside of our code—an external server providing an API, a database, another application, a command-line tool, or even a different module in our codebase.

How to write integration tests

In general, integration tests follow the same Arrange-Act-Assert structure, but in this case, the Arrange and Assert phases are trickier. We spawn an instance of an external service to call it. So, we often need to arrange the state of an external system and assert how this state has changed. Since the external system is usually out of our control, we have to do integration-specific tricks to make it work.

If a service is a data store or something like a message queue, we can access it to both read and write data. We write the data in the Arrange phase and read it in the Act phase or vice versa. We can access the data store directly in the test or use the service we’re testing for both actions.

For example, let’s test a Redis-backed key-value store.

Get hands-on with 1200+ tech skills courses.