当前位置:网站首页>Introduction to go web programming: a probe into the excellent test library gocovey
Introduction to go web programming: a probe into the excellent test library gocovey
2022-06-30 21:44:00 【51CTO】
0 Preface
In order to make up for the defects of the built-in test library , An excellent third-party library was born goconvey, at present gtihub stars The quantity has reached 7.4k, The website links : http://goconvey.co/.
slogan :Write behavioral tests in your editor. Get live results in your browser.

GoConvey Perfect compatibility Go Built in testing library , Provide command line tools to simplify built-in test execution commands , The test will run automatically , Provide more intuitive Web Interface , The most important thing is to get the test report easily .
1 GoConvey Characteristics of
- Direct integration Go Built in testing tools , For example, it can be used directly
go test - A large number of regression test suites
- Provide readable , Color console output
- Completely automated Web UI
- Test code generator
- Desktop reminder ( Optional )
- Automatically run the automatic test script in the terminal
- Available immediately at Sublime Text Open the code line corresponding to the test problem in ( some assembly required)
2 Download and install
After successful installation, you will see the following output :
[email protected]:~/GoProjects$ go get github.com/smartystreets/goconvey/convey
go: downloading github.com/smartystreets/assertions v1.2.0
go: downloading github.com/jtolds/gls v4.20.0+incompatible
go: downloading github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1
go get: added github.com/smartystreets/goconvey v1.7.2
- 1.
- 2.
- 3.
- 4.
- 5.
3 How to use
The structure is as follows :

- Create a
utils.gofile , Write a sum of integers Sum function :
Then create a unit test file utils_test.go file :
package utils
import (
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func TestSum(t *testing.T) {
Convey("Test Sum", t, func() {
Convey("1 + 2 + 3 + 4 + 5", func() {
So(Sum(1, 2, 3, 4, 5), ShouldEqual, 15)
})
Convey("1 + 2022", func() {
So(Sum(1, 2022), ShouldEqual, 2023)
})
})
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- The first 6 In line With
.The method of importing the library simplifies the call . - The first 9 Line code , Unit test function naming, precautions and built-in library
testingBe consistent ( For example, test functions with Test_ start , The incoming parameter is*testing.T). - The first layer in the function body Convey Provide 3 Parameters :
Test Sum( Describe the name of the test )、tandfunc(). - Nested Convey The layer provides two parameters :
1 + 2 + 3 + 4 + 5( Describe the name of the test ) andfunc(). - Use So To determine the expected value and output , Use here
ShouldEqual.
package utils
import (
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func TestSum(t *testing.T) {
Convey("Test Sum", t, func() {
Convey("1 + 2 + 3 + 4 + 5", func() {
So(Sum(1, 2, 3, 4, 5), ShouldEqual, 15)
})
Convey("1 + 2022", func() {
So(Sum(1, 2022), ShouldEqual, 2023)
})
})
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
because GoConvey Well integrated Go Native tes Execute... In the terminal go test -v command , The results are as follows , The test passed :
The terminal has a very user-friendly interface with color , As shown in the figure :

assert methods
In addition to the ShouldEqual Outside method ,GoConvey It provides us with many kinds of assertion methods in So() Use in a function .
General equivalent
So(thing1, ShouldEqual, thing2)
So(thing1, ShouldNotEqual, thing2)
So(thing1, ShouldResemble, thing2) // For arrays 、 section 、map Equal to the structure
So(thing1, ShouldNotResemble, thing2)
So(thing1, ShouldPointTo, thing2)
So(thing1, ShouldNotPointTo, thing2)
So(thing1, ShouldBeNil)
So(thing1, ShouldNotBeNil)
So(thing1, ShouldBeTrue)
So(thing1, ShouldBeFalse)
So(thing1, ShouldBeZeroValue)
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
Numerical quantity comparison class
So(1, ShouldBeGreaterThan, 0)
So(1, ShouldBeGreaterThanOrEqualTo, 0)
So(1, ShouldBeLessThan, 2)
So(1, ShouldBeLessThanOrEqualTo, 2)
So(1.1, ShouldBeBetween, .8, 1.2)
So(1.1, ShouldNotBeBetween, 2, 3)
So(1.1, ShouldBeBetweenOrEqual, .9, 1.1)
So(1.1, ShouldNotBeBetweenOrEqual, 1000, 2000)
So(1.0, ShouldAlmostEqual, 0.99999999, .0001) // tolerance is optional; default 0.0000000001
So(1.0, ShouldNotAlmostEqual, 0.9, .0001)
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
Containing class
So([]int{2, 4, 6}, ShouldContain, 4)
So([]int{2, 4, 6}, ShouldNotContain, 5)
So(4, ShouldBeIn, ...[]int{2, 4, 6})
So(4, ShouldNotBeIn, ...[]int{1, 3, 5})
So([]int{}, ShouldBeEmpty)
So([]int{1}, ShouldNotBeEmpty)
So(map[string]string{"a": "b"}, ShouldContainKey, "a")
So(map[string]string{"a": "b"}, ShouldNotContainKey, "b")
So(map[string]string{"a": "b"}, ShouldNotBeEmpty)
So(map[string]string{}, ShouldBeEmpty)
So(map[string]string{"a": "b"}, ShouldHaveLength, 1) // supports map, slice, chan, and string
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
String class
So("asdf", ShouldStartWith, "as")
So("asdf", ShouldNotStartWith, "df")
So("asdf", ShouldEndWith, "df")
So("asdf", ShouldNotEndWith, "df")
So("asdf", ShouldContainSubstring, " Wait a moment ") // optional 'expected occurences' arguments?
So("asdf", ShouldNotContainSubstring, "er")
So("adsf", ShouldBeBlank)
So("asdf", ShouldNotBeBlank)
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
panic class
Type check class
Time and interval classes
So(time.Now(), ShouldHappenBefore, time.Now())
So(time.Now(), ShouldHappenOnOrBefore, time.Now())
So(time.Now(), ShouldHappenAfter, time.Now())
So(time.Now(), ShouldHappenOnOrAfter, time.Now())
So(time.Now(), ShouldHappenBetween, time.Now(), time.Now())
So(time.Now(), ShouldHappenOnOrBetween, time.Now(), time.Now())
So(time.Now(), ShouldNotHappenOnOrBetween, time.Now(), time.Now())
So(time.Now(), ShouldHappenWithin, duration, time.Now())
So(time.Now(), ShouldNotHappenWithin, duration, time.Now())
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
Custom assertion method
If none of the assertion methods listed above meet your needs , Then you can also customize an assertion method according to the following format .
Be careful :<> The content in is what you need to replace according to your actual needs .
func should<do-something>(actual interface{}, expected ...interface{}) string {
if <some-important-condition-is-met(actual, expected)> {
return "" // Returns an empty string indicating that the assertion passed
}
return "< Some descriptive messages specify why the assertion failed ...>"
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
4 The graphical interface
- After installing the library, the command line tool is automatically installed , If it fails , You can see GOBIN Whether to join PATH environment variable . Execute in the project directory goconvey It will start automatically Web service .

The default access address is http://127.0.0.1:8080/, Can be found in Web Specific information can be viewed on the interface , The following interface :

Specific contents include :
- Overall coverage
- Operation of a single test file
- Rerun test
- Coverage or non coverage of a single test file .
- visit
http://127.0.0.1:8080/reports/, You can see the test coverage :

Click on this. .txt file , The results are as follows :
- adopt
http://127.0.0.1:8080/composer.htmlWrite semi-automatic test cases :

5 summary
This paper introduces Go Language is very easy to use and powerful third-party library GoConvey, Then download and install , Finally, a simple addition function is used to complete the GoConvey Usage flow , The test library provides more assertion methods , It can be used in the actual development process .
By running goconvey command , You can run a powerful... Locally Web The graphical interface , Readers are advised to goconvey With built-in testing Use , To improve development efficiency .
Reference link
边栏推荐
- 物联网僵尸网络Gafgyt家族与物联网设备后门漏洞利用
- 1-7 Path路径模块
- 攻防演练中的防泄露全家福
- To the Sultanate of Anderson
- Sqlserver gets the data of numbers, Chinese and characters in the string
- Text recognition svtr paper interpretation
- 興奮神經遞質——穀氨酸與大腦健康
- 根据肠道微生物组重新思考健康饮食
- 看阿里云 CIPU 的 10 大能力
- Reading notes of Clickhouse principle analysis and Application Practice (2)
猜你喜欢

Nacos部署及使用

5G 在智慧医疗中的需求

PyTorch量化实践(2)

Go Web 编程入门: 一探优秀测试库 GoConvey
Understand what MySQL index push down (ICP) is in one article

Reading notes of Clickhouse principle analysis and Application Practice (3)

Introduce an online platform for multi omics integration and network visual analysis

How to move forward when facing confusion in scientific research? How to give full play to women's advantages in scientific research?

Reading notes of Clickhouse principle analysis and Application Practice (1)

clickhouse原生監控項,系統錶描述
随机推荐
Auto-created primary key used when not defining a primary key
Five years after graduation, I wondered if I would still be so anxious if I hadn't taken the test
Clickhouse Native Monitoring item, System table Description
Reading notes of Clickhouse principle analysis and Application Practice (1)
1-12 preliminary understanding of Express
Go Web 编程入门: 一探优秀测试库 GoConvey
1-20 预检请求
网络营销之四大误解
ssh 默认端口不是22时的一些问题
ca i啊几次哦啊句iu家哦
漫谈Clickhouse Join
The 16th Heilongjiang Provincial Collegiate Programming Contest
1-3 使用SQL管理数据库
FreeRTOS record (IX. an example of a bare metal project transferring to FreeRTOS)
开发属于自己的包
Prediction and regression of stacking integrated model
ca i啊几次哦啊句iu家哦11111
Summary of errors reported when using YML file to migrate CONDA environment
Ml & DL: introduction to hyperparametric optimization in machine learning and deep learning, evaluation index, over fitting phenomenon, and detailed introduction to commonly used parameter adjustment
jupyterbook 清空控制台输出