当前位置:网站首页>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
边栏推荐
- 1-20 pre inspection request
- Coefficient of variation method matlab code [easy to understand]
- What happens when word encounters an error while trying to open a file?
- 1-12 preliminary understanding of Express
- Dm8: generate DM AWR Report
- What does grade evaluation mean? What is included in the workflow?
- 将el-table原样导出为excel表格
- 1-16 路由的概念
- 周少剑,很少见
- 的撒啊苏丹看老司机
猜你喜欢

Clickhouse Native Monitoring item, System table Description

A comprehensive understanding of gout: symptoms, risk factors, pathogenesis and management

兴奋神经递质——谷氨酸与大脑健康

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

jenkins下载插件下载不了,解决办法

興奮神經遞質——穀氨酸與大腦健康

介绍一款|用于多组学整合和网络可视化分析的在线平台

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

Troubleshooting the problem of pytorch geometric torch scatter and torch spark installation errors

Why have the intelligent investment advisory products collectively taken off the shelves of banks become "chicken ribs"?
随机推荐
Open the jupyter notebook/lab and FAQ & settings on the remote server with the local browser
Neurotransmetteurs excitateurs - glutamate et santé cérébrale
.netcore redis GEO类型
clickhouse原生監控項,系統錶描述
Five years after graduation, I wondered if I would still be so anxious if I hadn't taken the test
Anaconda下安装Jupyter notebook
PyTorch量化实践(2)
1-2 安装并配置MySQL相关的软件
How to run jenkins build, in multiple servers with ssh-key
漫谈Clickhouse Join
Zaah Sultan looks at the old driver
ceshi deces
pytorch geometric torch-scatter和torch-sparse安装报错问题解决
1-18 create the most basic express server & API module for creating routes
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
[untitled]
A comprehensive understanding of gout: symptoms, risk factors, pathogenesis and management
It is urgent for enterprises to protect API security
ca i啊几次哦啊句iu家哦
给苏丹国安德森苏丹的撒过 d s g