当前位置:网站首页>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
边栏推荐
- PAT甲级 1031 Hello World for U
- Leetcode 198: house raiding
- Rabbit MQ message sending of vertx
- Vertx restful style web router
- Jeecg data button permission settings
- Introduction of transformation flow
- Various postures of CS without online line
- Arduino 软串口通信 的几点体会
- Shengsi mindspire is upgraded again, the ultimate innovation of deep scientific computing
- Collector in ES (percentile / base)
猜你喜欢

技术干货|昇思MindSpore可变序列长度的动态Transformer已发布!

JS monitors empty objects and empty references

技术干货|关于AI Architecture未来的一些思考

Summary of Arduino serial functions related to print read

The embodiment of generics in inheritance and wildcards

Various postures of CS without online line

技术干货|利用昇思MindSpore复现ICCV2021 Best Paper Swin Transformer

Why is data service the direction of the next generation data center?

研究显示乳腺癌细胞更容易在患者睡觉时进入血液

【MySQL 12】MySQL 8.0.18 重新初始化
随机推荐
Use of other streams
The concept of C language pointer
【LeetCode】2. Valid Parentheses·有效的括号
Leetcode 213: 打家劫舍 II
Realize the reuse of components with different routing parameters and monitor the changes of routing parameters
Technical dry goods Shengsi mindspire lite1.5 feature release, bringing a new end-to-end AI experience
Some basic operations of reflection
Segment read
Technical dry goods Shengsi mindspire elementary course online: from basic concepts to practical operation, 1 hour to start!
Custom generic structure
Why is data service the direction of the next generation data center?
[Development Notes] cloud app control on device based on smart cloud 4G adapter gc211
VMWare网络模式-桥接,Host-Only,NAT网络
IPv4 address
HarmonyOS第三次培训笔记
项目经验分享:基于昇思MindSpore,使用DFCNN和CTC损失函数的声学模型实现
【CoppeliaSim4.3】C#调用 remoteApi控制场景中UR5
Lucene introduces NFA
技术干货|百行代码写BERT,昇思MindSpore能力大赏
Use of file class