Integration Testing: Adding Fixtures

Learn how we can apply integration testing using fixtures.

Most of the integration tests assess database queries. If they do, the test schema looks like this:

  • Fill the database with some predefined data.

  • Run the query we test.

  • Assert that the result matches the expected one.

We’ll see a similar test schema in tests for whole modules that query data from the database and perform some logic depending the result of that query.

The problem of filling the database with predefined data for tests is so common that we have many libraries to solve it.

In the context of tests, sample data for the database is called data fixtures. We define fixtures in code and use fixture loaders to dump them into the database.

Let’s make a service querying the database and test it. We’ll create a table with some addresses and a service that returns the count of saved addresses in that city given the country code and city name.

First, let’s make a database table and run the query we want to do.

Get hands-on with 1200+ tech skills courses.