Golang- 调试篇
- 打包篇
这篇文章我们介绍下 Go 语言中如何进行测试。
单元测试
- 功能测试
testinggo test编写测试代码需遵循以下原则:
_test.gogo testtestingTestTestXxx()testing.Tfunc TestXxx (t *testing.T)XxxTestintdivtesting.TErrorErrorfFailNowFatalFatalIfLogstringutil.go
// Package stringutil contains utility functions for working with strings.package stringutil// Reverse returns its argument string reversed rune-wise left to right.func Reverse(s string) string {r := []rune(s)for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 {r[i], r[j] = r[j], r[i]}return string(r)}
那我们的测试用例可以写成下面这样:
package stringutilimport "testing"func TestReverse(t *testing.T) {const in, want = "Hello, world", "dlrow ,olleH"got := Reverse(in)if got != want {t.Errorf("Reverse(%q) == %q, want %q", in, got, want)}}
go test -v
=== RUN TestReverse--- PASS: TestReverse (0.00s)PASSok github.com/startover/testing 0.001s
- 基准测试
基准测试与功能测试类似,不过有以下几点需要注意:
func BenchmarkXXX(b *testing.B) { ... }go test-test.bench-test.bench="test_name_regex"go test -test.bench=".*"testing.B.N_test.go代码示例如下:
package stringutilimport "testing"func BenchmarkReverse(b *testing.B) {const in = "Hello, world"for n := 0; n < b.N; n++ {Reverse(in)}}
go test -v -test.bench=".*"
PASSBenchmarkReverse-4 5000000 260 ns/opok github.com/startover/testing 1.579s
- 表驱动测试
编写测试代码时,一个较好的办法是把测试的输入数据和期望的结果写在一起组成一个数据表:表中的每条记录都是一个含有输入和期望值的完整测试用例,有时还可以结合像测试名字这样的额外信息来让测试输出更多的信息。这就是表驱动测试。
Go 语言的 struct 字面值语法让我们可以轻松写出表驱动测试,代码示例如下:
package stringutilimport "testing"func TestTableReverse(t *testing.T) {for _, c := range []struct {in, want string}{{"Hello, world", "dlrow ,olleH"},{"Hello, 世界", "界世 ,olleH"},{"", ""},} {got := Reverse(c.in)if got != c.want {t.Errorf("Reverse(%q) == %q, want %q", c.in, got, c.want)}}}
- 测试覆盖率
go test -v -cover
$ go test -v -cover=== RUN TestReverse--- PASS: TestReverse (0.00s)=== RUN TestTableReverse--- PASS: TestTableReverse (0.00s)PASScoverage: 100.0% of statementsok github.com/startover/testing 0.004s
go testcover
$ go test -coverprofile=cover.out$ go tool cover -func=cover.outgithub.com/startover/testing/stringutil.go:5: Reverse 100.0%total: (statements) 100.0%
BDD 测试
Go 语言比较主流的 BDD 测试框架主要有:GoConvey 和 Ginkgo。下面让我们感受下 BDD 风格的测试代码:
package stringutilimport ("testing". "github.com/smartystreets/goconvey/convey")func TestSpec(t *testing.T) {// Only pass t into top-level Convey callsConvey("Given some ASCII and UTF8 strings", t, func() {const in, want = "Hello, world", "dlrow ,olleH"Convey("The value should be equal the reversed one", func() {got := Reverse(in)So(got, ShouldEqual, want)})})}
package stringutilimport (. "github.com/onsi/ginkgo". "github.com/onsi/gomega")var _ = Describe("StringutilTest", func() {var in, want stringBeforeEach(func() {const in, want = "Hello, world", "dlrow ,olleH"})Describe("With ASCII and UTF8 strings defined", func() {Context("Reverse the give strings", func() {It("should be reversed", func() {got := Reverse(in)Expect(got).To(Equal(want))})})})})
ginkgo -rgo test相关链接:
https://talks.golang.org/2014/testing.slide#1
https://nathany.com/go-testing-toolbox/
http://codethoughts.info/go/2015/04/05/how-to-test-go-code/
https://github.com/astaxie/build-web-application-with-golang/blob/master/zh/11.3.md