当前位置:网站首页>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
边栏推荐
- What does grade evaluation mean? What is included in the workflow?
- 物联网僵尸网络Gafgyt家族与物联网设备后门漏洞利用
- It is urgent for enterprises to protect API security
- Open source internship experience sharing: openeuler software package reinforcement test
- jupyterbook 清空控制台输出
- Sqlserver string type converted to decimal or integer type
- 1-2 install and configure MySQL related software
- 1-10 respond to client content according to different URLs
- 1-14 express托管静态资源
- ca i啊几次哦啊句iu家哦11111
猜你喜欢

Inventory the six second level capabilities of Huawei cloud gaussdb (for redis)

Radar data processing technology

1-2 安装并配置MySQL相关的软件

Nacos部署及使用

It is urgent for enterprises to protect API security

Go Web 编程入门: 一探优秀测试库 GoConvey
笔记【JUC包以及Future介绍】

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

asp.net core JWT传递

How to move forward when facing confusion in scientific research? How to give full play to women's advantages in scientific research?
随机推荐
请问,启牛证券开户,可以开户吗?安全吗?你想要的答案全在这里
周少剑,很少见
Go Web 编程入门: 一探优秀测试库 GoConvey
1-14 express托管静态资源
Oprator-1 first acquaintance with oprator
1-3 使用SQL管理数据库
Rethink healthy diet based on intestinal microbiome
1-11 create online file service
Some problems when SSH default port is not 22
AKK菌——下一代有益菌
Dm8: generate DM AWR Report
漫谈Clickhouse Join
Nacos部署及使用
Ml & DL: Introduction à l’optimisation des hyperparamètres, indice d’évaluation, phénomène de surajustement et introduction détaillée aux méthodes d’optimisation des paramètres couramment utilisées da
Introduce an online platform for multi omics integration and network visual analysis
将el-table原样导出为excel表格
【无标题】第一次参加csdn活动
Analysis and proposal on the "sour Fox" vulnerability attack weapon platform of the US National Security Agency
Arcmap|assign values to different categories of IDS with the field calculator
Auto-created primary key used when not defining a primary key