OSDN Git Service

new repo
[bytom/vapor.git] / vendor / github.com / stretchr / testify / README.md
1 Testify - Thou Shalt Write Tests
2 ================================
3
4 [![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)
5
6 Go code (golang) set of packages that provide many tools for testifying that your code will behave as you intend.
7
8 Features include:
9
10   * [Easy assertions](#assert-package)
11   * [Mocking](#mock-package)
12   * [HTTP response trapping](#http-package)
13   * [Testing suite interfaces and functions](#suite-package)
14
15 Get started:
16
17   * Install testify with [one line of code](#installation), or [update it with another](#staying-up-to-date)
18   * For an introduction to writing test code in Go, see http://golang.org/doc/code.html#Testing
19   * Check out the API Documentation http://godoc.org/github.com/stretchr/testify
20   * To make your testing life easier, check out our other project, [gorc](http://github.com/stretchr/gorc)
21   * A little about [Test-Driven Development (TDD)](http://en.wikipedia.org/wiki/Test-driven_development)
22
23
24
25 [`assert`](http://godoc.org/github.com/stretchr/testify/assert "API documentation") package
26 -------------------------------------------------------------------------------------------
27
28 The `assert` package provides some helpful methods that allow you to write better test code in Go.
29
30   * Prints friendly, easy to read failure descriptions
31   * Allows for very readable code
32   * Optionally annotate each assertion with a message
33
34 See it in action:
35
36 ```go
37 package yours
38
39 import (
40   "testing"
41   "github.com/stretchr/testify/assert"
42 )
43
44 func TestSomething(t *testing.T) {
45
46   // assert equality
47   assert.Equal(t, 123, 123, "they should be equal")
48
49   // assert inequality
50   assert.NotEqual(t, 123, 456, "they should not be equal")
51
52   // assert for nil (good for errors)
53   assert.Nil(t, object)
54
55   // assert for not nil (good when you expect something)
56   if assert.NotNil(t, object) {
57
58     // now we know that object isn't nil, we are safe to make
59     // further assertions without causing any errors
60     assert.Equal(t, "Something", object.Value)
61
62   }
63
64 }
65 ```
66
67   * 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.
68   * 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.
69
70 if you assert many times, use the below:
71
72 ```go
73 package yours
74
75 import (
76   "testing"
77   "github.com/stretchr/testify/assert"
78 )
79
80 func TestSomething(t *testing.T) {
81   assert := assert.New(t)
82
83   // assert equality
84   assert.Equal(123, 123, "they should be equal")
85
86   // assert inequality
87   assert.NotEqual(123, 456, "they should not be equal")
88
89   // assert for nil (good for errors)
90   assert.Nil(object)
91
92   // assert for not nil (good when you expect something)
93   if assert.NotNil(object) {
94
95     // now we know that object isn't nil, we are safe to make
96     // further assertions without causing any errors
97     assert.Equal("Something", object.Value)
98   }
99 }
100 ```
101
102 [`require`](http://godoc.org/github.com/stretchr/testify/require "API documentation") package
103 ---------------------------------------------------------------------------------------------
104
105 The `require` package provides same global functions as the `assert` package, but instead of returning a boolean result they terminate current test.
106
107 See [t.FailNow](http://golang.org/pkg/testing/#T.FailNow) for details.
108
109
110 [`http`](http://godoc.org/github.com/stretchr/testify/http "API documentation") package
111 ---------------------------------------------------------------------------------------
112
113 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).
114
115 We recommend you use [httptest](http://golang.org/pkg/net/http/httptest) instead.
116
117 [`mock`](http://godoc.org/github.com/stretchr/testify/mock "API documentation") package
118 ----------------------------------------------------------------------------------------
119
120 The `mock` package provides a mechanism for easily writing mock objects that can be used in place of real objects when writing test code.
121
122 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:
123
124 ```go
125 package yours
126
127 import (
128   "testing"
129   "github.com/stretchr/testify/mock"
130 )
131
132 /*
133   Test objects
134 */
135
136 // MyMockedObject is a mocked object that implements an interface
137 // that describes an object that the code I am testing relies on.
138 type MyMockedObject struct{
139   mock.Mock
140 }
141
142 // DoSomething is a method on MyMockedObject that implements some interface
143 // and just records the activity, and returns what the Mock object tells it to.
144 //
145 // In the real object, this method would do something useful, but since this
146 // is a mocked object - we're just going to stub it out.
147 //
148 // NOTE: This method is not being tested here, code that uses this object is.
149 func (m *MyMockedObject) DoSomething(number int) (bool, error) {
150
151   args := m.Called(number)
152   return args.Bool(0), args.Error(1)
153
154 }
155
156 /*
157   Actual test functions
158 */
159
160 // TestSomething is an example of how to use our test object to
161 // make assertions about some target code we are testing.
162 func TestSomething(t *testing.T) {
163
164   // create an instance of our test object
165   testObj := new(MyMockedObject)
166
167   // setup expectations
168   testObj.On("DoSomething", 123).Return(true, nil)
169
170   // call the code we are testing
171   targetFuncThatDoesSomethingWithObj(testObj)
172
173   // assert that the expectations were met
174   testObj.AssertExpectations(t)
175
176 }
177 ```
178
179 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).
180
181 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.
182
183 [`suite`](http://godoc.org/github.com/stretchr/testify/suite "API documentation") package
184 -----------------------------------------------------------------------------------------
185
186 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.
187
188 An example suite is shown below:
189
190 ```go
191 // Basic imports
192 import (
193     "testing"
194     "github.com/stretchr/testify/assert"
195     "github.com/stretchr/testify/suite"
196 )
197
198 // Define the suite, and absorb the built-in basic suite
199 // functionality from testify - including a T() method which
200 // returns the current testing context
201 type ExampleTestSuite struct {
202     suite.Suite
203     VariableThatShouldStartAtFive int
204 }
205
206 // Make sure that VariableThatShouldStartAtFive is set to five
207 // before each test
208 func (suite *ExampleTestSuite) SetupTest() {
209     suite.VariableThatShouldStartAtFive = 5
210 }
211
212 // All methods that begin with "Test" are run as tests within a
213 // suite.
214 func (suite *ExampleTestSuite) TestExample() {
215     assert.Equal(suite.T(), 5, suite.VariableThatShouldStartAtFive)
216 }
217
218 // In order for 'go test' to run this suite, we need to create
219 // a normal test function and pass our suite to suite.Run
220 func TestExampleTestSuite(t *testing.T) {
221     suite.Run(t, new(ExampleTestSuite))
222 }
223 ```
224
225 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)
226
227 For more information on writing suites, check out the [API documentation for the `suite` package](http://godoc.org/github.com/stretchr/testify/suite).
228
229 `Suite` object has assertion methods:
230
231 ```go
232 // Basic imports
233 import (
234     "testing"
235     "github.com/stretchr/testify/suite"
236 )
237
238 // Define the suite, and absorb the built-in basic suite
239 // functionality from testify - including assertion methods.
240 type ExampleTestSuite struct {
241     suite.Suite
242     VariableThatShouldStartAtFive int
243 }
244
245 // Make sure that VariableThatShouldStartAtFive is set to five
246 // before each test
247 func (suite *ExampleTestSuite) SetupTest() {
248     suite.VariableThatShouldStartAtFive = 5
249 }
250
251 // All methods that begin with "Test" are run as tests within a
252 // suite.
253 func (suite *ExampleTestSuite) TestExample() {
254     suite.Equal(suite.VariableThatShouldStartAtFive, 5)
255 }
256
257 // In order for 'go test' to run this suite, we need to create
258 // a normal test function and pass our suite to suite.Run
259 func TestExampleTestSuite(t *testing.T) {
260     suite.Run(t, new(ExampleTestSuite))
261 }
262 ```
263
264 ------
265
266 Installation
267 ============
268
269 To install Testify, use `go get`:
270
271     * Latest version: go get github.com/stretchr/testify
272     * Specific version: go get gopkg.in/stretchr/testify.v1
273
274 This will then make the following packages available to you:
275
276     github.com/stretchr/testify/assert
277     github.com/stretchr/testify/mock
278     github.com/stretchr/testify/http
279
280 Import the `testify/assert` package into your code using this template:
281
282 ```go
283 package yours
284
285 import (
286   "testing"
287   "github.com/stretchr/testify/assert"
288 )
289
290 func TestSomething(t *testing.T) {
291
292   assert.True(t, true, "True is true!")
293
294 }
295 ```
296
297 ------
298
299 Staying up to date
300 ==================
301
302 To update Testify to the latest version, use `go get -u github.com/stretchr/testify`.
303
304 ------
305
306 Version History
307 ===============
308
309    * 1.0 - New package versioning strategy adopted.
310
311 ------
312
313 Contributing
314 ============
315
316 Please feel free to submit issues, fork the repository and send pull requests!
317
318 When submitting an issue, we ask that you please include a complete test function that demonstrates the issue.  Extra credit for those using Testify to write the test code that demonstrates it.
319
320 ------
321
322 Licence
323 =======
324 Copyright (c) 2012 - 2013 Mat Ryer and Tyler Bunnell
325
326 Please consider promoting this project if you find it useful.
327
328 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
329
330 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
331
332 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.