Write Unit Test in the project with Golang

Tram Ho

In the previous sections, we have completed the basic functions of a web serverver such as Login, Register, CRUD. The purpose of this article is to introduce basically how to deploy and write unit tests with Golang. The way to write more complex, more sophisticated test cases will not be introduced in this article. Hope to be introduced to you soon in a near post.

1. Introduction

Go provides 2 standard libraries for writing tests:

  • testing : Provides basic automated testing functions
  • httptest : Provides functions for http testing

There are also quite a lot of libraries, framework tests developed by 3rd parties such as gocheck , ginkgo , etc.

Test files when using package testing have a ending in the name _test.go . For example, if we have the file hello.go , the test file will be named hello_test.go In addition, the test function name will be in the form

The Test function passes a pointer type parameter T of the testing library. type T contains methods that support the management of the status of test cases as well as support the printing logs of test cases through the methods of Error , Errorf , Fail , Log , etc. Details of all methods of type T and You can see the usage details here .

2. Write testcase with testing library

We have a simple example as follows:

  • The hello function performs the basic function of returning a Hello + tham số name . If name empty then returns What is your name ? string What is your name ? .
  • The TestHello function tests two outputs of the hello function. If the return value is different from the expected output, the test will fail.
  • The t.Errorf method as well as the t.Error function has the function to mark failed tests if the output is not expected. After execution is complete, the code in the TestHello function will continue to be executed. t.Errorf different from t.Error like fmt.Printlnf different from fmt.Println .

We run the test, test case pass because the return value matches the expectation.

We try to fix the hello function a bit and keep the TestHello function intact

Run test case:

The actual output is You do not have name ! instead of What is your name ? as expected.

Check test coverage

100% coverage, great

3. Write testcase with httptest library

  • The simple Welcome function returns the client the Welcome to our website message when the user visits http//localhost:3000 .
  • The TestWelcome function will need to create a separate webserver (with the default multiplexer of the net / http library). The result status code returned is checked, if it is different from 200 then the test will fail.

3. Write testcase using a 3rd party library

Libraries officially supported by Go Team such as testing or httptesting are simple, easy to learn, and easy to use. However, for medium to large applications, the need for more test cases or the use of more advanced writing methods such as double tests , they show a certain limitation. It’s time to get more powerful tools from 3rd parties.

Gocheck

Install :

  • Suite function is responsible for registering the object to be tested, in the above code implies that we need to test methods of struct Person . Any method starting with the Test prefix will be considered a test method.
  • The function func TestingT(testingT *testing.T) will be responsible for running the test cases for the object registered in the Suite function.
  • Suppose we create a new struct as Personal and replace the statement var _ = Suite(&Person{}) with var _ = Suite(&Personal{})

Run test go:

As we can see, no test cases are run because we didn’t write any functions to test Personal . Returning to the same and running go test , we will have 2 testcase of Person as follows.

Ginkgo

Ginkgo is a framework that supports testing in Go. Compared to Gocheck , Ginkgo is strong, as well as supports more features. If you have ever written tests for Javascript, you will probably be familiar with Ginkgo.

Install :

Along with Ginko , we install Gomega as a matcher / assertion library, similar to the Chai library in Javascript. It helps to check that the return values ​​are as expected.

Demo

In this example, we define a struct Book containing three information and a CategoryByLength method to sort books by page number. Larger than 300 pages is of the NOVEL format, less than 300 pages return SHORT STORY .

  • File book_test.go we write test to test the CategoryByLength method
  • We fake 2 variables wild data for 2 types of books
  • When calling the CategoryByLength method with the longBook variable, we use the functions of the Gomega library to check if the expected return value is NOVEL or not? Same with the shortBook variable.

Just run the test

References

https://www.manning.com/books/go-web-programming

https://medium.com/rungo/unit-testing-made-easy-in-go-25077669318

https://onsi.github.io/ginkgo/

http://onsi.github.io/gomega

Share the news now

Source : Viblo