当前位置:网站首页>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
边栏推荐
- Industrial resilience
- Understanding of class
- Introduction of buffer flow
- Jeecg menu path display problem
- Realize the reuse of components with different routing parameters and monitor the changes of routing parameters
- [Development Notes] cloud app control on device based on smart cloud 4G adapter gc211
- Introduction of transformation flow
- 技术干货 | AlphaFold/ RoseTTAFold开源复现(2)—AlphaFold流程分析和训练构建
- Some basic operations of reflection
- Take you through the whole process and comprehensively understand the software accidents that belong to testing
猜你喜欢
技术干货|昇思MindSpore Lite1.5 特性发布,带来全新端侧AI体验
项目经验分享:基于昇思MindSpore,使用DFCNN和CTC损失函数的声学模型实现
图像识别与检测--笔记
Understanding of class
PdfWriter. GetInstance throws system Nullreferenceexception [en] pdfwriter GetInstance throws System. NullRef
The concept of C language pointer
技术干货|昇思MindSpore NLP模型迁移之Bert模型—文本匹配任务(二):训练和评估
技术干货|昇思MindSpore初级课程上线:从基本概念到实操,1小时上手!
技术干货|利用昇思MindSpore复现ICCV2021 Best Paper Swin Transformer
Use of other streams
随机推荐
Leetcode 213: looting II
Analysis of the eighth Blue Bridge Cup single chip microcomputer provincial competition
Robots protocol
An overview of IfM Engage
Grpc message sending of vertx
圖像識別與檢測--筆記
Reconnaissance et détection d'images - Notes
[mindspire paper presentation] summary of training skills in AAAI long tail problem
Some experiences of Arduino soft serial port communication
Lucene introduces NFA
TreeMap
Usage of requests module
lucene scorer
Image recognition and detection -- Notes
IPv4 address
Analysis of the problems of the 12th Blue Bridge Cup single chip microcomputer provincial competition
PdfWriter. GetInstance throws system Nullreferenceexception [en] pdfwriter GetInstance throws System. NullRef
Lombok cooperates with @slf4j and logback to realize logging
Circuit, packet and message exchange
Read config configuration file of vertx