当前位置:网站首页>Go language unit test 3: go language uses gocovey library to do unit test
Go language unit test 3: go language uses gocovey library to do unit test
2022-07-03 13:45:00 【Lao Liu, you are so awesome】
One , Libraries used for installation
One ,goconvey Code address of the library
https://github.com/smartystreets/goconvey
2, Install... From the command line
[email protected]:~$ go get -u github.com/smartystreets/goconvey
explain : Liu Hongdi's go The forest is a focus golang The blog of ,
Address :https://blog.csdn.net/weixin_43881017
explain : author : Liu Hongdi mailbox : [email protected]
Two , Demonstrate project related information
1, Address :
https://github.com/liuhongdi/unittest02
2, Functional specifications : Demonstrated the use of goconvey Unit testing assert
3, Project structure : Pictured :
3、 ... and ,go Code instructions
1,main.go
package main
// Define an addition method
func Add(a, b int) int {
return a + b
}
2,main_test.go
package main
import (
"strconv"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
// test add Method
func TestAdd(t *testing.T) {
// List the parameters to be tested and the returned results
cases := []struct {
first int
second int
excepted int
}{
{1, 2, 3},
{1, 3, 4},
{2, 2, 4},
}
// Traverse , To test
for _, c := range cases {
result := Add(c.first, c.second)
if result != c.excepted {
t.Fatalf("add function failed, first: %d, second:%d, execpted:%d, result:%d", c.first, c.second, c.excepted, result)
}
}
}
// test add Method ,convey
func TestAddConvey(t *testing.T) {
// List the parameters to be tested and the returned results
cases := []struct {
first int
second int
excepted int
}{
{1, 2, 3},
{1, 3, 4},
{2, 2, 4},
}
// Traverse , To test
for _, c := range cases {
Convey(" Test sum result ", t, func() {
So(Add(c.first, c.second), ShouldEqual,c.excepted)
})
}
}
// test add Method ,convey nesting
func TestAddConveyNest(t *testing.T) {
// List the parameters to be tested and the returned results
cases := []struct {
first int
second int
excepted int
}{
{1, 2, 3},
{2, 3, 4},
{2, 2, 4},
}
Convey(" Start a set of test addition results ", t, func() {
// Traverse , To test
for _, c := range cases {
Convey(" Additive test :"+strconv.Itoa(c.first)+"+"+strconv.Itoa(c.second), func() {
So(Add(c.first, c.second), ShouldEqual,c.excepted)
})
}
})
}
Four , The test results :
Execute in the project directory :
[email protected]:/data/go/unittest02# go test -v ./...
return :
Display in case of error :
5、 ... and , View the version of the library :
module github.com/liuhongdi/unittest02
go 1.15
require (
github.com/smartystreets/goconvey v1.6.4
)
边栏推荐
- Ubuntu 14.04 下开启PHP错误提示
- PowerPoint 教程,如何在 PowerPoint 中将演示文稿另存为视频?
- AI 考高数得分 81,网友:AI 模型也免不了“内卷”!
- Shell timing script, starting from 0, CSV format data is regularly imported into PostgreSQL database shell script example
- Tutoriel PowerPoint, comment enregistrer une présentation sous forme de vidéo dans Powerpoint?
- Complete DNN deep neural network CNN training with tensorflow to complete image recognition cases
- 实现CNN图像的识别和训练通过tensorflow框架对cifar10数据集等方法的处理
- R语言使用data函数获取当前R环境可用的示例数据集:获取datasets包中的所有示例数据集、获取所有包的数据集、获取特定包的数据集
- Flutter dynamic | fair 2.5.0 new version features
- 8 Queen question
猜你喜欢
KEIL5出现中文字体乱码的解决方法
Several common optimization methods matlab principle and depth analysis
使用tensorflow进行完整的DNN深度神经网络CNN训练完成图片识别案例
SQL Injection (POST/Select)
Mycms we media mall v3.4.1 release, user manual update
TensorBoard可视化处理案例简析
Logseq evaluation: advantages, disadvantages, evaluation, learning tutorial
logback日志的整理
Multi table query of MySQL - multi table relationship and related exercises
Flutter dynamic | fair 2.5.0 new version features
随机推荐
服务器硬盘冷迁移后网卡无法启动问题
MySQL_ JDBC
R语言gt包和gtExtras包优雅地、漂亮地显示表格数据:nflreadr包以及gtExtras包的gt_plt_winloss函数可视化多个分组的输赢值以及内联图(inline plot)
Annotation and reflection
【BW16 应用篇】安信可BW16模组与开发板更新固件烧录说明
TensorBoard可视化处理案例简析
[556. Next larger element III]
Replace the GPU card number when pytorch loads the historical model, map_ Location settings
Flutter dynamic | fair 2.5.0 new version features
Disruptor -- a high concurrency and high performance queue framework for processing tens of millions of levels
The network card fails to start after the cold migration of the server hard disk
Road construction issues
There is nothing new under the sun. Can the meta universe go higher?
The shortage of graphics cards finally came to an end: 3070ti for more than 4000 yuan, 2000 yuan cheaper than the original price, and 3090ti
8 Queen question
php 迷宫游戏
Typeerror resolved: argument 'parser' has incorrect type (expected lxml.etree.\u baseparser, got type)
Flink SQL knows why (19): the transformation between table and datastream (with source code)
JS 将伪数组转换成数组
When updating mysql, the condition is a query