Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In which cases do you test against an In-Memory Database instead of a Development Database?

When do you test against an In-Memory Database vs. a Development Database?

Also, as a related side question, when you do use a Development Database, do you use an Individual Development Database, an Integration Development Database, or both?

Also++, for unit testing, when do you use an In-Memory Database over mocking out your Repository/DAL, etc.?

like image 996
Troy DeMonbreun Avatar asked Sep 24 '08 16:09

Troy DeMonbreun


People also ask

What is the use case of in-memory database?

In-memory databases can persist data on disks by storing each operation in a log or by taking snapshots. In-memory databases are ideal for applications that require microsecond response times or have large spikes in traffic such as gaming leaderboards, session stores, and real-time analytics.

Should I use in-memory database for integration tests?

An in-memory database is useful for both unit tests and integration tests when you don't want to mock out a complete data access layer, or if you need to have a real database due to an ORM.

Which of the following is an in-memory database?

SQLite Database SQLite is a SQL database that runs only in embedded mode, either in memory or saved as a file.

Why in-memory database is faster?

In-memory databases are faster than traditional databases because they require fewer CPU instructions. They also eliminate the time it takes to access data from a disk. In-memory databases are more volatile than traditional databases because data is lost when there is a loss of power or the computer's RAM crashes.


2 Answers

In memory is an excellent choice for your unit-tests, when the data is easy to seed for your given test cases and a very particular operation is being tested. A real database is better for integration tests, where the data pre-requisites are more complex and there is value to having the base data remain after the tests complete.

For us, the only things we allow in our 'fast' test suite of JUnit tests are those that do not have any external dependencies (database, file, network, etc) so that the suite can be run quickly and efficiently by both developers and continuous integration on checkin. If there is a certain test that absolutely needs to go to the DB, then an in memory one is the only way to go.

A couple points to keep in mind:

  • Think carefully if you need to use a database at all in a unit test. It may be indicative of a poor design in that the data access layer is coupled too tightly to the business logic you are trying to test and cannot be mocked out.
  • If using a real database for integration testing, ensure that the tests always restore the data to a pristine state when finished. I've seen a lot of wasted time and failed integration tests because some other test messed up the data.

As for your other question, it really depends on your need. A good rule of thumb is one development database per code branch, since schema changes may be needed that are not relevant to another branch of code. Just having a dedicated development database is important; I'm surprised at how many development teams have to share a database with the QA team, etc. It is important to be able to make changes in a sandboxed environment that does not affect other teams or prevent others from doing their work, so if you've met those requirements you're doing well.

like image 78
Peter Avatar answered Sep 30 '22 10:09

Peter


For my team, it's in-memory on developper machine, and the real-database on the continuous integration server.

like image 20
pmlarocque Avatar answered Sep 30 '22 11:09

pmlarocque