• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

assert/H18-Feb-2015-2,7721,712

http/H18-Feb-2015-7238

mock/H18-Feb-2015-1,225706

require/H18-Feb-2015-1,090643

suite/H18-Feb-2015-425234

.gitignoreH A D18-Feb-2015263 2518

.travis.ymlH A D18-Feb-201581 128

README.mdH A D18-Feb-201510.6 KiB327226

doc.goH A D18-Feb-2015840 196

package_test.goH A D18-Feb-2015185 1310

README.md

1Testify - Thou Shalt Write Tests
2================================
3
4[![Build Status](https://travis-ci.org/stretchr/testify.svg)](https://travis-ci.org/stretchr/testify)
5
6Go code (golang) set of packages that provide many tools for testifying that your code will behave as you intend.
7
8Features 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
15Get 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 our [blog post article](http://blog.stretchr.com/2014/03/05/test-driven-development-specifically-in-golang/) or check out  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
28The `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
34See it in action:
35
36```go
37package yours
38
39import (
40  "testing"
41  "github.com/stretchr/testify/assert"
42)
43
44func 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
70if you assert many times, use the below:
71
72```go
73package yours
74
75import (
76  "testing"
77  "github.com/stretchr/testify/assert"
78)
79
80func 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` package
103-------------------------------------------------------------------------------------------
104
105The `require` package provides same global functions as the `assert` package, but instead of returning a boolean result they terminate current test.
106
107See [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
113The `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
115We 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
120The `mock` package provides a mechanism for easily writing mock objects that can be used in place of real objects when writing test code.
121
122An 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
125package yours
126
127import (
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.
138type 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.
149func (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.
162func 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
179For 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
181You 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
186The `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
188An example suite is shown below:
189
190```go
191// Basic imports
192import (
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
201type ExampleTestSuite struct {
202    suite.Suite
203    VariableThatShouldStartAtFive int
204}
205
206// Make sure that VariableThatShouldStartAtFive is set to five
207// before each test
208func (suite *ExampleTestSuite) SetupTest() {
209    suite.VariableThatShouldStartAtFive = 5
210}
211
212// All methods that begin with "Test" are run as tests within a
213// suite.
214func (suite *ExampleTestSuite) TestExample() {
215    assert.Equal(suite.T(), suite.VariableThatShouldStartAtFive, 5)
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
220func TestExampleTestSuite(t *testing.T) {
221    suite.Run(t, new(ExampleTestSuite))
222}
223```
224
225For 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
227For 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
233import (
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.
240type ExampleTestSuite struct {
241    suite.Suite
242    VariableThatShouldStartAtFive int
243}
244
245// Make sure that VariableThatShouldStartAtFive is set to five
246// before each test
247func (suite *ExampleTestSuite) SetupTest() {
248    suite.VariableThatShouldStartAtFive = 5
249}
250
251// All methods that begin with "Test" are run as tests within a
252// suite.
253func (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
259func TestExampleTestSuite(t *testing.T) {
260    suite.Run(t, new(ExampleTestSuite))
261}
262```
263
264------
265
266Installation
267============
268
269To install Testify, use `go get`:
270
271    go get github.com/stretchr/testify
272
273This will then make the following packages available to you:
274
275    github.com/stretchr/testify/assert
276    github.com/stretchr/testify/mock
277    github.com/stretchr/testify/http
278
279Import the `testify/assert` package into your code using this template:
280
281```go
282package yours
283
284import (
285  "testing"
286  "github.com/stretchr/testify/assert"
287)
288
289func TestSomething(t *testing.T) {
290
291  assert.True(t, true, "True is true!")
292
293}
294```
295
296------
297
298Staying up to date
299==================
300
301To update Testify, use `go get -u`:
302
303    go get -u github.com/stretchr/testify
304
305------
306
307Contributing
308============
309
310Please feel free to submit issues, fork the repository and send pull requests!
311
312When 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.
313
314------
315
316Licence
317=======
318Copyright (c) 2012 - 2013 Mat Ryer and Tyler Bunnell
319
320Please consider promoting this project if you find it useful.
321
322Permission 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:
323
324The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
325
326THE 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.
327