How to write golang integration test with MySQL

I want to write an integration test that uses MySQL to test my queries. How do I do this in golang?

This contains several questions:

  • How to setup MySQL server (in-memory?) In golang test?
  • How do I clean / recreate the data model before / after each test so they don't leave garbage?
  • How to crash mysql after all tests have finished?
+3


source to share


1 answer


If you really want to have MySQL embedded, you can use Golangs C bindings to integrate with: https://dev.mysql.com/doc/refman/5.1/en/libmysqld.html . I haven't seen any project build the bindings for this in a good Go package, it would be an interesting little project.

Otherwise, you can use Docker to set up a backup MySQL server, this requires a few setup / teardown steps before running the go test. This is what we do, where I work.



In both cases, you will need to write setup / drop methods that create and drop tables as needed for your tests. These are just normal SQL statements, DROP DATABASE, CREATE TABLE, etc.

Testify https://github.com/stretchr/testify has tools to set / detach, but just writing a helper function for this is very easy.

+2


source







All Articles