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. C++ 代码静态分析工具cppcheck【转】

    转自:http://blog.csdn.net/chen19870707/article/details/42393217 权声明:本文为博主原创文章,未经博主允许不得转载.   目录(?)[-] c ...

  2. C#中流写入类StreamWriter的介绍

    C#中流写入类StreamWriter的介绍 (转) 应用FileStream类需要许多额外的数据类型转换工作,十分影响效率.使用StreamWriter类将提供更简单,更方便的操作方式.   Str ...

  3. Android中沉浸式状态栏的应用

    在Android5.0版本后,谷歌公司为Android系统加入了很多新特性,刷新了Android用户的体验度.而其中的一个新特性就是沉浸式状态栏.那么问题来了,很多非移动端的小伙伴就要问了,什么是沉浸 ...

  4. linux sed 替换(整行替换,部分替换)、删除delete、新增add、选取

    sed命令行格式为:         sed [-nefri] ‘command’ 输入文本 常用选项:        -n∶使用安静(silent)模式.在一般 sed 的用法中,所有来自 STDI ...

  5. (4)ASP.NET HttpRequest 类

    HttpRequest 类的主要作用是读取客户端在 Web 请求期间发送的 HTTP 值. https://msdn.microsoft.com/zh-cn/library/system.web.ht ...

  6. ubuntu和raspberry下调试python_spi备忘

    Ubuntu12.04 自安装python3.3中头文件Python.h路径:usr/local/python3.3/include/python3.3m Ubuntu12.04 自带的Python2 ...

  7. 把Execl表格中的数据获取出来保存到数据库中

    比如我们遇到一些需要把execl表格中的数据保存到数据库中,一条一条保存效率底下而且容易出错,数据量少还好,一旦遇到数据量大的时候就会累死个人啊,下面我们就来把execl表格中数据保存到对应的数据库中 ...

  8. Ubuntu 16.04安装字体管理工具

    注意:这个字体管理工具只是简化了字体的安装和卸载,并没有快速下载字体去自动安装,所有的字体都是需要自行下载,因为字体本身是有版权的. 安装: sudo apt-get install font-man ...

  9. PHP平均整数红包算法

    <?php function RandomMoney( $money,$num ){ $arr = array(); $total_money = 0; $this_money = $money ...

  10. win10 nginx + django +flup 配置

    1 安装 Nginx 官网下载,直接点exe启动即可 2 安装django pip install django 注意 新建立项目时 python *****/django-admin.py star ...