database – API Integration Testing


There are such endpoints in a simplified form on the server:

GET /users
POST /users — создание пользователя
GET /users/{id}
DELETE /users/{id}

Server and client on the same server. They have a common basis for testing sqlite. Let's say I have a test for each endpoint. In each test, an http request is sent to the endpoint using the http client, as if it were a consumer (consumer), and the response is checked for correctness. Before the very beginning of the tests, an empty database is created and the migration is rolled. After the tests, the base is deleted.

My problem is that before each test I need to create 7 users with fake data. And after the test, so that they are "deleted". And on the next test, again the same create-delete chain.

  1. First, I did the database through transactions, that is, the test starts and a transaction is started on the client, 7 users are created, in the test there is an http request for GET / users, but there is no transaction on the server and there is an empty database. Therefore, this option is eliminated.

  2. If you do without a transaction, then on the client and server due to the fact that the shared database data are the same, but I need only 7 users before each test so that previous manipulations with the database do not distort the data. Therefore, this option is also eliminated.

  3. I tried the option where the database is deleted and migrated before each test. Works but for a very long time (it takes a minute to check 4 endpoints). An in memory database would speed up a lot here, but here is the same problem as with a transaction.

I just can't find a solution. What can you advise? I want to test all endpoints not through functional tests inside the code, but as if from the outside (as an interface is tested through selenium).


In a similar situation, in one project, before starting the tests, a separate test database is created, the structure of the combat database is copied into it and filled with fake data. For one test, or for a group of tests. Then it is removed.

Tests use a mock object to work with a database, where a test database is substituted instead of a combat one.

Sometimes you need to run non-destructive tests on existing records in a combat or stage database; or it is not convenient to update the test data – you can switch the used database.

Scroll to Top