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. 過充保護警告訊息 over charging protection,Battery over voltage protection, warning message

    Definition: over charging protection.battery over voltage protection, 是一種 battery 保護機制, 避免 battery 充 ...

  2. hdu 2078(DFS)

    Matrix Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 3845   Accepted: 1993 Descriptio ...

  3. CSS-点赞爱心小动画

    爱心图片: HTML: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> & ...

  4. 字蛛(font-spider)-单独压缩字体(解决页面少有的特殊字体的字体包引用)

    特别想独立的把这个问题写成一篇内容,分享给大家. 反正我是这个字体压缩使用的受益者,不是打广告. 很久以前,设计师总是爱用一些奇奇怪怪的字体放在页面上,而作为前端我们很容易的就能直接使用TA们用到的字 ...

  5. Software Engineering | Strategy pattern

    聚合关系.

  6. POJ 1860 Currency Exchange 最短路+负环

    原题链接:http://poj.org/problem?id=1860 Currency Exchange Time Limit: 1000MS   Memory Limit: 30000K Tota ...

  7. javascript 函数初探 (五)--- 几种类型的函数

    即时函数: 目前我们已经讨论了匿名函数在回调时的应用.接下来,我们来看看匿名函数的另一种应用实例 --- javascript即时函数: 比如: ( function(){ alert('her'); ...

  8. hdu5379||2015多校联合第7场1011 树形统计

    pid=5379">http://acm.hdu.edu.cn/showproblem.php? pid=5379 Problem Description Little sun is ...

  9. Office HPDeskjetD2468 打印机电源灯闪烁不停,打印机不工作怎么办

    怎么处理HP DeskjetD2468 打印机电源灯闪烁不停,打印机不工作? 最佳答案 一般电源灯闪烁时因为你的打印喷头上面的盖子没有盖好,你看看.....盖好之后关机再开 谢谢!

  10. iOS_7_scrollView大图缩放

    终于效果图: BeyondViewController.h // // BeyondViewController.h // 7_scrollView大图展示 // // Created by beyo ...