Get the App
SLTechnology News&Howtos  ›  Development  › 

How to write GO project test

Shulou Source: shulou.com Published: 2022-06-03 15:31:32 10月05日 Update

This article mainly shows you "GO project test how to write", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "GO project test how to write" this article.

The Writing method of golang Project test

Business code

Package main import "fmt" func sum (an int,b int) int {return aquib} func main () {fmt.Println ("hello test")}

Test test case

Package main import ("fmt"testing") func TestSum (t * testing.T) {var a = 3 var b = 4 res: = sum (an error b) fmt.Printf ("the sum of% d and% d:% d", a t.Error b) if res! = 7 {t.Error ("error")}}

Each test file must import a testing.

Every test case under the test file must begin with Test and conform to the TestXxx form, otherwise go test will directly pass the test and not execute.

Go test will automatically look for the test file in this directory, and go test-v will show the execution process in detail.

The input parameter of test case is t testing.T or b testing.B

T.Error is a print error message, and the current test case is skipped

T.SkipNow () skips test and directly presses PASS to process the next test, and must be written on the first line of test case, otherwise it is invalid

The test of go does not guarantee that multiple TestXxx are executed sequentially, but it is usually executed sequentially. In order to allow sequential execution, t.Run (name string, f func) can be used to ensure sequential execution.

TestMain (m * testing.M) initializes test and uses m.Run () to call other tests to complete some initialization operations testing, such as database connection, file opening, REST service login. If m.Run () is not called in testMain, no other test case except TestMain will be executed.

The above is all the content of the article "how to write GO Project test". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Project file sequence content article guarantee study help test line business sum code information writing parameters multiple regular meeting beginning form Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Microsoft Shulou Tech Info OPPO Reno Shulou Technology