https://github.com/stretchr/testify

Testify - Thou Shalt Write Tests

  

Go code (golang) set of packages that provide many tools for testifying that your code will behave as you intend.

Features include:

Get started:

assert package

The assert package provides some helpful methods that allow you to write better test code in Go.

  • Prints friendly, easy to read failure descriptions
  • Allows for very readable code
  • Optionally annotate each assertion with a message

See it in action:

package yours

import (
"testing"
"github.com/stretchr/testify/assert"
) func TestSomething(t *testing.T) { // assert equality
assert.Equal(t, 123, 123, "they should be equal") // assert inequality
assert.NotEqual(t, 123, 456, "they should not be equal") // assert for nil (good for errors)
assert.Nil(t, object) // assert for not nil (good when you expect something)
if assert.NotNil(t, object) { // now we know that object isn't nil, we are safe to make
// further assertions without causing any errors
assert.Equal(t, "Something", object.Value) } }
  • Every assert func takes the testing.T object as the first argument. This is how it writes the errors out through the normal go test capabilities.
  • Every assert func returns a bool indicating whether the assertion was successful or not, this is useful for if you want to go on making further assertions under certain conditions.

if you assert many times, use the below:

package yours

import (
"testing"
"github.com/stretchr/testify/assert"
) func TestSomething(t *testing.T) {
assert := assert.New(t) // assert equality
assert.Equal(123, 123, "they should be equal") // assert inequality
assert.NotEqual(123, 456, "they should not be equal") // assert for nil (good for errors)
assert.Nil(object) // assert for not nil (good when you expect something)
if assert.NotNil(object) { // now we know that object isn't nil, we are safe to make
// further assertions without causing any errors
assert.Equal("Something", object.Value)
}
}

require package

The require package provides same global functions as the assert package, but instead of returning a boolean result they terminate current test.

See t.FailNow for details.

mock package

The mock package provides a mechanism for easily writing mock objects that can be used in place of real objects when writing test code.

An example test function that tests a piece of code that relies on an external object testObj, can setup expectations (testify) and assert that they indeed happened:

package yours

import (
"testing"
"github.com/stretchr/testify/mock"
) /*
Test objects
*/ // MyMockedObject is a mocked object that implements an interface
// that describes an object that the code I am testing relies on.
type MyMockedObject struct{
mock.Mock
} // DoSomething is a method on MyMockedObject that implements some interface
// and just records the activity, and returns what the Mock object tells it to.
//
// In the real object, this method would do something useful, but since this
// is a mocked object - we're just going to stub it out.
//
// NOTE: This method is not being tested here, code that uses this object is.
func (m *MyMockedObject) DoSomething(number int) (bool, error) { args := m.Called(number)
return args.Bool(0), args.Error(1) } /*
Actual test functions
*/ // TestSomething is an example of how to use our test object to
// make assertions about some target code we are testing.
func TestSomething(t *testing.T) { // create an instance of our test object
testObj := new(MyMockedObject) // setup expectations
testObj.On("DoSomething", 123).Return(true, nil) // call the code we are testing
targetFuncThatDoesSomethingWithObj(testObj) // assert that the expectations were met
testObj.AssertExpectations(t) } // TestSomethingElse is a second example of how to use our test object to
// make assertions about some target code we are testing.
// This time using a placeholder. Placeholders might be used when the
// data being passed in is normally dynamically generated and cannot be
// predicted beforehand (eg. containing hashes that are time sensitive)
func TestSomethingElse(t *testing.T) { // create an instance of our test object
testObj := new(MyMockedObject) // setup expectations with a placeholder in the argument list
testObj.On("DoSomething", mock.Anything).Return(true, nil) // call the code we are testing
targetFuncThatDoesSomethingWithObj(testObj) // assert that the expectations were met
testObj.AssertExpectations(t) }

For more information on how to write mock code, check out the API documentation for the mock package.

You can use the mockery tool to autogenerate the mock code against an interface as well, making using mocks much quicker.

suite package

The suite package provides functionality that you might be used to from more common object oriented languages. With it, you can build a testing suite as a struct, build setup/teardown methods and testing methods on your struct, and run them with 'go test' as per normal.

An example suite is shown below:

// Basic imports
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
) // Define the suite, and absorb the built-in basic suite
// functionality from testify - including a T() method which
// returns the current testing context
type ExampleTestSuite struct {
suite.Suite
VariableThatShouldStartAtFive int
} // Make sure that VariableThatShouldStartAtFive is set to five
// before each test
func (suite *ExampleTestSuite) SetupTest() {
suite.VariableThatShouldStartAtFive = 5
} // All methods that begin with "Test" are run as tests within a
// suite.
func (suite *ExampleTestSuite) TestExample() {
assert.Equal(suite.T(), 5, suite.VariableThatShouldStartAtFive)
} // In order for 'go test' to run this suite, we need to create
// a normal test function and pass our suite to suite.Run
func TestExampleTestSuite(t *testing.T) {
suite.Run(t, new(ExampleTestSuite))
}

For a more complete example, using all of the functionality provided by the suite package, look at our example testing suite

For more information on writing suites, check out the API documentation for the suite package.

Suite object has assertion methods:

// Basic imports
import (
"testing"
"github.com/stretchr/testify/suite"
) // Define the suite, and absorb the built-in basic suite
// functionality from testify - including assertion methods.
type ExampleTestSuite struct {
suite.Suite
VariableThatShouldStartAtFive int
} // Make sure that VariableThatShouldStartAtFive is set to five
// before each test
func (suite *ExampleTestSuite) SetupTest() {
suite.VariableThatShouldStartAtFive = 5
} // All methods that begin with "Test" are run as tests within a
// suite.
func (suite *ExampleTestSuite) TestExample() {
suite.Equal(suite.VariableThatShouldStartAtFive, 5)
} // In order for 'go test' to run this suite, we need to create
// a normal test function and pass our suite to suite.Run
func TestExampleTestSuite(t *testing.T) {
suite.Run(t, new(ExampleTestSuite))
}

r testifying that your code will behave as you intend.的更多相关文章

  1. A real example of vioplot in R (sample data and code attached)

    Basic information Package name: vioplot Package homepage: https://cran.r-project.org/web/packages/vi ...

  2. go语言项目汇总

    Horst Rutter edited this page 7 days ago · 529 revisions Indexes and search engines These sites prov ...

  3. Golang优秀开源项目汇总, 10大流行Go语言开源项目, golang 开源项目全集(golang/go/wiki/Projects), GitHub上优秀的Go开源项目

    Golang优秀开源项目汇总(持续更新...)我把这个汇总放在github上了, 后面更新也会在github上更新. https://github.com/hackstoic/golang-open- ...

  4. GO语言的开源库

    Indexes and search engines These sites provide indexes and search engines for Go packages: godoc.org ...

  5. Go语言(golang)开源项目大全

    转http://www.open-open.com/lib/view/open1396063913278.html内容目录Astronomy构建工具缓存云计算命令行选项解析器命令行工具压缩配置文件解析 ...

  6. [转]Go语言(golang)开源项目大全

    内容目录 Astronomy 构建工具 缓存 云计算 命令行选项解析器 命令行工具 压缩 配置文件解析器 控制台用户界面 加密 数据处理 数据结构 数据库和存储 开发工具 分布式/网格计算 文档 编辑 ...

  7. VS code 配置为 Python R LaTeX IDE

    VS code配置为Python R LaTeX IDE VS code的中文断行.编辑功能强大,配置简单. VSC的扩展在应用商店搜索安装,快捷键ctrl+shift+x调出应用商店. 安装扩展后, ...

  8. THE R QGRAPH PACKAGE: USING R TO VISUALIZE COMPLEX RELATIONSHIPS AMONG VARIABLES IN A LARGE DATASET, PART ONE

    The R qgraph Package: Using R to Visualize Complex Relationships Among Variables in a Large Dataset, ...

  9. js获取微信code

    function callback(result) { alert('cucess'); alert(result); //输出openid } function getQueryString(nam ...

随机推荐

  1. Device Tree Usage 【转】

    转自:http://blog.chinaunix.net/uid-20522771-id-3457184.html 原文链接:http://devicetree.org/Device_Tree_Usa ...

  2. UML学习倒腾记

    先看到http://www.jianshu.com/p/1256e2643923这篇博客,号称21分钟入门uml,也许是我太笨了吧,一下午也没有完全搞定: 使用过atom编辑器,没有完全运行出来结果. ...

  3. C、C++变量auto,static,register,extern类型

    auto: 推导类型变量:编译器选项指示编译器如何使用 auto 关键字来声明变量. 如果指定默认选项 /Zc:auto,编译器从其初始化表达式中推导声明的变量的类型. 如果指定 /Zc:auto-, ...

  4. Oracle单个datafile大小的限制

    http://blog.itpub.net/30776559/viewspace-2146790/

  5. 1004 Counting Leaves

    A family hierarchy is usually presented by a pedigree tree. Your job is to count those family member ...

  6. java正则表达式的知识

    /** 用途:正则表达式 * 创建人:向家康 * 创建日期:2019年4月21日 下午9:59:08 */ //有了登录界面当然少不了正则表达式啦,这是做项目必备的知识点 //通过本博客的代码,想必即 ...

  7. spring aop提供了两种实现方式jdk和cglib

    Spring AOP使用了两种代理机制:一种是基于JDK的动态代理:另一种是基于CGLib的动态代理.之所以需要两种代理机制,很大程度上是因为JDK本身只提供接口的代理,而不支持类的代理. Sprin ...

  8. Android 中的Canvas画图

    Android中有一个Canvas类,Canvas类就是表示一块画布,你可以在上面画你想画的东西.当然,你还可以设置画布的属性,如画布的颜色/尺寸等.Canvas提供了如下一些方法: Canvas() ...

  9. linux内核I2C子系统学习(三)

    写设备驱动: 四部曲: 构建i2c_driver 注册i2c_driver 构建i2c_client ( 第一种方法:注册字符设备驱动.第二种方法:通过板文件的i2c_board_info填充,然后注 ...

  10. 【转】css浮动元素的知识

    原文: http://www.cnblogs.com/xuyao100/p/8940958.html ------------------------------------------------- ...