Thanks for your overview of different packages. I would also like to throw another one in the ring, github.com/DATA-DOG/go-sqlmock. It works great for cases where you only care about the general interactions with databases. Such as testing that transactions are rolled back on an error or that fixed value are inserted into a table.

In my experience, I would advise against cleaning the database between runs for three important reasons:

  1. It’s really, really slow. Truncating or otherwise deleting data (especially if you have to keep reapplying a data fixture) add a huge bourden to tests making your test suite very slow.
  2. It fails to catch some errors. Let’s say that you have a function that fetches rows from a people table. If you wipe people before running the test there is no risk of getting back data that you should not and so an extra filtering condition could be missed. You would have to be vigilant to create extra records that don’t match your condition.
  3. Wiping the data means you can’t run the tests in parallel. It’s likely that the application also receives requests in parallel so the database under test should work that way as well. Preferably the run order is randomized. This may catch locking issues you otherwise wouldn’t pickup.

Written by

I’m a data nerd and TDD enthusiast originally from Sydney. Currently working for Uber in New York. My thoughts here are my own. 🤓 elliotchance@gmail.com

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store