Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I do test setup using the testing package in Go

How can I do overall test setup processing which sets the stage for all the tests when using the testing package?

As an example in Nunit there is a [SetUp] attribute.

[TestFixture] public class SuccessTests {   [SetUp] public void Init()   { /* Load test data */ } } 
like image 745
miltonb Avatar asked May 19 '14 04:05

miltonb


People also ask

How do you run a test in go?

To run your tests in this mode, run go test in your project's root directory. In the package list mode, go test compiles and tests each package listed as arguments to the command. If a package test passes, go test prints only the final 'ok' summary line. If a package test fails, go test prints the complete test output.

What is setup in testing?

Setup test cases are designed to run at the beginning of your test case in order to declare and define conditions and variables. The primary benefit of using a setup test case is to simplify the process of making global changes to test case parameters.

Where do I put test files in go?

By convention, Go testing files are always located in the same folder, or package, where the code they are testing resides. These files are not built by the compiler when you run the go build command, so you needn't worry about them ending up in deployments.


2 Answers

Starting with Go 1.4 you can implement setup/teardown (no need to copy your functions before/after each test). The documentation is outlined here in the Main section:

TestMain runs in the main goroutine and can do whatever setup and teardown is necessary around a call to m.Run. It should then call os.Exit with the result of m.Run

It took me some time to figure out that this means that if a test contains a function func TestMain(m *testing.M) then this function will be called instead of running the test. And in this function I can define how the tests will run. For example I can implement global setup and teardown:

func TestMain(m *testing.M) {     setup()     code := m.Run()      shutdown()     os.Exit(code) } 

A couple of other examples can be found here.

The TestMain feature added to Go’s testing framework in the latest release is a simple solution for several testing use cases. TestMain provides a global hook to perform setup and shutdown, control the testing environment, run different code in a child process, or check for resources leaked by test code. Most packages will not need a TestMain, but it is a welcome addition for those times when it is needed.

like image 104
Salvador Dali Avatar answered Nov 15 '22 22:11

Salvador Dali


This can be achieved by putting a init() function in the myfile_test.go file. This will be run before the init() function.

// myfile_test.go package main  func init() {      /* load test data */ } 

The myfile_test.init() will be called before the package init() function.

like image 43
miltonb Avatar answered Nov 15 '22 22:11

miltonb