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:
- 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.
- It fails to catch some errors. Let’s say that you have a function that fetches rows from a
peopletable. If you wipe
peoplebefore 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.
- 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.