Testify - Thou Shalt Write Tests
================================
[![Build Status](https://travis-ci.org/stretchr/testify.svg)](https://travis-ci.org/stretchr/testify) [![Go Report Card](https://goreportcard.com/badge/github.com/stretchr/testify)](https://goreportcard.com/report/github.com/stretchr/testify) [![GoDoc](https://godoc.org/github.com/stretchr/testify?status.svg)](https://godoc.org/github.com/stretchr/testify)
Go code (golang) set of packages that provide many tools for testifying that your code will behave as you intend.
Features include:
* [Easy assertions](#assert-package)
* [Mocking](#mock-package)
* [HTTP response trapping](#http-package)
* [Testing suite interfaces and functions](#suite-package)
Get started:
* Install testify with [one line of code](#installation), or [update it with another](#staying-up-to-date)
* For an introduction to writing test code in Go, see http://golang.org/doc/code.html#Testing
* Check out the API Documentation http://godoc.org/github.com/stretchr/testify
* To make your testing life easier, check out our other project, [gorc](http://github.com/stretchr/gorc)
* A little about [Test-Driven Development (TDD)](http://en.wikipedia.org/wiki/Test-driven_development)
[`assert`](http://godoc.org/github.com/stretchr/testify/assert "API documentation") 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:
```go
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:
```go
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`](http://godoc.org/github.com/stretchr/testify/require "API documentation") 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](http://golang.org/pkg/testing/#T.FailNow) for details.
[`http`](http://godoc.org/github.com/stretchr/testify/http "API documentation") package
---------------------------------------------------------------------------------------
The `http` package contains test objects useful for testing code that relies on the `net/http` package. Check out the [(deprecated) API documentation for the `http` package](http://godoc.org/github.com/stretchr/testify/http).
We recommend you use [httptest](http://golang.org/pkg/net/http/httptest) instead.
[`mock`](http://godoc.org/github.com/stretchr/testify/mock "API documentation") 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:
```go
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)
}
```
For more information on how to write mock code, check out the [API documentation for the `mock` package](http://godoc.org/github.com/stretchr/testify/mock).
You can use the [mockery tool](http://github.com/vektra/mockery) to autogenerate the mock code against an interface as well, making using mocks much quicker.
[`suite`](http://godoc.org/github.com/stretchr/testify/suite "API documentation") 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:
```go
// 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](https://github.com/stretchr/testify/blob/master/suite/suite_test.go)
For more information on writing suites, check out the [API documen
没有合适的资源?快使用搜索试试~ 我知道了~
Go-利用Redis和Golang实现分布式令牌桶算法
共181个文件
go:141个
md:7个
txt:6个
需积分: 50 12 下载量 201 浏览量
2019-08-14
03:39:09
上传
评论 1
收藏 620KB ZIP 举报
温馨提示
利用Redis和Golang实现分布式令牌桶算法,令牌桶算法对速率限制和网络拥塞控制非常有用
资源推荐
资源详情
资源评论
收起资源包目录
Go-利用Redis和Golang实现分布式令牌桶算法 (181个子文件)
redis.conf 132B
.gitignore 263B
.gitignore 252B
.gitignore 252B
.gitignore 18B
commands_test.go 89KB
type_specific_codegen.go 71KB
format_test.go 56KB
commands.go 50KB
dump_test.go 33KB
mock_test.go 31KB
assertions_test.go 30KB
assertions.go 29KB
difflib.go 22KB
difflib.go 22KB
mock.go 22KB
cluster.go 19KB
command.go 18KB
cluster_test.go 17KB
forward_assertions_test.go 17KB
require.go 14KB
assertion_forward.go 14KB
dump.go 13KB
dump.go 13KB
require_forward.go 13KB
config.go 12KB
config.go 12KB
format.go 11KB
format.go 11KB
common.go 10KB
common.go 10KB
pubsub_test.go 9KB
ring.go 9KB
spew_test.go 9KB
difflib_test.go 9KB
forward_requirements_test.go 8KB
pubsub.go 8KB
redis.go 8KB
doc.go 8KB
doc.go 8KB
main_test.go 8KB
example_test.go 8KB
common_test.go 8KB
parser.go 7KB
sentinel.go 7KB
requirements_test.go 7KB
bucket.go 7KB
bucket_test.go 7KB
redis_test.go 7KB
suite_test.go 7KB
main.go 6KB
reader.go 6KB
pool.go 6KB
spew.go 6KB
spew.go 6KB
bypass.go 6KB
bypass.go 6KB
example_test.go 5KB
race_test.go 5KB
map.go 5KB
pool_test.go 5KB
ring_test.go 5KB
options.go 4KB
accessors.go 4KB
bench_test.go 4KB
redis.go 4KB
dumpcgo_test.go 4KB
result.go 4KB
universal.go 4KB
http_assertions.go 4KB
http_assertions_test.go 4KB
pool_test.go 3KB
storage_test.go 3KB
tx_test.go 3KB
internalunsafe_test.go 3KB
dumpcgo.go 3KB
iterator_test.go 3KB
hashtag.go 3KB
suite.go 3KB
doc.go 3KB
internal_test.go 3KB
conversions.go 2KB
tx.go 2KB
consistenthash_test.go 2KB
redis_test.go 2KB
scan.go 2KB
doc.go 2KB
write_buffer.go 2KB
memory.go 2KB
mutations.go 2KB
reader_test.go 2KB
sentinel_test.go 2KB
pool_sticky.go 2KB
consistenthash.go 2KB
bypasssafe.go 2KB
bypasssafe.go 2KB
pipeline_test.go 2KB
options_test.go 2KB
doc.go 2KB
pipeline.go 2KB
共 181 条
- 1
- 2
资源评论
weixin_39841882
- 粉丝: 443
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功