当前位置:网站首页>Use of golang testing framework test
Use of golang testing framework test
2022-06-24 04:55:00 【Johns】
1. Why do I need an assertion Library ?
Officially :Go No assertion provided , We know this will bring some inconvenience , Its main purpose is to prevent you programmers from being lazy in error handling . It is convenient for us to introduce assertions —— Improve test efficiency , Enhance code readability .
testify Yes, it is go The realization of a assert Style test framework , This package provides the assertions we need , Provides very rich assertion methods , It is very simple to use and easy to understand .
2. How to use testify To assert that ?
- install
go get github.com/stretchr/testify
2.Quick start
package algo import ( "github.com/stretchr/testify/assert" "testing" ) /** * @Description * @Author guirongguo * @Email [email protected] * @Date 2021/8/30 23:00 **/ // Easy to use func TestSimpleRand(t *testing.T) { t.Log("start ...") assert := assert.New(t) assert.Equal(1, 1) assert.NotEqual(1, 2) assert.NotNil("123") assert.IsType([]string{}, []string{""}) assert.Contains("Hello World", "World") assert.Contains(map[string]string{"Hello": "World"}, "Hello") assert.Contains([]string{"Hello", "World"}, "Hello") assert.True(true) assert.True(false) t.Log("next ...") var s []string assert.Empty(s) assert.Nil(s) t.Log("end ...") } // Generally, the table driven method is used to put the test cases of the same unit together func TestCalculate(t *testing.T) { assert := assert.New(t) var tests = []struct { input int expected int }{ {2, 4}, {-1, 1}, {0, 2}, {-5, -3}, {99999, 100001}, } for _, test := range tests { assert.Equal(Calculate(test.input), test.expected) } }
Running results
=== RUN TestSimpleRand
simple_random_test.go:16: start ...
simple_random_test.go:27:
Error Trace: simple_random_test.go:27
Error: Should be true
Test: TestSimpleRand
simple_random_test.go:28: next ...
simple_random_test.go:32: end ...
--- FAIL: TestSimpleRand (0.00s)
=== RUN TestCalculate
simple_random_test.go:50:
Error Trace: simple_random_test.go:50
Error: Not equal:
expected: 1
actual : 4
Test: TestCalculate
simple_random_test.go:50:
Error Trace: simple_random_test.go:50
Error: Not equal:
expected: -2
actual : 1
Test: TestCalculate
simple_random_test.go:50:
Error Trace: simple_random_test.go:50
Error: Not equal:
expected: -1
actual : 2
Test: TestCalculate
simple_random_test.go:50:
Error Trace: simple_random_test.go:50
Error: Not equal:
expected: -6
actual : -3
Test: TestCalculate
simple_random_test.go:50:
Error Trace: simple_random_test.go:50
Error: Not equal:
expected: 99998
actual : 100001
Test: TestCalculate
--- FAIL: TestCalculate (0.00s)
Expected :99998
Actual :100001
<Click to see difference>
FAILYou can see that an assertion failure will throw an error message , And continue to execute the following assertions .
3. suite Kit package
github.com/stretchr/testify/suite Provides test suite functionality , You can perform actions at the beginning and end of the entire suite , You can also perform actions at the beginning and end of each test . Usage is as follows :
package algo import ( "fmt" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/suite" "testing" ) /** * @Description * @Author guirongguo * @Email [email protected] * @Date 2021/8/30 23:00 **/ type _Suite struct { suite.Suite } // SetupSuite() and TearDownSuite() The overall situation will only be executed once // SetupTest() TearDownTest() BeforeTest() AfterTest() Execute once for each test in the suite func (s *_Suite) AfterTest(suiteName, testName string) { fmt.Printf("AferTest: suiteName=%s,testName=%s\n", suiteName, testName) } func (s *_Suite) BeforeTest(suiteName, testName string) { fmt.Printf("BeforeTest: suiteName=%s,testName=%s\n", suiteName, testName) } // SetupSuite() Only once func (s *_Suite) SetupSuite() { fmt.Printf("SetupSuite() ...\n") } // TearDownSuite() Only once func (s *_Suite) TearDownSuite() { fmt.Printf("TearDowmnSuite()...\n") } func (s *_Suite) SetupTest() { fmt.Printf("SetupTest()... \n") } func (s *_Suite) TearDownTest() { fmt.Printf("TearDownTest()... \n") } func (s *_Suite) TestSimpleRand() { fmt.Printf("TestSimpleRand()... \n") ret := SimpleRand(1, 10) // 4. assert.Equal(s.T(), ret, int64(10)) } func (s *_Suite) TestCalculate() { fmt.Printf("TestCalculate()... \n") ret := Calculate(1) //9. assert.Equal(s.T(), ret, 0) } // Give Way go test Perform the test func TestAll(t *testing.T) { suite.Run(t, new(_Suite)) }
Running results
GOROOT=/usr/local/go #gosetup
GOPATH=/Users/guirong/go #gosetup
/usr/local/go/bin/go test -c -o /private/var/folders/kk/5llx7c2j0r90sp2hhlptlc1m0000gn/T/____Suite_in_testcase_demo_cmd_algo testcase-demo/cmd/algo #gosetup
/usr/local/go/bin/go tool test2json -t /private/var/folders/kk/5llx7c2j0r90sp2hhlptlc1m0000gn/T/____Suite_in_testcase_demo_cmd_algo -test.v -test.run ^\QTestAll\E$ -testify.m ^TestSimpleRand|TestCalculate$
=== RUN TestAll
SetupSuite() ...
--- PASS: TestAll (0.00s)
=== RUN TestAll/TestCalculate
SetupTest()...
BeforeTest: suiteName=_Suite,testName=TestCalculate
TestCalculate()...
AferTest: suiteName=_Suite,testName=TestCalculate
TearDownTest()...
--- PASS: TestAll/TestCalculate (0.00s)
=== RUN TestAll/TestSimpleRand
SetupTest()...
BeforeTest: suiteName=_Suite,testName=TestSimpleRand
TestSimpleRand()...
AferTest: suiteName=_Suite,testName=TestSimpleRand
TearDownTest()...
TearDowmnSuite()...
--- PASS: TestAll/TestSimpleRand (0.00s)
PASS
Process finished with exit code 0边栏推荐
- What is Ping? How can the server disable Ping?
- When remote, your resolution is lower than a × B. Some items may not be displayed on the screen
- 少儿编程教育在特定场景中的普及作用
- Brief introduction: how much do you know about supply chain attacks
- apipost接口断言详解
- Many regulations come into effect today! The main responsibility of network security will be further implemented
- What does IIS mean and what is its function? How does IIS set the size of the web site space on the server?
- Summary of Android interview questions in 2020 (intermediate)
- How to file ECS? What should be paid attention to when selecting ECS
- Black horse programmer machine learning handout: preliminary use of linear regression API
猜你喜欢

阿里云新一代云计算体系架构 CIPU 到底是啥?

阿里云混合云首席架构师张晓丹:政企混合云技术架构的演进和发展

『渗透基础』Cobalt Strike基础使用入门_Cobalt Strike联动msfconsole

重新认识WorkPlus,不止IM即时通讯,是企业移动应用管理专家

少儿编程课程改革后的培养方式

SAP mts/ato/mto/eto topic 10: ETO mode q+ empty mode unvalued inventory policy customization

让孩子们学习Steam 教育的应用精髓

Abnova多肽设计和合成解决方案

大一下学期期末总结(补充知识漏洞)

SAP MTS/ATO/MTO/ETO专题之七:ATO模式1 M+M模式策略用82(6892)
随机推荐
How to select a suitable optical fiber tester
Bi-sql distinct
Replication of variables in golang concurrency
What are the functions and advantages of the Internet of things cloud platform?
How to create an FTP server on the ECS? Is it safe to create an FTP server on the ECS?
Powerbi - for you who are learning
Verifying data models in golang
What are the advantages of ECS? Is ECS better than VM?
Analyze the actual user groups and demand positioning of distributed database products from the market and demand
LeetCode 1662. Check whether two string arrays are equal
How to add a domain name to ECS? What are the advantages of ECS?
集成阿里云短信服务以及报签名不合法的原因
Network timeout configuration method when PR and push are proposed
Training methods after the reform of children's programming course
Bi-sql and & or & in
Integration of Alibaba cloud SMS services and reasons for illegal message signing
How to enlarge the ECS page? How to select ECS instance specifications?
Black horse programmer machine learning handout: preliminary use of linear regression API
Pgbouncer lightweight PG connection pool management tool
Confluence data center version is nearing its lifecycle