go的测试编写、断言、性能测试

本文涉及的产品
性能测试 PTS,5000VUM额度
简介: go的测试编写、断言、性能测试

一、要点

源文件以_test结尾:xxx_test.go

测试方法名以Test开头:func TestXXX(t *testing.T){...}

二、例子

package constant_test
 
import "testing"
 
const (
  Monday = iota + 1
  Tuesday
  Wednesday
)
const (
  Readable = 1 << iota
  Writable
  Executable
)
 
func TestConstantTry(t *testing.T) {
  t.Log(Monday, Tuesday, Wednesday)
}
 
func TestConstantTry1(t *testing.T) {
  a := 7 //o111
  t.Log(a&Readable == Readable, a&Writable == Writable, a&Executable == Executable)
}
 
 
=== RUN   TestConstantTry
    constant_try_test.go:17: 1 2 3
--- PASS: TestConstantTry (0.00s)
=== RUN   TestConstantTry1
    constant_try_test.go:22: true true true
--- PASS: TestConstantTry1 (0.00s)
PASS
package fib
 
import (
  "fmt"
  "testing"
)
 
//菲波拉数列 变量赋值
func TestFibList(t *testing.T) {
  //var a int = 1
  //var b int = 1
  //var(
  //  a int=1
  //  b=1
  //)
  a := 1
  b := 1
  fmt.Print(a, " ")
  for i := 0; i < 5; i++ {
    fmt.Print(" ", b)
    tem := a
    a = b
    b = tem + a
  }
  fmt.Println()
}
 
func TestExchange(t *testing.T) {
  a := 1
  b := 2
  a, b = b, a
  t.Log(a, b)
}
 
=== RUN   TestFibList
1  1 2 3 5 8
--- PASS: TestFibList (0.00s)
=== RUN   TestExchange
    fib_test.go:32: 2 1
--- PASS: TestExchange (0.00s)
PASS
package try_test
 
import "testing"
 
func TestFirstTry(t *testing.T) {
  t.Log("My first try!")
}
=== RUN   TestFirstTry
    first_test.go:6: My first try!
--- PASS: TestFirstTry (0.00s)
PASS

三、Fail、Fatal

package testing
 
func square(op int) int {
  return op * op
}
package testing
 
import (
  "fmt"
  "testing"
)
 
//Fail,Error:该测试失败,该测试继续,其他测试继续执行
//FailNow,Fatal:该测试失败,该测试中止,其他测试继续执行
func TestSquare(t *testing.T) {
  inputs := [...]int{1, 2, 3}
  expected := [...]int{1, 2, 3}
  for i := 0; i < len(inputs); i++ {
    ret := square(inputs[i])
    if ret != expected[i] {
      t.Errorf("iput is %d,the expected is %d,the actual %d", inputs[i], expected[i], ret)
    }
  }
}
 
func TestErrorInCode(t *testing.T) {
  fmt.Println("start")
  t.Error("Error")
  fmt.Println("end")
}
 
func TestFailInCode(t *testing.T) {
  fmt.Println("start")
  t.Fatal("Error")
  fmt.Println("end")
}
=== RUN   TestSquare
    function_test.go:16: iput is 2,the expected is 2,the actual 4
    function_test.go:16: iput is 3,the expected is 3,the actual 9
--- FAIL: TestSquare (0.00s)
 
=== RUN   TestErrorInCode
start
    function_test.go:23: Error
end
--- FAIL: TestErrorInCode (0.00s)
 
=== RUN   TestFailInCode
start
    function_test.go:29: Error
--- FAIL: TestFailInCode (0.00s)
 
FAIL

四、代码测试覆盖率

go test -v -cover

五、断言

安装依赖

go get -u github.com/stretchr/testify/assert

//使用断言
func TestSquareWithAssert(t *testing.T) {
  inputs := [...]int{1, 2, 3}
  expected := [...]int{1, 4, 9}
  for i := 0; i < len(inputs); i++ {
    ret := square(inputs[i])
    assert.Equal(t, expected[i], ret)
  }
}
=== RUN   TestSquareWithAssert
--- PASS: TestSquareWithAssert (0.00s)
PASS

六、Benchmark性能测试

func BenchmarkConcatStringByAdd(b *testing.B) {
 
  elems := []string{"1", "2", "3", "4", "5"}
  b.ResetTimer()
  for i := 0; i < b.N; i++ {
    ret := ""
    for _, elem := range elems {
      ret += elem
    }
  }
  b.StopTimer()
}
func BenchmarkConcatStringByBytesBuffer(b *testing.B) {
  elems := []string{"1", "2", "3", "4", "5"}
  b.ResetTimer()
  for i := 0; i < b.N; i++ {
    var buf bytes.Buffer
    for _, elem := range elems {
      buf.WriteString(elem)
    }
  }
  b.StopTimer()
}

使用bytes.Buffer处理字符串,性能更优。

goos: windows
goarch: amd64
pkg: jike_introduction/src/ch35/benchmark
cpu: AMD Ryzen 7 4800H with Radeon Graphics
BenchmarkConcatStringByAdd
BenchmarkConcatStringByAdd-16           10294040               117.0 ns/op
PASS
 
goos: windows
goarch: amd64
pkg: jike_introduction/src/ch35/benchmark
cpu: AMD Ryzen 7 4800H with Radeon Graphics
BenchmarkConcatStringByBytesBuffer
BenchmarkConcatStringByBytesBuffer-16           16814658                70.41 ns
/op
PASS

七、BDD

安装依赖

go get -u github.com/smartystreets/goconvey/convey

测试编码

import (
  . "github.com/smartystreets/goconvey/convey"
  "testing"
)
 
func TestSpec(t *testing.T) {
  Convey("Given 2 even number", t, func() {
    a := 2
    b := 4
    Convey("when add two numbers", func() {
      c := a + b
      Convey("Then the result is still even", func() {
        So(c%2, ShouldEqual, 0)
      })
    })
  })
}

运行结果

=== RUN   TestSpec
.
1 total assertion
 
--- PASS: TestSpec (0.00s)
PASS
修改数值,错误结果
=== RUN   TestSpec
x
Failures:
 
  * F:/myCode/goland/jike_introduction/src/ch36/bdd/bdd_spec_test.go 
  Line 15:
  Expected: '0'
  Actual:   '1'
  (Should be equal)
 
 
1 total assertion
 
--- FAIL: TestSpec (0.00s)
 
FAIL

相关实践学习
通过性能测试PTS对云服务器ECS进行规格选择与性能压测
本文为您介绍如何利用性能测试PTS对云服务器ECS进行规格选择与性能压测。
目录
相关文章
|
1月前
|
SQL 安全 数据库连接
《Go 简易速速上手小册》第6章:错误处理和测试(2024 最新版)(上)
《Go 简易速速上手小册》第6章:错误处理和测试(2024 最新版)
52 1
|
1月前
|
JSON 测试技术 Go
《Go 简易速速上手小册》第6章:错误处理和测试(2024 最新版)(下)
《Go 简易速速上手小册》第6章:错误处理和测试(2024 最新版)
48 0
|
3天前
|
测试技术 Windows
软件测试之 性能测试 性能测试基础指标 Loadrunner、Jmeter等工具(下)
软件测试之 性能测试 性能测试基础指标 Loadrunner、Jmeter等工具(下)
6 2
|
1天前
|
存储 测试技术
【工作实践(多线程)】十个线程任务生成720w测试数据对系统进行性能测试
【工作实践(多线程)】十个线程任务生成720w测试数据对系统进行性能测试
9 0
【工作实践(多线程)】十个线程任务生成720w测试数据对系统进行性能测试
|
3天前
|
测试技术 程序员
软件测试之 性能测试 性能测试基础指标 Loadrunner、Jmeter等工具(上)
软件测试之 性能测试 性能测试基础指标 Loadrunner、Jmeter等工具(上)
10 1
|
1月前
|
IDE 测试技术 Go
【字节跳动青训营】后端笔记整理-3 | Go语言工程实践之测试
用于验证已经修改或新增功能后,软件的既有功能是否受到影响。
70 2
|
1月前
|
Java 数据挖掘 关系型数据库
软件测试——性能测试2
软件测试——性能测试
28 0
|
1月前
|
数据可视化 Java 测试技术
软件测试——性能测试1
软件测试——性能测试
30 0
|
1月前
|
安全 测试技术 Go
Golang深入浅出之-Go语言单元测试与基准测试:testing包详解
【4月更文挑战第27天】Go语言的`testing`包是单元测试和基准测试的核心,简化了测试流程并鼓励编写高质量测试代码。本文介绍了测试文件命名规范、常用断言方法,以及如何进行基准测试。同时,讨论了测试中常见的问题,如状态干扰、并发同步、依赖外部服务和测试覆盖率低,并提出了相应的避免策略,包括使用`t.Cleanup`、`t.Parallel()`、模拟对象和检查覆盖率。良好的测试实践能提升代码质量和项目稳定性。
30 1
|
1月前
|
分布式计算 安全 Hadoop
Hadoop节点网络性能测试时延测试
【4月更文挑战第22天】
48 2