Lamps in testing the Golang

From the python world, fixtures are very useful (Fixtures defines a Python contract for reusable state / support logic, primarily for unit testing). I was wondering if there is such support in the Golang that can allow me to run my tests using some predefined devices, such as setting up a server, tearing it down, and performing repetitive tasks every time I run the test? Can someone point me to some examples of how to do the same in the Golang?

+6
source share
3 answers

If you want to use standard Go testing tools, you can define a function with the signature TestMain(m *testing.M) and place your instrument code there.

From the test suite wiki :

Sometimes it is necessary for a test program to perform additional tuning or breaks before or after testing. Sometimes it is also necessary for a test to control which code works in the main thread. To support these and other cases, if the test file contains a function:

func TestMain(m *testing.M)

then the generated test will call TestMain (m) instead of running the tests directly. TestMain runs in the main goroutine and can perform any configuration and disassembly when calling m.Run. Then it should call os.Exit with the result of m.Run. When TestMain is called, the .Parse flag does not fire. If TestMain depends on command line flags, including a test package, it must call flag.Parse explicitly.

Simple implementation of TestMain:

 func TestMain(m *testing.M) { flag.Parse() os.Exit(m.Run()) } 
+6
source

I know this is an old question, but it still appeared in the search results, so I decided to give a possible answer.

You can isolate code from helper functions that return a โ€œbreakโ€ function for cleaning after yourself. Here is one possible way to start the server and close it at the end of the test.

 func setUpServer() (string, func()) { h := func(w http.ResponseWriter, r *http.Request) { code := http.StatusTeapot http.Error(w, http.StatusText(code), code) } ts := httptest.NewServer(http.HandlerFunc(h)) return ts.URL, ts.Close } func TestWithServer(t *testing.T) { u, close := setUpServer() defer close() rsp, err := http.Get(u) assert.Nil(t, err) assert.Equal(t, http.StatusTeapot, rsp.StatusCode) } 

This starts the server with net/http/httptest and returns its URL along with a function that acts as a "break". This function is added to the deferral stack, so it is always called regardless of the exit from the test case.

Optionally, you can pass *testing.T if you have a more complicated setup there and you need to handle errors. This example shows the set function that returns *url.URL instead of a string in URL format, and the analysis may possibly return an error.

 func setUpServer(t *testing.T) (*url.URL, func()) { h := func(w http.ResponseWriter, r *http.Request) { code := http.StatusTeapot http.Error(w, http.StatusText(code), code) } ts := httptest.NewServer(http.HandlerFunc(h)) u, err := url.Parse(ts.URL) assert.Nil(t, err) return u, ts.Close } func TestWithServer(t *testing.T) { u, close := setUpServer(t) defer close() u.Path = "/a/b/c/d" rsp, err := http.Get(u.String()) assert.Nil(t, err) assert.Equal(t, http.StatusTeapot, rsp.StatusCode) } 
+1
source

Now there are many Go packages that you can use instead of the standard Go software testing tools. My favorites include:

  • give evidence
  • gocheck
  • goblin

They can potentially satisfy most of your testing automation needs, and the Golang community is constantly improving and developing new testing methods. Check out https://golang.org/pkg/testing/ for more information.

0
source

All Articles