1 Testify - Thou Shalt Write Tests
2 ================================
4 [![Build Status](https://travis-ci.org/stretchr/testify.svg)](https://travis-ci.org/stretchr/testify)
6 Go code (golang) set of packages that provide many tools for testifying that your code will behave as you intend.
10 * [Easy assertions](#assert-package)
11 * [Mocking](#mock-package)
12 * [HTTP response trapping](#http-package)
13 * [Testing suite interfaces and functions](#suite-package)
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)
25 [`assert`](http://godoc.org/github.com/stretchr/testify/assert "API documentation") package
26 -------------------------------------------------------------------------------------------
28 The `assert` package provides some helpful methods that allow you to write better test code in Go.
30 * Prints friendly, easy to read failure descriptions
31 * Allows for very readable code
32 * Optionally annotate each assertion with a message
41 "github.com/stretchr/testify/assert"
44 func TestSomething(t *testing.T) {
47 assert.Equal(t, 123, 123, "they should be equal")
50 assert.NotEqual(t, 123, 456, "they should not be equal")
52 // assert for nil (good for errors)
55 // assert for not nil (good when you expect something)
56 if assert.NotNil(t, object) {
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)
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.
70 if you assert many times, use the below:
77 "github.com/stretchr/testify/assert"
80 func TestSomething(t *testing.T) {
81 assert := assert.New(t)
84 assert.Equal(123, 123, "they should be equal")
87 assert.NotEqual(123, 456, "they should not be equal")
89 // assert for nil (good for errors)
92 // assert for not nil (good when you expect something)
93 if assert.NotNil(object) {
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)
102 [`require`](http://godoc.org/github.com/stretchr/testify/require "API documentation") package
103 ---------------------------------------------------------------------------------------------
105 The `require` package provides same global functions as the `assert` package, but instead of returning a boolean result they terminate current test.
107 See [t.FailNow](http://golang.org/pkg/testing/#T.FailNow) for details.
110 [`http`](http://godoc.org/github.com/stretchr/testify/http "API documentation") package
111 ---------------------------------------------------------------------------------------
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).
115 We recommend you use [httptest](http://golang.org/pkg/net/http/httptest) instead.
117 [`mock`](http://godoc.org/github.com/stretchr/testify/mock "API documentation") package
118 ----------------------------------------------------------------------------------------
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.
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:
129 "github.com/stretchr/testify/mock"
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{
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.
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.
148 // NOTE: This method is not being tested here, code that uses this object is.
149 func (m *MyMockedObject) DoSomething(number int) (bool, error) {
151 args := m.Called(number)
152 return args.Bool(0), args.Error(1)
157 Actual test functions
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) {
164 // create an instance of our test object
165 testObj := new(MyMockedObject)
167 // setup expectations
168 testObj.On("DoSomething", 123).Return(true, nil)
170 // call the code we are testing
171 targetFuncThatDoesSomethingWithObj(testObj)
173 // assert that the expectations were met
174 testObj.AssertExpectations(t)
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).
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.
183 [`suite`](http://godoc.org/github.com/stretchr/testify/suite "API documentation") package
184 -----------------------------------------------------------------------------------------
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.
188 An example suite is shown below:
194 "github.com/stretchr/testify/assert"
195 "github.com/stretchr/testify/suite"
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 {
203 VariableThatShouldStartAtFive int
206 // Make sure that VariableThatShouldStartAtFive is set to five
208 func (suite *ExampleTestSuite) SetupTest() {
209 suite.VariableThatShouldStartAtFive = 5
212 // All methods that begin with "Test" are run as tests within a
214 func (suite *ExampleTestSuite) TestExample() {
215 assert.Equal(suite.T(), 5, suite.VariableThatShouldStartAtFive)
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))
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)
227 For more information on writing suites, check out the [API documentation for the `suite` package](http://godoc.org/github.com/stretchr/testify/suite).
229 `Suite` object has assertion methods:
235 "github.com/stretchr/testify/suite"
238 // Define the suite, and absorb the built-in basic suite
239 // functionality from testify - including assertion methods.
240 type ExampleTestSuite struct {
242 VariableThatShouldStartAtFive int
245 // Make sure that VariableThatShouldStartAtFive is set to five
247 func (suite *ExampleTestSuite) SetupTest() {
248 suite.VariableThatShouldStartAtFive = 5
251 // All methods that begin with "Test" are run as tests within a
253 func (suite *ExampleTestSuite) TestExample() {
254 suite.Equal(suite.VariableThatShouldStartAtFive, 5)
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))
269 To install Testify, use `go get`:
271 * Latest version: go get github.com/stretchr/testify
272 * Specific version: go get gopkg.in/stretchr/testify.v1
274 This will then make the following packages available to you:
276 github.com/stretchr/testify/assert
277 github.com/stretchr/testify/mock
278 github.com/stretchr/testify/http
280 Import the `testify/assert` package into your code using this template:
287 "github.com/stretchr/testify/assert"
290 func TestSomething(t *testing.T) {
292 assert.True(t, true, "True is true!")
302 To update Testify to the latest version, use `go get -u github.com/stretchr/testify`.
309 * 1.0 - New package versioning strategy adopted.
316 Please feel free to submit issues, fork the repository and send pull requests!
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.
324 Copyright (c) 2012 - 2013 Mat Ryer and Tyler Bunnell
326 Please consider promoting this project if you find it useful.
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:
330 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
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.