当前位置:网站首页>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
)
边栏推荐
- Annotation and reflection
- Task6: using transformer for emotion analysis
- Disruptor -- a high concurrency and high performance queue framework for processing tens of millions of levels
- Typeerror resolved: argument 'parser' has incorrect type (expected lxml.etree.\u baseparser, got type)
- SQL Injection (POST/Select)
- Unable to stop it, domestic chips have made another breakthrough, and some links have reached 4nm
- MapReduce实现矩阵乘法–实现代码
- Heap structure and heap sort heapify
- There is nothing new under the sun. Can the meta universe go higher?
- Logseq 评测:优点、缺点、评价、学习教程
猜你喜欢

常见的几种最优化方法Matlab原理和深度分析

KEIL5出现中文字体乱码的解决方法

logback日志的整理

Logseq evaluation: advantages, disadvantages, evaluation, learning tutorial

RichView TRVStyle ListStyle 列表样式(项目符号编号)

物联网毕设 --(STM32f407连接云平台检测数据)
![[sort] bucket sort](/img/52/95514b5a70cea75821883e016d8adf.jpg)
[sort] bucket sort
![[技术发展-24]:现有物联网通信技术特点](/img/f3/a219fe8e7438b8974d2226b4c3d4a4.png)
[技术发展-24]:现有物联网通信技术特点

Unable to stop it, domestic chips have made another breakthrough, and some links have reached 4nm

Resource Cost Optimization Practice of R & D team
随机推荐
Asp. Net core1.1 without project JSON, so as to generate cross platform packages
R language uses the data function to obtain the sample datasets available in the current R environment: obtain all the sample datasets in the datasets package, obtain the datasets of all packages, and
Red hat satellite 6: better management of servers and clouds
AI scores 81 in high scores. Netizens: AI model can't avoid "internal examination"!
Tutoriel PowerPoint, comment enregistrer une présentation sous forme de vidéo dans Powerpoint?
The difference between stratifiedkfold (classification) and kfold (regression)
HALCON联合C#检测表面缺陷——HALCON例程autobahn
The R language GT package and gtextras package gracefully and beautifully display tabular data: nflreadr package and gt of gtextras package_ plt_ The winloss function visualizes the win / loss values
Flutter dynamic | fair 2.5.0 new version features
The network card fails to start after the cold migration of the server hard disk
Golang — template
Red Hat Satellite 6:更好地管理服务器和云
[技术发展-24]:现有物联网通信技术特点
Swiftui development experience: the five most powerful principles that a programmer needs to master
Detailed explanation of multithreading
SQL Injection (POST/Search)
MySQL constraints
双链笔记 RemNote 综合评测:快速输入、PDF 阅读、间隔重复/记忆
R语言使用data函数获取当前R环境可用的示例数据集:获取datasets包中的所有示例数据集、获取所有包的数据集、获取特定包的数据集
106. How to improve the readability of SAP ui5 application routing URL