Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write GO project test

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report