时间:2019-11-08 11:28  编辑:admin
  • 简介
  • 单位测试
  • 机能测试
  • 机能剖析
  • 测试掩盖率
  • 总结
  • 当前局部的代码

简介

一样平常开辟中, 测试是没有能短少的.

Drupal8 尺度库中有1个叫做 testing 的测试框架, 能够用于单位测试战机能测试.
它是战下令 go test 散成利用的.

测试文件是今后缀 _test.go 定名的, 一般战被测试的文件放正在统一个包中.

单位测试

单位测试的格局形如:

func TestAbs(t *testing.T) {
  got := Abs(⑴)
  if got != 1 {
    t.Errorf("Abs(⑴) = %d; want 1", got)
  }
}

正在 util 目次下创立1个文件 util_test.go, 加减1个单位测试:

package util

import "testing"

// 一般的测试
func TestGenShortID(t *testing.T) {
    shortID, err := GenShortID()
    if shortID == "" || err != nil {
        t.Error("GenShortID failed")
    }
}

然后, 正在根目次下运转 go test -v ./util/, 测试了局以下:

root@592402321ce7:/workspace# go test -v ./util/
=== RUN   TestGenShortID
--- PASS: TestGenShortID (0.00s)
PASS
ok      tzh.com/web/util        0.006s

机能测试

机能测试的了局形如:

func BenchmarkHello(b *testing.B) {
  for i := 0; i < b.N; i++ {
    fmt.Sprintf("hello")
  }
}

正在 util_test.go 加减机能测试:

// 机能测试
func BenchmarkGenShortID(b *testing.B) {
    for i := 0; i < b.N; i++ {
        GenShortID()
    }
}

运转了局以下(利用 --run=none 躲免运转一般的测试函数, 果为1般没有大概有函数名婚配 none):

root@592402321ce7:/workspace# go test -v -bench="BenchmarkGenShortID$" --run=none ./util/
goos: linux
goarch: amd64
pkg: tzh.com/web/util
BenchmarkGenShortID⑵             507237              2352 ns/op
PASS
ok      tzh.com/web/util        1.229s

那道明, 仄均每次运转 GenShortID() 必要 2352 纳秒.

机能剖析

运转测试的时分, 能够指定1些参数, 死成机能文件 profile.

-blockprofile block.out
    Write a goroutine blocking profile to the specified file
    when all tests are complete.
    Writes test binary as -c would.

-blockprofilerate n
    Control the detail provided in goroutine blocking profiles by
    calling runtime.SetBlockProfileRate with n.
    See 'go doc runtime.SetBlockProfileRate'.
    The profiler aims to sample, on average, one blocking event every
    n nanoseconds the program spends blocked. By default,
    if -test.blockprofile is set without this flag, all blocking events
    are recorded, equivalent to -test.blockprofilerate=1.

-coverprofile cover.out
    Write a coverage profile to the file after all tests have passed.
    Sets -cover.

-cpuprofile cpu.out
    Write a CPU profile to the specified file before exiting.
    Writes test binary as -c would.

-memprofile mem.out
    Write an allocation profile to the file after all tests have passed.
    Writes test binary as -c would.

-memprofilerate n
    Enable more precise (and expensive) memory allocation profiles by
    setting runtime.MemProfileRate. See 'go doc runtime.MemProfileRate'.
    To profile all memory allocations, use -test.memprofilerate=1.

-mutexprofile mutex.out
    Write a mutex contention profile to the specified file
    when all tests are complete.
    Writes test binary as -c would.

-mutexprofilefraction n
    Sample 1 in n stack traces of goroutines holding a
    contended mutex.
标签: Drupal 中文  

Drupal安全更新

亚艾元软件以Drupal为基础,推动Drupal的中国化、Drupal教程中文化、将Drupal与微信相结合,服务广大的中国Drupal用户。

最近Drupal安全升级频繁,亚艾元提醒Drupal用户,及时升级安全补丁。


业务联系:崔克俊
手机: 13381113196
QQ: 372948992
公司电话:010-62669785

亚艾元象棋谱


亚艾元象棋谱,是亚艾元软件的一个实验项
目,以中国象棋棋谱为实例,用来探索最新
的Drupal网站开发、微信公众号开发、微信
公小程序开发、APP开发相关技术