当前位置:网站首页>Go language foundation ------ 14 ------ gotest
Go language foundation ------ 14 ------ gotest
2022-07-03 07:37:00 【Mango sauce】
1 precondition :
- 1、 The file name must be in "_test.go" ending .
- 2、 Method name must be "Test" Lead , And the formal parameter is (t *testing.T).
2 give an example
gotest.go( This is only the internal file used in the test file , So there's no need to _test.go ending ):
package mytest
import (
"errors"
)
func Division(a, b float64) (float64, error) {
if b == 0 {
return 0, errors.New(" The divisor cannot be zero 0")
}
return a / b, nil
}
This is the real test file :
gotest_test.go:
package mytest
import (
"testing"
)
func Test_Division_1(t *testing.T) {
if i, e := Division(6, 2); i != 3 || e != nil {
//try a unit test on function
t.Error(" Division function test failed ") // If it's not as expected, report an error
} else {
t.Log(" The first test passed ") // Record some information you expect to record
}
}
func Test_Division_2(t *testing.T) {
if _, e := Division(6, 0); e != nil {
//try a unit test on function
t.Error("Division did not work as expected.") // If it's not as expected, report an error
} else {
t.Log("one test passed.") // Record some information you expect to record
}
}
3 Go test test
1. Execute... In the directory go test Is the test directory all with XXX_test.go Final document .
2. Testing individual methods
go test -v -run="Test_Division_1" -count 5
3. view help go help test


adopt go help test You can see go test Instructions for use :
The format is like :
go test [-c] [-i] [build flags] [packages] [flags for test binary]
Parameter interpretation :
-c : compile go test Become an executable binary , But don't run tests .
-i : The installation test package depends on package, But don't run tests .
About build flags, call go help build, These are the parameters that need to be used in compiling and running , Generally set to empty
About packages, call go help packages, These are about package management , Generally set to empty
About flags for test binary, call go help testflag, These are go test Parameters often used in the process
-test.v : Whether to output all unit test cases ( No matter success or failure ), Default does not add , So just output failed unit test cases .
-test.run pattern: Which unit test cases only to run
-test.bench patten: Run only those performance test cases
-test.benchmem : Whether to output the memory condition during the performance test
-test.benchtime t : Time of performance test run , The default is 1s
-test.cpuprofile cpu.out : Whether the output cpu Performance analysis file
-test.memprofile mem.out : Whether to output memory performance analysis file
-test.blockprofile block.out : Whether to output internal goroutine Blocked performance analysis file
-test.memprofilerate n : During the memory performance analysis, there is a problem of how much memory is allocated before recording . This parameter is set to hit
Point memory allocation interval , That is to say profile In a sample Represents the memory size of . The default setting is 512 * 1024 Of . If you set it
by 1, Every memory block allocated will be in profile One of them is a little bit , So the generated profile Of sample There will be a lot of . If you set it to 0,
That's to say, we don't need to do some maintenance .
You can set up memprofilerate=1 and GOGC=off To turn off memory recycling , And observe the allocation of each memory block .
-test.blockprofilerate n: Basically the same as above , Control is goroutine The number of nanoseconds in the block . Default no setting is equivalent to -
test.blockprofilerate=1, Record every nanosecond
-test.parallel n : Program parallelism in performance testing cpu Count , Default equal to GOMAXPROCS. -test.timeout t : If the test case runs longer than t, Throw out panic
-test.cpu 1,2,4 : Where does the program run CPU above , Use binary 1 On behalf of , and nginx Of nginx_worker_cpu_affinity It's one
A reason
-test.short : Reduce the running time of test cases with longer running time
边栏推荐
- Partage de l'expérience du projet: mise en œuvre d'un pass optimisé pour la fusion IR de la couche mindstore
- Analysis of the ninth Blue Bridge Cup single chip microcomputer provincial competition
- 技术干货|昇思MindSpore可变序列长度的动态Transformer已发布!
- Rabbit MQ message sending of vertx
- Some basic operations of reflection
- 技术干货|AI框架动静态图统一的思考
- Arduino Serial系列函数 有关print read 的总结
- Understanding of class
- VMware virtual machine installation
- Usage of requests module
猜你喜欢

技术干货|昇思MindSpore初级课程上线:从基本概念到实操,1小时上手!

【LeetCode】2. Valid Parentheses·有效的括号

圖像識別與檢測--筆記

Common architectures of IO streams

Leetcode 198: house raiding

Introduction of transformation flow

PAT甲级 1027 Colors in Mars

Technical dry goods Shengsi mindspire innovation model EPP mvsnet high-precision and efficient 3D reconstruction

Introduction of buffer flow

Go language foundation ----- 01 ----- go language features
随机推荐
【CoppeliaSim4.3】C#调用 remoteApi控制场景中UR5
IndexSort
Use of other streams
The concept of C language pointer
Some basic operations of reflection
Map interface and method
技术干货|昇思MindSpore算子并行+异构并行,使能32卡训练2420亿参数模型
Technical dry goods Shengsi mindspire elementary course online: from basic concepts to practical operation, 1 hour to start!
PgSQL converts string to double type (to_number())
Use of generics
URL programming
PAT甲级 1030 Travel Plan
opensips与对方tls sip trunk对接注意事项
Technical dry goods Shengsi mindspire lite1.5 feature release, bringing a new end-to-end AI experience
基于RNA的新型癌症疗法介绍
Talk about floating
研究显示乳腺癌细胞更容易在患者睡觉时进入血液
Various postures of CS without online line
Technical dry goods Shengsi mindspire innovation model EPP mvsnet high-precision and efficient 3D reconstruction
Analysis of the problems of the 12th Blue Bridge Cup single chip microcomputer provincial competition