In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article focuses on "what is the unit testing method of Go language". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what is the unit testing method of Go language"?
The Go language seems to be paranoid, sacrificing unnecessary flexibility and bringing some mandatory programming styles and conventions. For example:
Without any form of Makefile, the module is the directory, the package is the directory, and the compilation and configuration is the directory!
Not only is the directory used, but the file name can also be used for a specified purpose. File names with the suffix _ test.go are unit test files, and _ linux32.go is 32-bit linux-specific code.
Not only is the file name used, but the function name has a specific purpose. In the unit test file, the test function begins with Test.
Similar conventions are hardly pioneered by the go language, and there are similar specifications in some file formats. But for the time being, I don't know what other programming languages are so mandatory for programming styles.
For unit tests, suppose you have the following calc.go file:
Package calcfunc Add (int b int) int {return axib} func Max (int b int) (ret int) {ret = an if b > a {ret = b} return} func Min (int b int) (ret int) {ret = an if b
< a { ret = b } return} 有如下测试代码calc_test.go: package calcimport ( "testing")type calcTest struct { a,b,ret int}var addTests = []calcTest{ calcTest{4,6,10}, calcTest{5,6,11}, calcTest{8,-10,-2},}func TestAdd(t *testing.T) { for _,v := range addTests { ret := Add(v.a,v.b) if ret != v.ret { t.Errorf("%d add %d,want %d,but get %d",v.a,v.b,v.ret,ret) } }}func TestMax(t *testing.T){ a,b := 100,300 ret := Max(a,b) if ret != b { t.Errorf("%d is bigger than %d",b,a) }}func TestMin(t *testing.T) { a,b := 100,300 ret := Min(a,b) if ret != a { t.Errorf("%d is smaller than %d",a,b) }} 执行go test 得到如下结果: PASSok _/home/liangdi/Desktop/test/go/testing 0.008在calc.go中设计一个逻辑错误,把 func Min(a,b int) ret int 改为:func Min(a,b int) (ret int) { ret = a //逻辑错误 if b >A {ret = b} return}
The result of executing go test is:
Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.