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

..03-May-2022-

_codegen/H09-Jun-2018-

assert/H09-Jun-2018-

http/H09-Jun-2018-

mock/H09-Jun-2018-

require/H09-Jun-2018-

suite/H09-Jun-2018-

vendor/github.com/H09-Jun-2018-

.gitignoreH A D09-Jun-2018263

.travis.gofmt.shH A D09-Jun-2018105

.travis.gogenerate.shH A D09-Jun-2018239

.travis.govet.shH A D09-Jun-2018138

.travis.ymlH A D09-Jun-2018208

Gopkg.lockH A D09-Jun-2018738

Gopkg.tomlH A D09-Jun-2018293

LICENSEH A D09-Jun-20181.1 KiB

README.mdH A D09-Jun-201810.1 KiB

doc.goH A D09-Jun-2018940

package_test.goH A D09-Jun-2018185

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) [![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
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  * [Testing suite interfaces and functions](#suite-package)
13
14Get started:
15
16  * Install testify with [one line of code](#installation), or [update it with another](#staying-up-to-date)
17  * For an introduction to writing test code in Go, see http://golang.org/doc/code.html#Testing
18  * Check out the API Documentation http://godoc.org/github.com/stretchr/testify
19  * To make your testing life easier, check out our other project, [gorc](http://github.com/stretchr/gorc)
20  * A little about [Test-Driven Development (TDD)](http://en.wikipedia.org/wiki/Test-driven_development)
21
22
23
24[`assert`](http://godoc.org/github.com/stretchr/testify/assert "API documentation") package
25-------------------------------------------------------------------------------------------
26
27The `assert` package provides some helpful methods that allow you to write better test code in Go.
28
29  * Prints friendly, easy to read failure descriptions
30  * Allows for very readable code
31  * Optionally annotate each assertion with a message
32
33See it in action:
34
35```go
36package yours
37
38import (
39  "testing"
40  "github.com/stretchr/testify/assert"
41)
42
43func TestSomething(t *testing.T) {
44
45  // assert equality
46  assert.Equal(t, 123, 123, "they should be equal")
47
48  // assert inequality
49  assert.NotEqual(t, 123, 456, "they should not be equal")
50
51  // assert for nil (good for errors)
52  assert.Nil(t, object)
53
54  // assert for not nil (good when you expect something)
55  if assert.NotNil(t, object) {
56
57    // now we know that object isn't nil, we are safe to make
58    // further assertions without causing any errors
59    assert.Equal(t, "Something", object.Value)
60
61  }
62
63}
64```
65
66  * 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.
67  * 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.
68
69if you assert many times, use the below:
70
71```go
72package yours
73
74import (
75  "testing"
76  "github.com/stretchr/testify/assert"
77)
78
79func TestSomething(t *testing.T) {
80  assert := assert.New(t)
81
82  // assert equality
83  assert.Equal(123, 123, "they should be equal")
84
85  // assert inequality
86  assert.NotEqual(123, 456, "they should not be equal")
87
88  // assert for nil (good for errors)
89  assert.Nil(object)
90
91  // assert for not nil (good when you expect something)
92  if assert.NotNil(object) {
93
94    // now we know that object isn't nil, we are safe to make
95    // further assertions without causing any errors
96    assert.Equal("Something", object.Value)
97  }
98}
99```
100
101[`require`](http://godoc.org/github.com/stretchr/testify/require "API documentation") package
102---------------------------------------------------------------------------------------------
103
104The `require` package provides same global functions as the `assert` package, but instead of returning a boolean result they terminate current test.
105
106See [t.FailNow](http://golang.org/pkg/testing/#T.FailNow) for details.
107
108[`mock`](http://godoc.org/github.com/stretchr/testify/mock "API documentation") package
109----------------------------------------------------------------------------------------
110
111The `mock` package provides a mechanism for easily writing mock objects that can be used in place of real objects when writing test code.
112
113An 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:
114
115```go
116package yours
117
118import (
119  "testing"
120  "github.com/stretchr/testify/mock"
121)
122
123/*
124  Test objects
125*/
126
127// MyMockedObject is a mocked object that implements an interface
128// that describes an object that the code I am testing relies on.
129type MyMockedObject struct{
130  mock.Mock
131}
132
133// DoSomething is a method on MyMockedObject that implements some interface
134// and just records the activity, and returns what the Mock object tells it to.
135//
136// In the real object, this method would do something useful, but since this
137// is a mocked object - we're just going to stub it out.
138//
139// NOTE: This method is not being tested here, code that uses this object is.
140func (m *MyMockedObject) DoSomething(number int) (bool, error) {
141
142  args := m.Called(number)
143  return args.Bool(0), args.Error(1)
144
145}
146
147/*
148  Actual test functions
149*/
150
151// TestSomething is an example of how to use our test object to
152// make assertions about some target code we are testing.
153func TestSomething(t *testing.T) {
154
155  // create an instance of our test object
156  testObj := new(MyMockedObject)
157
158  // setup expectations
159  testObj.On("DoSomething", 123).Return(true, nil)
160
161  // call the code we are testing
162  targetFuncThatDoesSomethingWithObj(testObj)
163
164  // assert that the expectations were met
165  testObj.AssertExpectations(t)
166
167
168}
169
170// TestSomethingElse is a second example of how to use our test object to
171// make assertions about some target code we are testing.
172// This time using a placeholder. Placeholders might be used when the
173// data being passed in is normally dynamically generated and cannot be
174// predicted beforehand (eg. containing hashes that are time sensitive)
175func TestSomethingElse(t *testing.T) {
176
177  // create an instance of our test object
178  testObj := new(MyMockedObject)
179
180  // setup expectations with a placeholder in the argument list
181  testObj.On("DoSomething", mock.Anything).Return(true, nil)
182
183  // call the code we are testing
184  targetFuncThatDoesSomethingWithObj(testObj)
185
186  // assert that the expectations were met
187  testObj.AssertExpectations(t)
188
189
190}
191```
192
193For 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).
194
195You 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.
196
197[`suite`](http://godoc.org/github.com/stretchr/testify/suite "API documentation") package
198-----------------------------------------------------------------------------------------
199
200The `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.
201
202An example suite is shown below:
203
204```go
205// Basic imports
206import (
207    "testing"
208    "github.com/stretchr/testify/assert"
209    "github.com/stretchr/testify/suite"
210)
211
212// Define the suite, and absorb the built-in basic suite
213// functionality from testify - including a T() method which
214// returns the current testing context
215type ExampleTestSuite struct {
216    suite.Suite
217    VariableThatShouldStartAtFive int
218}
219
220// Make sure that VariableThatShouldStartAtFive is set to five
221// before each test
222func (suite *ExampleTestSuite) SetupTest() {
223    suite.VariableThatShouldStartAtFive = 5
224}
225
226// All methods that begin with "Test" are run as tests within a
227// suite.
228func (suite *ExampleTestSuite) TestExample() {
229    assert.Equal(suite.T(), 5, suite.VariableThatShouldStartAtFive)
230}
231
232// In order for 'go test' to run this suite, we need to create
233// a normal test function and pass our suite to suite.Run
234func TestExampleTestSuite(t *testing.T) {
235    suite.Run(t, new(ExampleTestSuite))
236}
237```
238
239For 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)
240
241For more information on writing suites, check out the [API documentation for the `suite` package](http://godoc.org/github.com/stretchr/testify/suite).
242
243`Suite` object has assertion methods:
244
245```go
246// Basic imports
247import (
248    "testing"
249    "github.com/stretchr/testify/suite"
250)
251
252// Define the suite, and absorb the built-in basic suite
253// functionality from testify - including assertion methods.
254type ExampleTestSuite struct {
255    suite.Suite
256    VariableThatShouldStartAtFive int
257}
258
259// Make sure that VariableThatShouldStartAtFive is set to five
260// before each test
261func (suite *ExampleTestSuite) SetupTest() {
262    suite.VariableThatShouldStartAtFive = 5
263}
264
265// All methods that begin with "Test" are run as tests within a
266// suite.
267func (suite *ExampleTestSuite) TestExample() {
268    suite.Equal(suite.VariableThatShouldStartAtFive, 5)
269}
270
271// In order for 'go test' to run this suite, we need to create
272// a normal test function and pass our suite to suite.Run
273func TestExampleTestSuite(t *testing.T) {
274    suite.Run(t, new(ExampleTestSuite))
275}
276```
277
278------
279
280Installation
281============
282
283To install Testify, use `go get`:
284
285    go get github.com/stretchr/testify
286
287This will then make the following packages available to you:
288
289    github.com/stretchr/testify/assert
290    github.com/stretchr/testify/mock
291    github.com/stretchr/testify/http
292
293Import the `testify/assert` package into your code using this template:
294
295```go
296package yours
297
298import (
299  "testing"
300  "github.com/stretchr/testify/assert"
301)
302
303func TestSomething(t *testing.T) {
304
305  assert.True(t, true, "True is true!")
306
307}
308```
309
310------
311
312Staying up to date
313==================
314
315To update Testify to the latest version, use `go get -u github.com/stretchr/testify`.
316
317------
318
319Supported go versions
320==================
321
322We support the three major Go versions, which are 1.8, 1.9 and 1.10 at the moment.
323
324------
325
326Contributing
327============
328
329Please feel free to submit issues, fork the repository and send pull requests!
330
331When 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.
332