In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Today, I will talk to you about how to write testable Go language code, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following content for you. I hope you can get something from this article.
The first test is "Hello Test!"
First, create the hello directory under our $GOPATH/src directory as the root directory for all the sample code covered in this article.
Then, create a new file named hello.go and define a function hello () that returns a sentence concatenated by several words:
Package hellofunc hello () string {words: = [] string {"hello", "func", "in", "package", "hello"} wl: = len (words) sentence: = "" for key, word: = range words {sentence + = word if key
< wl-1 { sentence += " " } else { sentence += "." } } return sentence} 接着,新建名为hello_test.go的文件,填入如下内容: package helloimport ( "fmt" "testing")func TestHello(t *testing.T) { got := hello() expect := "hello func in package hello." if got != expect { t.Errorf("got [%s] expected [%s]", got, expect) }}func BenchmarkHello(b *testing.B) { for i := 0; i < b.N; i++ { hello() }}func ExampleHello() { hl := hello() fmt.Println(hl) // Output: hello func in package hello.} 最后,打开终端,进入hello目录,输入go test命令并回车,可以看到如下输出: PASSok hello 0.007s 编写测试代码 Golang的测试代码位于某个包的源代码中名称以_test.go结尾的源文件里,测试代码包含测试函数、测试辅助代码和示例函数;测试函数有以Test开头的功能测试函数和以Benchmark开头的性能测试函数两种,测试辅助代码是为测试函数服务的公共函数、初始化函数、测试数据等,示例函数则是以Example开头的说明被测试函数用法的函数。 大部分情况下,测试代码是作为某个包的一部分,意味着它可以访问包中不可导出的元素。但在有需要的时候(如避免循环依赖)也可以修改测试文件的包名,如package hello的测试文件,包名可以设为package hello_test。 功能测试函数 功能测试函数需要接收*testing.T类型的单一参数t,testing.T 类型用来管理测试状态和支持格式化的测试日志。测试日志在测试执行过程中积累起来,完成后输出到标准错误输出。 下面是从Go标准库摘抄的 testing.T类型的常用方法的用法: 测试函数中的某条测试用例执行结果与预期不符时,调用t.Error()或t.Errorf()方法记录日志并标记测试失败 # /usr/local/go/src/bytes/compare_test.gofunc TestCompareIdenticalSlice(t *testing.T) { var b = []byte("Hello Gophers!") if Compare(b, b) != 0 { t.Error("b != b") } if Compare(b, b[:1]) != 1 { t.Error("b >B [: 1] failed ")}}
Use the t.Fatal () and t.Fatalf () methods to jump out of a test case after it fails
# / usr/local/go/src/bytes/reader_test.gofunc TestReadAfterBigSeek (t * testing.T) {r: = NewReader ([] byte ("0123456789")) if _, err: = r.Seek (1
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.