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

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

Server and client on the same server. Base for testing sqlite they have in common. Let's say for every endpoint I have a test. In each test, an http request is sent to the endpoint using the http client, as if the consumer (consumer) did, and the answer to correctness is checked. 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 you need to create 7 users with fake data. And after the test so that they are "removed". And on the next test again the same creation-delete chain.

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

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

  3. I tried the option where the removal of the database and the migration before each test is done. It works but for a very long time (a minute is needed to check 4 endpoints). In memory the database here would be sped up strongly, but here the same problem as with the transaction.

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

    1 answer 1

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

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

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

    • My question is, in fact, in the run of non-destructive and destructive tests in case requests go through http and there is no possibility to make transactions since http is stateless. - German Malinovsky
    • For tests, a separate copy of the project and its unique ip: port, where no one else knocks, is used. - Sergiks