当前位置:网站首页>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错误提示
- 又一个行业被中国芯片打破空白,难怪美国模拟芯片龙头降价抛售了
- Stack application (balancer)
- R语言使用data函数获取当前R环境可用的示例数据集:获取datasets包中的所有示例数据集、获取所有包的数据集、获取特定包的数据集
- 使用tensorflow进行完整的DNN深度神经网络CNN训练完成图片识别案例
- Flink SQL knows why (17): Zeppelin, a sharp tool for developing Flink SQL
- SQL Injection (POST/Search)
- DQL basic query
- 双链笔记 RemNote 综合评测:快速输入、PDF 阅读、间隔重复/记忆
- Spark实战1:单节点本地模式搭建Spark运行环境
猜你喜欢

Unity EmbeddedBrowser浏览器插件事件通讯

Complete DNN deep neural network CNN training with tensorflow to complete image recognition cases

When updating mysql, the condition is a query

【电脑插入U盘或者内存卡显示无法格式化FAT32如何解决】

Unity embeddedbrowser browser plug-in event communication

Error running 'application' in idea running: the solution of command line is too long

使用tensorflow进行完整的DNN深度神经网络CNN训练完成图片识别案例

Internet of things completion -- (stm32f407 connects to cloud platform detection data)

Flink SQL knows why (17): Zeppelin, a sharp tool for developing Flink SQL

MyCms 自媒体商城 v3.4.1 发布,使用手册更新
随机推荐
SwiftUI 开发经验之作为一名程序员需要掌握的五个最有力的原则
php 迷宫游戏
Convolution emotion analysis task4
Golang — 命令行工具cobra
今日睡眠质量记录77分
Unity render streaming communicates with unity through JS
MySQL
Error running 'application' in idea running: the solution of command line is too long
双向链表(我们只需要关注插入和删除函数)
Box layout of Kivy tutorial BoxLayout arranges sub items in vertical or horizontal boxes (tutorial includes source code)
Ubuntu 14.04 下开启PHP错误提示
There is nothing new under the sun. Can the meta universe go higher?
Flutter动态化 | Fair 2.5.0 新版本特性
Logseq evaluation: advantages, disadvantages, evaluation, learning tutorial
R语言使用data函数获取当前R环境可用的示例数据集:获取datasets包中的所有示例数据集、获取所有包的数据集、获取特定包的数据集
Anan's doubts
untiy世界边缘的物体阴影闪动,靠近远点的物体阴影正常
MapReduce实现矩阵乘法–实现代码
Task6: using transformer for emotion analysis
阿南的疑惑