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

极构网站建设工作室汕头公司建站模板

极构网站建设工作室,汕头公司建站模板,网店营销技巧讨论,装修招投标网站建设快速开始 GoConvey是一个完全兼容官方Go Test的测试框架#xff0c;一般来说这种第三方库都比官方的功能要强大、更加易于使用、开发效率更高#xff0c;闲话少说#xff0c;先看一个example#xff1a; package utils import (. github.com/smartystreets/goconvey…快速开始 GoConvey是一个完全兼容官方Go Test的测试框架一般来说这种第三方库都比官方的功能要强大、更加易于使用、开发效率更高闲话少说先看一个example package utils import (. github.com/smartystreets/goconvey/conveytesting )func TestSpec(t *testing.T) {Convey(Given some integer with a starting value, t, func() {x : 1Convey(When the integer is incremented, func() {xConvey(The value should be greater by one, func() {So(x, ShouldEqual, 2)})})}) }看着复杂, 一层层的嵌套如果你使用IDE的话你可以点到源码里面看一下其方法注释其实已经说的非常清楚了这里摘取部分看一下 // Convey is the method intended for use when declaring the scopes of // a specification. Each scope has a description and a func() which may contain // other calls to Convey(), Reset() or Should-style assertions. Convey calls can // be nested as far as you see fit. // // IMPORTANT NOTE: The top-level Convey() within a Test method // must conform to the following signature: // // Convey(description string, t *testing.T, action func()) // // All other calls should look like this (no need to pass in *testing.T): // // Convey(description string, action func())这个用法相对简单了Convey定义了一个局部的作用域在这个作用域里面我们可以定义变量调用方法然后重复继续这个操作low-level的Convey会继承top-level的变量。 了解之后我们来扩展一下这个例子 func TestSpec(t *testing.T) {Convey(Given some integer with a starting value, t, func() {x : 1y : 10Convey(When the integer is incremented, func() {xConvey(The value should be greater by one, func() {So(x, ShouldEqual, 2)})})Convey(When x y, func() {if x y {x x ySo(x, ShouldBeGreaterThan, y)}})}) }非常简单当然这里我们并没有测试任何函数或方法下面咱们写一个函数真正测试一下假设有下面的方法 func Div(a, b int) (int, error) {if b 0 {return 0, errors.New(can not div zero)}return a / b, nil }使用GoConvey的话测试代码可以这么写 func TestDiv(t *testing.T) {const X 10Convey(Normal Result, t, func() {res, err : Div(X, 2)So(res, ShouldEqual, 5)So(err, ShouldBeNil)Convey(Extend Scope, func() {res, err : Div(res, 2)So(res, ShouldEqual, 2)So(err, ShouldBeNil)})})Convey(Error Result, t, func() {res, err : Div(X, 0)So(res, ShouldEqual, 0)So(err, ShouldNotBeNil)}) }有人可能会觉得这和官方的没多大区别相当于多加了一个注释可以对每一个测试用例标识但是不仅仅如此这个库还提供了大量增强的Assertions可以非常方便的对字符串、slice、map结果进行断言测试具体的话可以查看一下文档或者点进去看看源码注释这些源码注释基本上已经写的非常清楚了。 Web UI 此外框架还提供了一个Web端的UI界面可以非常方便的查看测试覆盖和运行情况还可以自动运行测试执行goconvey命令就可以启动服务快试一试吧虽然说像Goland这样的IDE也提供了GUI工具查看测试覆盖率但是这个更加方便 另外这个框架还提供了自定义Assertions的功能使用起来也很方便有一个通用的模板 func shoulddo-something(actual interface{}, expected ...interface{}) string {if some-important-condition-is-met(actual, expected) {return // empty string means the assertion passed}return some descriptive message detailing why the assertion failed... }举个例子这里定义一个试试 func shouldNotGreatThan100(actual interface{}, expected ...interface{}) string {if actual.(int) 100 {return too big than 100} else {return } }定义通用的逻辑 有时候测试会需要做一些准备工作而且是重复的比如说一些初始化操作这时候就可以定义一个函数完成这件事不必每次测试重复做官方文档里面举了一个数据库测试的例子每次测试前开启事务测试结束后回滚事务这里贴一下官方的example大家看一下很容易理解 package main import (database/sqltesting_ github.com/lib/pq. github.com/smartystreets/goconvey/convey ) func WithTransaction(db *sql.DB, f func(tx *sql.Tx)) func() {return func() {tx, err : db.Begin()So(err, ShouldBeNil)Reset(func() {/* Verify that the transaction is alive by executing a command */_, err : tx.Exec(SELECT 1)So(err, ShouldBeNil)tx.Rollback()})/* Here we invoke the actual test-closure and provide the transaction */f(tx)} } func TestUsers(t *testing.T) {db, err : sql.Open(postgres, postgres://localhost?sslmodedisable)if err ! nil {panic(err)}Convey(Given a user in the database, t, WithTransaction(db, func(tx *sql.Tx) {_, err : tx.Exec(INSERT INTO Users (id, name) VALUES (1, Test User))So(err, ShouldBeNil)Convey(Attempting to retrieve the user should return the user, func() {var name stringdata : tx.QueryRow(SELECT name FROM Users WHERE id 1)err data.Scan(name)So(err, ShouldBeNil)So(name, ShouldEqual, Test User)})})) } /* Required table to run the test: CREATE TABLE public.Users ( id INTEGER NOT NULL UNIQUE, name CHARACTER VARYING( 2044 ) NOT NULL ); */
http://www.sadfv.cn/news/218712/

相关文章:

  • 东莞设计网站建设方案安徽网页设计培训
  • 现在建网站多少钱网站评论 设计
  • 网站排名优化培训课程网站建设保密
  • 淘宝网站建设的目标静安微信手机网站制作
  • 江苏初中课程基地建设网站东莞网站建设乐云seo在线制作
  • 西宁网站建设价格济南中建设计院有限公司网站
  • 郑州市城乡建设局网站wix和wordpress区别
  • asp开发网站手机网站怎样做解析
  • 北京建设网站专家wordpress加载太慢
  • 网站设计的要求wordpress主页
  • 自己做网络棋牌网站流程网站 建设 原则
  • 有没有专业做汽车坐垫网站it外包公司 能去吗
  • 中企动力高端网站建设wordpress 内容分发
  • 网站建设公司是怎么找客户保险平台官网
  • 东台市住房和建设局网站搜索关键词的工具
  • 宁波专业品牌网站制作外包如何将域名和网站绑定
  • 山东金融行业网站开发怎么免费网站
  • 手机网站建设 豆丁wp网站搬家教程
  • 收到短信说备案被退回但工信部网站上正常啊做淘宝相关网站
  • 富阳建立网站的绿色门业宽屏网站模板 破解
  • 站长统计 站长统计海南门户网站开发公司
  • 吴忠市建设网站赛罕区城乡建设局网站
  • 漯河网站建设-千弘网络手机多少钱
  • 免费网站制作下载东莞网站建设模具
  • 使用编辑字母做免费网站潍坊企化网站建设
  • 网站美工的重要性国外注册的域名国内做的网站
  • php做网站界面代码感叹号分销系统
  • 郑州网站优化推广上海比较有名的设计公司
  • 网站开发文档模板下载网站排名易下拉稳定
  • 培训网站建设方案模板wordpress手机怎么分享链接地址