用GoConvey编写单元测试的一些总结

一、尽量用Convey将所有测试用例的Convey汇总

用Convey嵌套的方法,将所有测试用例的Convey用一个大的Convey包裹起来,每个测试函数下只有一个大的Convey。比如下面的示例代码:

import ("testing". "github.com/smartystreets/goconvey/convey"
)func TestStringSliceEqual(t *testing.T) {Convey("TestStringSliceEqual", t, func() {Convey("should return true when a != nil  && b != nil", func() {a := []string{"hello", "goconvey"}b := []string{"hello", "goconvey"}So(StringSliceEqual(a, b), ShouldBeTrue)})Convey("should return true when a == nil  && b == nil", func() {So(StringSliceEqual(nil, nil), ShouldBeTrue)})Convey("should return false when a == nil  && b != nil", func() {a := []string(nil)b := []string{}So(StringSliceEqual(a, b), ShouldBeFalse)})Convey("should return false when a != nil  && b != nil", func() {a := []string{"hello", "world"}b := []string{"hello", "goconvey"}So(StringSliceEqual(a, b), ShouldBeFalse)})})
}

这样做的好处是,看单测结果更为清晰直观:

=== RUN   TestStringSliceEqualTestStringSliceEqual should return true when a != nil  && b != nil ✔should return true when a == nil  && b == nil ✔should return false when a == nil  && b != nil ✔should return false when a != nil  && b != nil ✔4 total assertions--- PASS: TestStringSliceEqual (0.00s)
PASS
ok      infra/alg       0.006s

二、用GWT结构来描述复杂的测试用例

GWT结构嵌套了三层Convey:最外层是Given层,用来给定测试用例需要的数据;中间一层是When层,用来执行被测试的函数方法,得到result;最内层是Then层,用So来对result进行断言,看结果是否满足期望。

1 示例代码

示例代码如下:

func TestStringSliceEqualIfBothNil(t *testing.T) {Convey("Given two string slice which are both nil", t, func() {var a []string = nilvar b []string = nilConvey("When the comparision is done", func() {result := StringSliceEqual(a, b)Convey("Then the result should be true", func() {So(result, ShouldBeTrue)})})})
}func TestStringSliceNotEqualIfNotBothNil(t *testing.T) {Convey("Given two string slice which are both nil", t, func() {a := []string(nil)b := []string{}Convey("When the comparision is done", func() {result := StringSliceEqual(a, b)Convey("Then the result should be false", func() {So(result, ShouldBeFalse)})})})
}func TestStringSliceNotEqualIfBothNotNil(t *testing.T) {Convey("Given two string slice which are both not nil", t, func() {a := []string{"hello", "world"}b := []string{"hello", "goconvey"}Convey("When the comparision is done", func() {result := StringSliceEqual(a, b)Convey("Then the result should be false", func() {So(result, ShouldBeFalse)})})})
}

在实际运用中,可以结合第一条方法构成四层嵌套来描述一个测试用例:

func TestStringSliceEqual(t *testing.T) {Convey("TestStringSliceEqualIfBothNotNil", t, func() {Convey("Given two string slice which are both not nil", func() {a := []string{"hello", "goconvey"}b := []string{"hello", "goconvey"}Convey("When the comparision is done", func() {result := StringSliceEqual(a, b)Convey("Then the result should be true", func() {So(result, ShouldBeTrue)})})})})Convey("TestStringSliceEqualIfBothNil", t, func() {Convey("Given two string slice which are both nil", func() {var a []string = nilvar b []string = nilConvey("When the comparision is done", func() {result := StringSliceEqual(a, b)Convey("Then the result should be true", func() {So(result, ShouldBeTrue)})})})})Convey("TestStringSliceNotEqualIfNotBothNil", t, func() {Convey("Given two string slice which are both nil", func() {a := []string(nil)b := []string{}Convey("When the comparision is done", func() {result := StringSliceEqual(a, b)Convey("Then the result should be false", func() {So(result, ShouldBeFalse)})})})})Convey("TestStringSliceNotEqualIfBothNotNil", t, func() {Convey("Given two string slice which are both not nil", func() {a := []string{"hello", "world"}b := []string{"hello", "goconvey"}Convey("When the comparision is done", func() {result := StringSliceEqual(a, b)Convey("Then the result should be false", func() {So(result, ShouldBeFalse)})})})})}

 2 大坑

注意!Given层中最好只有一个Then,因为多个Then会导致每执行完一个Then就会再次执行一遍被测试的函数方法,导致多次执行的结果可能并不相同从而导致意料之外的错误(比如上面示例中的“result := StringSliceEqual(a, b)”)。所以如果选择使用GWT的结构,那么就要保证W中只有一个T,最好也要保证G中只有一个W。

三、自定义断言函数

断言函数So中第二个参数Assertion类型定义:

type Assertion func(actual interface{}, expected ...interface{}) string

返回空字符串表示断言成功,否则就是断言失败了。

1 自定义断言函数

所以我们自定义断言函数时也要注意这点,下面是一个参考示例:

func ShouldSummerBeComming(actual interface{}, expected ...interface{}) string {if actual == "summer" && expected[0] == "comming" {return ""} else {return "summer is not comming!"}
}

上述代码中,第一个条件表示断言成功,其它所有情况都是断言失败。

2 在So中使用自定义的断言函数

func TestSummer(t *testing.T) {Convey("TestSummer", t, func() {So("summer", ShouldSummerBeComming, "comming")So("winter", ShouldSummerBeComming, "comming")})
}

测试结果:

=== RUN   TestSummerTestSummer ✔✘Failures:* /Users/zhangxiaolong/Desktop/D/go-workspace/src/infra/alg/slice_test.go Line 52:summer is not comming!2 total assertions--- FAIL: TestSummer (0.00s)
FAIL
exit status 1
FAIL    infra/alg       0.006s

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/81612.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Linux--线程(与进程区别)

Linux线程 1、线程与进程 进程可以看成只有一个控制线程:一个进程同时只做一件事情。有了多个控制线程后,可以把进程设计成在同一时刻做不止一件事,每个线程各自处理独立的任务。 进程是程序执行时的一个实例,是分配系统资源&am…

【面试题】智力题

文章目录 腾讯1000瓶毒药里面只有1瓶是有毒的,问需要多少只老鼠才能在24小时后试出那瓶有毒。有两根不规则的绳子,两根绳子从头烧到尾均需要一个小时,现在有一个45分钟的比赛,裁判员忘记带计时器,你能否通过烧绳子的方…

C++---异常处理

异常处理 异常处理try语句块和throw表达式异常的抛出和捕获异常的抛出和匹配原则 异常安全异常规范标准异常 异常处理 异常是指存在于运行时的反常行为,这些行为超出了函数正常功能的范围。当程序的某部分检测到一个他无法处理的问题时,需要用到异常处理…

transforms数据预处理【图像增强】 ->(个人学习记录笔记)

文章目录 1. 安装2. transforms——Crop 裁剪2.1 transforms.CenterCrop2.2 transforms.RandomCrop2.3 transforms.RandomResizedCrop2.4 transforms.FiveCrop2.5 transforms.TenCrop 3. transforms——Flip 翻转3.1 transforms.RandomHorizontalFlip3.2 transforms.RandomVert…

leetcode 817. 链表组件(java)

链表组件 题目描述HashSet 模拟 题目描述 给定链表头结点 head,该链表上的每个结点都有一个 唯一的整型值 。同时给定列表 nums,该列表是上述链表中整型值的一个子集。 返回列表 nums 中组件的个数,这里对组件的定义为:链表中一段…

【学习笔记】EC-Final 2022 K. Magic

最近的题都只会抄题解😅 首先,操作顺序会影响答案,因此不能直接贪心。其次,因为是求贡献最大,所以可以考虑枚举最终哪些位置对答案产生了贡献,进而转化为全局贡献。 1.1 1.1 1.1 如果 [ l 1 , r 1 ) ⊆ [ …

zabbix学习1--zabbix6.x单机

文章目录 1. 环境2. MYSQL8.02.1 单节点2.2 配置主从 3. 依赖组件4. zabbix-server5. agent5.1 yum5.2 编译 附录my.cnfJDK默认端口号 1. 环境 进入官网查看所需部署环境配置以及应用版本要求https://www.zabbix.com/documentation/current/zh/manual/installation/requiremen…

MySQL 解决数据重复添加

1. sql语句: insert ignore into insert ignore into 表名 (xx1,xx2,xx3) VALUES (#{xx1},#{xx2},#{xx3}) 2. 复合索引

pytest(二)框架实现一些前后置(固件,夹具)的处理,常用三种

为什么需要这些功能? 比如:web自动化执行用例前是否需要打开浏览器?执行用例后需要关闭浏览器? 示例代码: import pytest class Testcase:#这是每条测试用例执行前的初始化函数def setup(self):print("\n我是每…

Android保存文件路径汇总

一、Android 中存储可以分为两大类:私有存储和共享存储 私有存储 (Private Storage) : 每个应用在内部存储种都拥有自己的私有目录 (/data/data/packageName),其它应用看不到,彼此也无法访问到该目录共享存储 (Shared Storage) : 除了私有存…

7-38 掉入陷阱的数字

输入样例: 5 输出样例: 1:16 2:22 3:13 4:13 ACcode: #include <bits/stdc.h>using namespace std;int main(){int n;cin >> n;vector<int> ans;int limit 1;ans.push_back(n);for(int i0; i<limit; i){//各位数字的和int sum 0;int num ans[i];w…

C++:组播代码实现

以下为C&#xff1a;组播代码实现 multicast_recver.cpp // its a demo to receive multicast udp data with the one of multi net interfaces.... #include <sys/types.h> #include <sys/socket.h> #include <arpa/inet.h> #include <stdio.h> #inc…

【TA】OP-TEE demo学习

前言&#xff1a;工作原因接触Apple软件需要搭建TA环境&#xff0c;涉及到OP-TEE&#xff0c;学习一下 OP-TEE&#xff08;Open Portable Trusted Execution Environment&#xff09;是一个开放源代码的可信执行环境&#xff08;TEE&#xff09;软件框架。它提供了安全的执行环…

echarts的折线图,在点击图例后,提示出现变化,不报错。tooltip的formatter怎么写

在点击图例的年后&#xff0c;提示框会相应的变化&#xff0c;多选和单选都会响应变化。tooptip的重度在formatter tooltip:{show:true,trigger:"axis",alwaysShowContent:true,triggerOn:"mousemove",textStyle:{color:"#fff"},backgroundColor…

平板用的触控笔什么牌子好?开学值得推荐的ipad手写笔

在开学季有什么电容笔值得购买&#xff1f;和以往的电容笔比起来&#xff0c;目前的电容笔多出了许多新的功能&#xff0c;比如具备了防误触功能&#xff0c;可以防止手指无意中碰到屏幕而导致书写失灵&#xff0c;以及可以随意调节笔画的粗细等等。苹果原装的Pencil如今卖得非…

微信小程序 按钮颜色

<button type"primary">主要按钮样式类型</button> <button type"default">默认按钮样式类型</button> <button type"warn">警告按钮样式类型</button> <view>按钮plain是否镂空</view> <bu…

Swagger(2):Springfox简介

使用Swagger时如果碰见版本更新或迭代时&#xff0c;只需要更改Swagger的描述文件即可。但是在频繁的更新项目版本时很多开发人员认为即使修改描述文件&#xff08;yml或json&#xff09;也是一定的工作负担&#xff0c;久而久之就直接修改代码&#xff0c;而不去修改描述文件了…

1.简单工厂模式

UML类图 代码 main.cpp #include <iostream> #include "OperationFactory.h" using namespace std;int main(void) {float num1;float num2;char operate;cin >> num1 >> num2 >> operate;Operation* oper OperationFactory::createOpera…

Mars3d插件参考开发教程并在相关页面引入

问题场景&#xff1a; 1.在使用Mars3d热力图功能时&#xff0c;提示mars3d.layer.HeatLayer is not a constructor 问题原因: 1.mars3d的热力图插件mars3d-heatmap没有安装引用。 解决方案&#xff1a; 1.参考开发教程&#xff0c;找到相关的插件库&#xff1a;Mars3D 三维…

Taro小程序隐私协议开发指南填坑

一. 配置文件app.config.js export default {...__usePrivacyCheck__: true,... }二. 开发者工具基础库修改 原因&#xff1a;从基础库 2.32.3 开始支持 修改路径&#xff1a;详情->本地设置->调试基础库 三. 用户隐私保护指引更新 修改路径&#xff1a;mp后台->设…