当前位置: 首页 > news >正文

做网站利用自己电脑磁力神器

做网站利用自己电脑,磁力神器,施甸网站建设,wordpress配置ip访问不了一、尽量用Convey将所有测试用例的Convey汇总 用Convey嵌套的方法,将所有测试用例的Convey用一个大的Convey包裹起来,每个测试函数下只有一个大的Convey。比如下面的示例代码: import ("testing". "github.com/smartystreet…

一、尽量用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.hrbkazy.com/news/34529.html

相关文章:

  • 网站图片链接是怎么做的南京百度seo代理
  • 网站开发的发展百度网站安全检测
  • 在哪查看网站被收录的情况营销课程
  • 外加工平台班级优化大师学生版
  • 网站推广该怎么做免费关键词优化工具
  • 广东网页空间网站平台最新seo课程
  • 滁州市南谯区规划建设局网站合肥seo网络营销推广
  • 上海二手房网站网站推广互联网推广
  • 马鞍山网站建设咨询电专门做推广的软文
  • 政府类网站建设永久免费的网站服务器有哪些软件
  • 西安网站建设哪家专业工业设计公司
  • 后端开发和前端开发的区别智能网站推广优化
  • 所谓做网站就这么几步深圳关键词自动排名
  • 手工做女宝宝衣服的网站青岛疫情最新情况
  • 乌鲁木齐做网站百度官网推广平台电话
  • 红色大气网络公司企业网站源码_适合广告设计百度数据开放平台
  • 山东网络建站推广每日新闻快报
  • 培训网站建设方案书旺道seo推广有用吗
  • 电子商务网站设计的原则沈阳seo技术
  • 网站建设公司营销推广链接搜索
  • 网站建设优化方案汤阴县seo快速排名有哪家好
  • 网站制作公司怎样帮客户做优化普通话手抄报文字内容
  • 网站建设课程总结郑州竞价托管代运营
  • 网站案例比较多的公司今日热点新闻一览
  • php做网站的好处sem是什么显微镜
  • 医院网站制作百度自媒体注册入口
  • 旅游网站怎么用dw做yande搜索引擎官网入口
  • 网站当前位置 样式指数函数公式
  • 瑞安商业网站建设网站备案查询工信部官网
  • 网站如何做线上和线下推广企业营销策划书如何编写