Writing integration tests
In our previous example, we executed a SQL query through a database client, and, in our unit tests, we replaced that client with a mock. This allowed us to isolate the business logic and test it independently. But it also raises two important questions:
- How do we know that the SQL query itself is correct and actually returns what we expect?
- How can we be sure that our mocked data is a faithful representation of what the real database would return in a real scenario?
This is where integration tests come into play.
Integration tests allow us to move beyond isolated units of code and verify how different components (such as our business logic and our database) work together in practice. They help us test the real flow, using real inputs, real queries, and real infrastructure (like an actual SQLite database), giving us confidence that everything behaves correctly as a system.
In this section, we’ll explore how to write...