Anas Anjaria
Anas Anjaria's blog


Anas Anjaria's blog

Using a Separate Database for Every Unit Test

Photo by David Travis on Unsplash

Using a Separate Database for Every Unit Test

Test one functionality at a time

Anas Anjaria's photo
Anas Anjaria
·Sep 11, 2022·

1 min read

Unit testing means

Testing one functionality at a time.

Unit testing is crucial in my opinion. It not only allows us to test specific functionality but also enables us to understand the given functionality.

Therefore, it’s crucial to give the same importance to unit tests as to other programming practices.

Over time I have learned some good practices and I am sharing them in this series.

Using a separate database for every test

It’s about testing DAO layers where we populate test data into a database (DB) to test some functionality.

Please note that each unit test will have its own DB with test data.

So here is a pseudo code.


1. Start unit test
2. Create a database
3. Populate test data
4. Test functionality
5. Tear down the created database.

Why should I do that?

  • To have deterministic testing irrespective of the order of test execution.
  • Each test is isolated and enables us to maintain it easily since there are no dependencies between unit tests.

Thanks for reading.

If you have any questions, please use the comments section. I will be happy to answer them.

Originally posted on medium

Want to connect?

Want to subscribe to my newsletter?

Share this