@@ -38,30 +38,27 @@ See it in action:
3838package yours
3939
4040import (
41- " testing"
42- " github.com/stretchr/testify/assert"
41+ " testing"
42+
43+ " github.com/stretchr/testify/assert"
4344)
4445
4546func TestSomething (t *testing .T ) {
47+ // assert equality
48+ assert.Equal (t, 123 , 123 , " they should be equal" )
4649
47- // assert equality
48- assert.Equal (t, 123 , 123 , " they should be equal" )
49-
50- // assert inequality
51- assert.NotEqual (t, 123 , 456 , " they should not be equal" )
52-
53- // assert for nil (good for errors)
54- assert.Nil (t, object)
50+ // assert inequality
51+ assert.NotEqual (t, 123 , 456 , " they should not be equal" )
5552
56- // assert for not nil (good when you expect something)
57- if assert.NotNil (t, object) {
58-
59- // now we know that object isn't nil, we are safe to make
60- // further assertions without causing any errors
61- assert.Equal (t, " Something" , object.Value )
62-
63- }
53+ // assert for nil (good for errors)
54+ assert.Nil (t, object)
6455
56+ // assert for not nil (good when you expect something)
57+ 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 )
61+ }
6562}
6663```
6764
@@ -74,29 +71,29 @@ if you assert many times, use the below:
7471package yours
7572
7673import (
77- " testing"
78- " github.com/stretchr/testify/assert"
74+ " testing"
75+
76+ " github.com/stretchr/testify/assert"
7977)
8078
8179func TestSomething (t *testing .T ) {
82- assert := assert.New (t)
80+ assert := assert.New (t)
8381
84- // assert equality
85- assert.Equal (123 , 123 , " they should be equal" )
82+ // assert equality
83+ assert.Equal (123 , 123 , " they should be equal" )
8684
87- // assert inequality
88- assert.NotEqual (123 , 456 , " they should not be equal" )
85+ // assert inequality
86+ assert.NotEqual (123 , 456 , " they should not be equal" )
8987
90- // assert for nil (good for errors)
91- assert.Nil (object)
88+ // assert for nil (good for errors)
89+ assert.Nil (object)
9290
93- // assert for not nil (good when you expect something)
94- if assert.NotNil (object) {
95-
96- // now we know that object isn't nil, we are safe to make
97- // further assertions without causing any errors
98- assert.Equal (" Something" , object.Value )
99- }
91+ // assert for not nil (good when you expect something)
92+ if assert.NotNil (object) {
93+ // now we know that object isn't nil, we are safe to make
94+ // further assertions without causing any errors
95+ assert.Equal (" Something" , object.Value )
96+ }
10097}
10198```
10299
@@ -120,8 +117,9 @@ An example test function that tests a piece of code that relies on an external o
120117package yours
121118
122119import (
123- " testing"
124- " github.com/stretchr/testify/mock"
120+ " testing"
121+
122+ " github.com/stretchr/testify/mock"
125123)
126124
127125/*
@@ -130,8 +128,8 @@ import (
130128
131129// MyMockedObject is a mocked object that implements an interface
132130// that describes an object that the code I am testing relies on.
133- type MyMockedObject struct {
134- mock.Mock
131+ type MyMockedObject struct {
132+ mock.Mock
135133}
136134
137135// DoSomething is a method on MyMockedObject that implements some interface
@@ -142,10 +140,8 @@ type MyMockedObject struct{
142140//
143141// NOTE: This method is not being tested here, code that uses this object is.
144142func (m *MyMockedObject ) DoSomething (number int ) (bool , error ) {
145-
146- args := m.Called (number)
147- return args.Bool (0 ), args.Error (1 )
148-
143+ args := m.Called (number)
144+ return args.Bool (0 ), args.Error (1 )
149145}
150146
151147/*
@@ -155,20 +151,17 @@ func (m *MyMockedObject) DoSomething(number int) (bool, error) {
155151// TestSomething is an example of how to use our test object to
156152// make assertions about some target code we are testing.
157153func TestSomething (t *testing .T ) {
154+ // create an instance of our test object
155+ testObj := new (MyMockedObject)
158156
159- // create an instance of our test object
160- testObj := new (MyMockedObject)
161-
162- // set up expectations
163- testObj.On (" DoSomething" , 123 ).Return (true , nil )
164-
165- // call the code we are testing
166- targetFuncThatDoesSomethingWithObj (testObj)
167-
168- // assert that the expectations were met
169- testObj.AssertExpectations (t)
157+ // set up expectations
158+ testObj.On (" DoSomething" , 123 ).Return (true , nil )
170159
160+ // call the code we are testing
161+ targetFuncThatDoesSomethingWithObj (testObj)
171162
163+ // assert that the expectations were met
164+ testObj.AssertExpectations (t)
172165}
173166
174167// TestSomethingWithPlaceholder is a second example of how to use our test object to
@@ -177,45 +170,42 @@ func TestSomething(t *testing.T) {
177170// data being passed in is normally dynamically generated and cannot be
178171// predicted beforehand (eg. containing hashes that are time sensitive)
179172func TestSomethingWithPlaceholder (t *testing .T ) {
173+ // create an instance of our test object
174+ testObj := new (MyMockedObject)
180175
181- // create an instance of our test object
182- testObj := new (MyMockedObject)
183-
184- // set up expectations with a placeholder in the argument list
185- testObj.On (" DoSomething" , mock.Anything ).Return (true , nil )
176+ // set up expectations with a placeholder in the argument list
177+ testObj.On (" DoSomething" , mock.Anything ).Return (true , nil )
186178
187- // call the code we are testing
188- targetFuncThatDoesSomethingWithObj (testObj)
189-
190- // assert that the expectations were met
191- testObj.AssertExpectations (t)
179+ // call the code we are testing
180+ targetFuncThatDoesSomethingWithObj (testObj)
192181
182+ // assert that the expectations were met
183+ testObj.AssertExpectations (t)
193184
194185}
195186
196187// TestSomethingElse2 is a third example that shows how you can use
197188// the Unset method to cleanup handlers and then add new ones.
198189func TestSomethingElse2 (t *testing .T ) {
190+ // create an instance of our test object
191+ testObj := new (MyMockedObject)
199192
200- // create an instance of our test object
201- testObj := new (MyMockedObject)
202-
203- // set up expectations with a placeholder in the argument list
204- mockCall := testObj.On (" DoSomething" , mock.Anything ).Return (true , nil )
193+ // set up expectations with a placeholder in the argument list
194+ mockCall := testObj.On (" DoSomething" , mock.Anything ).Return (true , nil )
205195
206- // call the code we are testing
207- targetFuncThatDoesSomethingWithObj (testObj)
196+ // call the code we are testing
197+ targetFuncThatDoesSomethingWithObj (testObj)
208198
209- // assert that the expectations were met
210- testObj.AssertExpectations (t)
199+ // assert that the expectations were met
200+ testObj.AssertExpectations (t)
211201
212- // remove the handler now so we can add another one that takes precedence
213- mockCall.Unset ()
202+ // remove the handler now so we can add another one that takes precedence
203+ mockCall.Unset ()
214204
215- // return false now instead of true
216- testObj.On (" DoSomething" , mock.Anything ).Return (false , nil )
205+ // return false now instead of true
206+ testObj.On (" DoSomething" , mock.Anything ).Return (false , nil )
217207
218- testObj.AssertExpectations (t)
208+ testObj.AssertExpectations (t)
219209}
220210```
221211
@@ -235,35 +225,36 @@ An example suite is shown below:
235225``` go
236226// Basic imports
237227import (
238- " testing"
239- " github.com/stretchr/testify/assert"
240- " github.com/stretchr/testify/suite"
228+ " testing"
229+
230+ " github.com/stretchr/testify/assert"
231+ " github.com/stretchr/testify/suite"
241232)
242233
243234// Define the suite, and absorb the built-in basic suite
244235// functionality from testify - including a T() method which
245236// returns the current testing context
246237type ExampleTestSuite struct {
247- suite.Suite
248- VariableThatShouldStartAtFive int
238+ suite.Suite
239+ VariableThatShouldStartAtFive int
249240}
250241
251242// Make sure that VariableThatShouldStartAtFive is set to five
252243// before each test
253244func (suite *ExampleTestSuite ) SetupTest () {
254- suite.VariableThatShouldStartAtFive = 5
245+ suite.VariableThatShouldStartAtFive = 5
255246}
256247
257248// All methods that begin with "Test" are run as tests within a
258249// suite.
259250func (suite *ExampleTestSuite ) TestExample () {
260- assert.Equal (suite.T (), 5 , suite.VariableThatShouldStartAtFive )
251+ assert.Equal (suite.T (), 5 , suite.VariableThatShouldStartAtFive )
261252}
262253
263254// In order for 'go test' to run this suite, we need to create
264255// a normal test function and pass our suite to suite.Run
265256func TestExampleTestSuite (t *testing .T ) {
266- suite.Run (t, new (ExampleTestSuite))
257+ suite.Run (t, new (ExampleTestSuite))
267258}
268259```
269260
@@ -276,33 +267,34 @@ For more information on writing suites, check out the [API documentation for the
276267``` go
277268// Basic imports
278269import (
279- " testing"
280- " github.com/stretchr/testify/suite"
270+ " testing"
271+
272+ " github.com/stretchr/testify/suite"
281273)
282274
283275// Define the suite, and absorb the built-in basic suite
284276// functionality from testify - including assertion methods.
285277type ExampleTestSuite struct {
286- suite.Suite
287- VariableThatShouldStartAtFive int
278+ suite.Suite
279+ VariableThatShouldStartAtFive int
288280}
289281
290282// Make sure that VariableThatShouldStartAtFive is set to five
291283// before each test
292284func (suite *ExampleTestSuite ) SetupTest () {
293- suite.VariableThatShouldStartAtFive = 5
285+ suite.VariableThatShouldStartAtFive = 5
294286}
295287
296288// All methods that begin with "Test" are run as tests within a
297289// suite.
298290func (suite *ExampleTestSuite ) TestExample () {
299- suite.Equal (suite.VariableThatShouldStartAtFive , 5 )
291+ suite.Equal (suite.VariableThatShouldStartAtFive , 5 )
300292}
301293
302294// In order for 'go test' to run this suite, we need to create
303295// a normal test function and pass our suite to suite.Run
304296func TestExampleTestSuite (t *testing .T ) {
305- suite.Run (t, new (ExampleTestSuite))
297+ suite.Run (t, new (ExampleTestSuite))
306298}
307299```
308300
@@ -329,14 +321,13 @@ Import the `testify/assert` package into your code using this template:
329321package yours
330322
331323import (
332- " testing"
333- " github.com/stretchr/testify/assert"
324+ " testing"
325+
326+ " github.com/stretchr/testify/assert"
334327)
335328
336329func TestSomething (t *testing .T ) {
337-
338- assert.True (t, true , " True is true!" )
339-
330+ assert.True (t, true , " True is true!" )
340331}
341332```
342333
0 commit comments