Reset a separate backend service when testing the interface

I have an API project running on localhost: 8000 connected to a test database, and I have an external angular application running on localhost: 9000. How do I reset the database after running each test foreground test case? I use Protractor for my front-end testing of E2E. I do not want to make fun of or drown out the entire backend. Although this will speed up my test faster, however, each user interaction will lead to changes in the serverโ€™s state and several more requests for the same server endpoints will follow, and these endpoints with different results at different points may be too big than just talking to a real server. I would prefer to reset the database to its original state or reset all tables.

One possible way to do this is to write my first E2E test suite in my API project. Since it is now launched using the backend, it can easily reset the database before each test case. But I would prefer to keep my tests in my front-end project, as my api will also serve other clients, not just the browser client. And since my backend is in php and uses Phpunit for testing, while I use Protractor to test my interface, integrating it into the backend seems a bit out of place.

Have you encountered this problem and your solution? How do you coordinate two projects in your E2E testing (or integration?)?

+5
source share
2 answers

I went to write all the E2E tests inside the API project after a failed transition to the client side. Trade-offs:

  • The API has all the factories and models that you can use to download test cases. If you are testing only from the client side, I need to recreate the relationship between the model and the factory in order to make it work and keep track of all changes in the API models.

  • I need to manually configure the client side to talk to the api test environment each time before running the E2E test. And also manually install the api server in test mode. Because you need the actual web server API to work somewhere so that the client can get there.

  • At present, it is still difficult to determine how to transfer this custom test setup to the cloud and, therefore, integrate with the continuous integration server, since the client and api are two separate projects. But I finally have E2E client-side testing running on my local machine, which is better than no tests.

+1
source

I had the same problem. My solution is to add the api "reset" endpoint to restore the database to its state before running the E2E test.

We use ASP.NET WebApi to create a RESTful service. Here is my code structure.

[HttpGet] [Route("reset")] public IHttpAction Reset(){ // check and remove records added in the e2e tests // check and restore the records modified in the e2e tests } 

Thus, the api call to this test/reset method helps restore the database to its original state. We can put a call inside afterAll() (if you use JasmineJS). Or you can simply do a manual reset by making this GET call from Postman or Fiddler.

Sometimes I have several reset api methods to call. So I can check and reset by function groups. For example, run the e2d tests on user management functions and reset this piece of data, and then move on to the next function.

It should be noted that if the primary key in the table is ordered, deleting the data added to the test will violate this sequence. Make sure our tests do not "expect" this primary key.

The goal is to re-run the e2e tests and get the same result.


And, of course, if allowed, another good way out is to back up the database before our e2e testing and restore it after the test completes. Manual, stupid, but sometimes convenient and sufficient for our e2e test to be repeated again.

+1
source

Source: https://habr.com/ru/post/1211956/


All Articles