Golang测试技术

发表于:2015-5-25 11:36

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:bigwhite    来源:51Testing软件测试网采编

分享:
  3、T结构
  *testing.T参数用于错误报告:
  t.Errorf("got bar = %v, want %v", got, want)
  t.Fatalf("Frobnicate(%v) returned error: %v", arg, err)
  t.Logf("iteration %v", i)
  也可以用于enable并行测试(parallet test):
  t.Parallel()
  控制一个测试是否运行:
  if runtime.GOARCH == "arm" {
  t.Skip("this doesn't work on ARM")
  }
  4、运行测试
  我们用go test命令来运行特定包的测试。
  默认执行当前路径下包的测试代码。
  $ go test
  PASS
  $ go test -v
  === RUN TestIndex
  — PASS: TestIndex (0.00 seconds)
  PASS
  要运行工程下的所有测试,我们执行如下命令:
  $ go test github.com/nf/…
  标准库的测试:
  $ go test std
  注:假设strings_test.go的当前目录为testgo,在testgo目录下执行go test都是OK的。但如果我们切换到testgo的上一级目录执行go test,我们会得到什么结果呢?
  $go test testgo
  can't load package: package testgo: cannot find package "testgo" in any of:
  /usr/local/go/src/pkg/testgo (from $GOROOT)
  /Users/tony/Test/GoToolsProjects/src/testgo (from $GOPATH)
  提示找不到testgo这个包,go test后面接着的应该是一个包名,go test会在GOROOT和GOPATH下查找这个包并执行包的测试。
  5、测试覆盖率
  go tool命令可以报告测试覆盖率统计。
  我们在testgo下执行go test -cover,结果如下:
  go build _/Users/tony/Test/Go/testgo: no buildable Go source files in /Users/tony/Test/Go/testgo
  FAIL    _/Users/tony/Test/Go/testgo [build failed]
  显然通过cover参数选项计算测试覆盖率不仅需要测试代码,还要有被测对象(一般是函数)的源码文件。
  我们将目录切换到$GOROOT/src/pkg/strings下,执行go test -cover:
  $go test -v -cover
  === RUN TestReader
  — PASS: TestReader (0.00 seconds)
  … …
  === RUN: ExampleTrimPrefix
  — PASS: ExampleTrimPrefix (1.75us)
  PASS
  coverage: 96.9% of statements
  ok      strings    0.612s
  go test可以生成覆盖率的profile文件,这个文件可以被go tool cover工具解析。
  在$GOROOT/src/pkg/strings下面执行:
  $ go test -coverprofile=cover.out
  会再当前目录下生成cover.out文件。
  查看cover.out文件,有两种方法:
  a) cover -func=cover.out
  $sudo go tool cover -func=cover.out
  strings/reader.go:24:    Len                66.7%
  strings/reader.go:31:    Read                100.0%
  strings/reader.go:44:    ReadAt                100.0%
  strings/reader.go:59:    ReadByte            100.0%
  strings/reader.go:69:    UnreadByte            100.0%
  … …
  strings/strings.go:638:    Replace                100.0%
  strings/strings.go:674:    EqualFold            100.0%
  total:            (statements)            96.9%
  b) 可视化查看
  执行go tool cover -html=cover.out命令,会在/tmp目录下生成目录coverxxxxxxx,比如/tmp/cover404256298。目录下有一个 coverage.html文件。用浏览器打开coverage.html,即可以可视化的查看代码的测试覆盖情况。
  关于go tool的cover命令,我的go version go1.3 darwin/amd64默认并不自带,需要通过go get下载。
  $sudo GOPATH=/Users/tony/Test/GoToolsProjects go get code.google.com/p/go.tools/cmd/cover
  下载后,cover安装在$GOROOT/pkg/tool/darwin_amd64下面。
  二、高级测试技术
  1、一个例子程序
  outyet是一个web服务,用于宣告某个特定Go版本是否已经打标签发布了。其获取方法:
  go get github.com/golang/example/outyet
  注:
  go get执行后,cd $GOPATH/src/github.com/golang/example/outyet下,执行go run main.go。然后用浏览器打开http://localhost:8080即可访问该Web服务了。
  2、测试Http客户端和服务端
  net/http/httptest包提供了许多帮助函数,用于测试那些发送或处理Http请求的代码。
  3、httptest.Server
  httptest.Server在本地回环网口的一个系统选择的端口上listen。它常用于端到端的HTTP测试。
  type Server struct {
  URL      string // base URL of form http://ipaddr:port with no trailing slash
  Listener net.Listener
  // TLS is the optional TLS configuration, populated with a new config
  // after TLS is started. If set on an unstarted server before StartTLS
  // is called, existing fields are copied into the new config.
  TLS *tls.Config
  // Config may be changed after calling NewUnstartedServer and
  // before Start or StartTLS.
  Config *http.Server
  }
  func NewServer(handler http.Handler) *Server
  func (*Server) Close() error
  4、httptest.Server实战
  下面代码创建了一个临时Http Server,返回简单的Hello应答:
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Hello, client")
}))
defer ts.Close()
res, err := http.Get(ts.URL)
if err != nil {
log.Fatal(err)
}
greeting, err := ioutil.ReadAll(res.Body)
res.Body.Close()
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s", greeting)
  5、httptest.ResponseRecorder
  httptest.ResponseRecorder是http.ResponseWriter的一个实现,用来记录变化,用在测试的后续检视中。
  type ResponseRecorder struct {
  Code      int           // the HTTP response code from WriteHeader
  HeaderMap http.Header   // the HTTP response headers
  Body      *bytes.Buffer // if non-nil, the bytes.Buffer to append written data to
  Flushed   bool
  }
  6、httptest.ResponseRecorder实战
  向一个HTTP handler中传入一个ResponseRecorder,通过它我们可以来检视生成的应答。
  handler := func(w http.ResponseWriter, r *http.Request) {
  http.Error(w, "something failed", http.StatusInternalServerError)
  }
  req, err := http.NewRequest("GET", "http://example.com/foo", nil)
  if err != nil {
  log.Fatal(err)
  }
  w := httptest.NewRecorder()
  handler(w, req)
  fmt.Printf("%d – %s", w.Code, w.Body.String())
32/3<123>
重磅发布,2022软件测试行业现状调查报告~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2023
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号