Fast tests with in-memory MongoDB
It’s always better to have a faster tests. For tests which use a database there are two ways to increase the speed:
- a mock library, like mongomock
- in-memory database
With the mock library it’s faster, but it’s more reliable to use real db and usually the mock libraries does not cover all functionality.
For in-memory MongoDB there is an easy way to run it in Docker with a tmpfs volume.
docker run -d --name=mongo_test -p 27018:27017 --tmpfs /data/db mongo:3.4
Then it can be used in tests on 27018 port.
In docker-compose it can be described as:
mongo_test:
image: mongo:3.4.0
tmpfs: /data/db
For one of my apps I have an increase more than 3 times, from 84s to 25s.
The same way it also can be used for in-memory MySQL.
docker run --tmpfs /var/lib/mysql -p 3307:3306 -e MYSQL_ROOT_PASSWORD=pass -e MYSQL_DATABASE=db -d --name=mysql-test mysql:5.6
I have an increase also around 3 times, from 3m 33s to 54s.