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

企业应加强自身网站建设舆情网站大全

企业应加强自身网站建设,舆情网站大全,wordpress分类添加html,工厂erp管理系统软件一、尽量用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.yayakq.cn/news/425223/

相关文章:

  • 西安做网站好的公司济宁网站建设电话
  • 黄岐建网站网易企业邮箱登录参数错误
  • 如何做自己网站的访问记录个人网站模板html
  • 招商网站怎么做抓取式网站建设
  • 17zwd一起做业网站邢台信息港房屋出租
  • 洛阳网站建设费用山东东方路桥建设总公司官方网站
  • 帮人做网站犯法什么类型的网站开发比较困难
  • php婚庆网站源码网站开发的心得与体会
  • 那里做网站好专业企业网站建设多少钱服务
  • 做h5动画的素材网站html5手机网站返回顶部
  • 辽宁网站建设多少钱建筑产业大数据综合服务平台
  • 彩妆网站模板一个服务器可以做多个网站吗
  • 重庆做网站及公众号公司建设银行车贷网站
  • 南通网站建设方案外包建设一个网站项目预算
  • 搞个网站需要多少钱电子商务网站的功能分析
  • 广州建设网站怎么做哈尔滨seo网络推广
  • 网站平台专业开发制作appwordpress 字段
  • 网站体验步骤设计说明是什么意思
  • dell公司网站建设的特点网络公司怎么挣钱的
  • 一个网站不兼容ie怎么做郑州做网站擎天
  • 成都 高端网站建设免费做微网站
  • 建一个设计网站要多少钱手机端下载
  • 广东门户网站建设太平洋电脑配置报价官网
  • 外贸网站的作用有哪些网站描述怎么设置
  • 网站建设 有必要吗小程序制作教程零基础入门
  • pc网站开发成app难度简述网站开发步骤
  • 莱州网站定制苏州妙笔网络科技有限公司
  • 网站菜单设置自适应的网站模板
  • 网站制作需要什么资料设计北京
  • 网站开发与维护专业前景现在做网络推广网站建设怎么样