1

Does Go have any ability to run some code before ALL tests? I know that Go provides with TestMain(), but its package scope func, and will set up and tear down only tests in package where she located.

But I want to run some docker test containers before ALL tests, and of course tear it down, when all test be done.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Vadziec Poplavsky
  • 709
  • 1
  • 6
  • 12
  • Not specifically using the Go tools which if that is exactly what you're after ignore this; However you could look into Makefiles which can provide a build script system based on *nix terminal commands. I've previously used this for Integration testing (start containers, run go test, tear-down containers regardless of test result). – Michael Jan 17 '20 at 07:58
  • `init()` runs before all tests _in a package_. – Jonathan Hall Jan 17 '20 at 08:42

1 Answers1

3

Write Setup() and TearDown() functions in your tests. If you are using table driven tests, or normal tests, call Setup() and TearDown() in each test before and after.

You can also use a test suite, which does a similar thing, it kind of provides this feature like a library: https://godoc.org/github.com/stretchr/testify/suite

Ankit Deshpande
  • 3,476
  • 1
  • 29
  • 42