8 Integration testing
This chapter covers
- What are integration tests
- Simulating SQL databases
- Mocking HTTP calls
- Using WebApplicationFactory for API testing
Unit testing tests individual units in isolation. Dependencies between units are abstracted, usually by interface. The interfaces create a contract that both the dependent and the dependency adhere to. But it is very rare that your code has no side effects. Even in previous chapters we have side effects with using Streams. The stream’s position and whether its open or closed or disposed can be changed by any code that touches it. There may be changes to a database while your code is running that causes an insert to fail or an update to be overwritten. Unit testing is not enough. We also need to test how units work together, i.e. integration tests.
What are integration tests?
The clearest definition of integration tests I’ve found comes from Martin Fowler (read the article at https://martinfowler.com/bliki/IntegrationTest.html). The article states "Integration tests determine if independently developed units of software work correctly when they are connected to each other." Fowler goes on to distinguish between broad and narrow integration tests, the former involving many active modules and the latter using mocks to test a limited set of interactions.