Massoud the Great
Massoud the Great

Reputation: 63

How can I run integration tests without modifying the database?

I am making some integration tests for an app, testing routes that modify the database. So far, I have added some code to my tests to delete all the changes I have made to the DB because I don't want to change it, but it adds a lot of work and doesn't sounds right. I then thought about copying the database, testing, deleting the database in my testing script. The problem with that is that it is too long to do. Is there a method for doing that ?

Upvotes: 1

Views: 1166

Answers (2)

Serhii Shushliapin
Serhii Shushliapin

Reputation: 2708

You can start a database transaction at the beginning of a test and then roll it back. See the following post for details: https://lostechies.com/jimmybogard/2012/10/18/isolating-database-data-in-integration-tests/

Upvotes: 1

Ivan Lymar
Ivan Lymar

Reputation: 2293

I see two possible ways to solve your problem:

  • In-memory database e.g. (h2)
  • Database in docker container.

Both approaches solve your problem, you can just shutdown db/container and run it again, db will be clean in that case and you don't have to care about it. Just run new one. However there are some peculiarities:

In-memory is easier to implement and use, but it may have problems with dialects, e.g. some oracle sql commands are not available for H2. And eventually you are running your tests on different DB

Docker container with db is harder to plugin into your build and tests, but it doesn't have embeded DB problems with dialects and DB in docker is the same as your real one.

Upvotes: 1

Related Questions