当前位置:网站首页>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
边栏推荐
- 用yml文件进行conda迁移环境时的报错小结
- Why have the intelligent investment advisory products collectively taken off the shelves of banks become "chicken ribs"?
- Oprator-1 first acquaintance with oprator
- 【回溯】全排列 II leetcode47
- 物联网僵尸网络Gafgyt家族与物联网设备后门漏洞利用
- 1-21 jsonp interface
- 给苏丹国安德森苏丹的撒过 d s g
- Can flinksql two Kafka streams join?
- 布隆过滤器
- 1-19 利用CORS解决接口跨域问题
猜你喜欢

jupyterbook 清空控制台输出

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

《ClickHouse原理解析与应用实践》读书笔记(2)

布隆过滤器

Why have the intelligent investment advisory products collectively taken off the shelves of banks become "chicken ribs"?

USBCAN分析仪的配套CAN和CANFD综合测试软件LKMaster软件解决工程师CAN总线测试难题

Jupyter notebook/lab switch CONDA environment

Phoenix architecture: an architect's perspective

5G 在智慧医疗中的需求

Text recognition svtr paper interpretation
随机推荐
《ClickHouse原理解析与应用实践》读书笔记(2)
【回溯】全排列 II leetcode47
《ClickHouse原理解析与应用实践》读书笔记(1)
Jupyter notebook/lab switch CONDA environment
Three techniques for reducing debugging time of embedded software
程序员女友给我做了一个疲劳驾驶检测
针对美国国家安全局“酸狐狸”漏洞攻击武器平台的分析与应对方案建议
To the Sultanate of Anderson
qsort函数和模拟实现qsort函数
Is it safe to open an account for stock trading on mobile phones?
网络营销之四大误解
Go Web 编程入门: 一探优秀测试库 GoConvey
Document Layout Analysis: A Comprehensive Survey 2019论文学习总结
sdfsdf
The Jenkins download Plug-in can't be downloaded. Solution
Jupyterbook clear console output
【无标题】
A group of K inverted linked lists
Why have the intelligent investment advisory products collectively taken off the shelves of banks become "chicken ribs"?
12345