github.com/v2pro/plz@v0.0.0-20221028024117-e5f9aec5b631/test/testify/README.md (about) 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 * [Testing suite interfaces and functions](#suite-package) 13 14 Get 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 27 The `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 33 See it in action: 34 35 ```go 36 package yours 37 38 import ( 39 "testing" 40 "github.com/stretchr/testify/assert" 41 ) 42 43 func 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 69 if you assert many times, use the below: 70 71 ```go 72 package yours 73 74 import ( 75 "testing" 76 "github.com/stretchr/testify/assert" 77 ) 78 79 func 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 104 The `require` package provides same global functions as the `assert` package, but instead of returning a boolean result they terminate current test. 105 106 See [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 111 The `mock` package provides a mechanism for easily writing mock objects that can be used in place of real objects when writing test code. 112 113 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: 114 115 ```go 116 package yours 117 118 import ( 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. 129 type 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. 140 func (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. 153 func 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 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). 171 172 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. 173 174 [`suite`](http://godoc.org/github.com/stretchr/testify/suite "API documentation") package 175 ----------------------------------------------------------------------------------------- 176 177 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. 178 179 An example suite is shown below: 180 181 ```go 182 // Basic imports 183 import ( 184 "testing" 185 "github.com/stretchr/testify/assert" 186 "github.com/stretchr/testify/suite" 187 ) 188 189 // Define the suite, and absorb the built-in basic suite 190 // functionality from testify - including a T() method which 191 // returns the current testing context 192 type ExampleTestSuite struct { 193 suite.Suite 194 VariableThatShouldStartAtFive int 195 } 196 197 // Make sure that VariableThatShouldStartAtFive is set to five 198 // before each test 199 func (suite *ExampleTestSuite) SetupTest() { 200 suite.VariableThatShouldStartAtFive = 5 201 } 202 203 // All methods that begin with "Test" are run as tests within a 204 // suite. 205 func (suite *ExampleTestSuite) TestExample() { 206 assert.Equal(suite.T(), 5, suite.VariableThatShouldStartAtFive) 207 } 208 209 // In order for 'go test' to run this suite, we need to create 210 // a normal test function and pass our suite to suite.Run 211 func TestExampleTestSuite(t *testing.T) { 212 suite.Run(t, new(ExampleTestSuite)) 213 } 214 ``` 215 216 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) 217 218 For more information on writing suites, check out the [API documentation for the `suite` package](http://godoc.org/github.com/stretchr/testify/suite). 219 220 `Suite` object has assertion methods: 221 222 ```go 223 // Basic imports 224 import ( 225 "testing" 226 "github.com/stretchr/testify/suite" 227 ) 228 229 // Define the suite, and absorb the built-in basic suite 230 // functionality from testify - including assertion methods. 231 type ExampleTestSuite struct { 232 suite.Suite 233 VariableThatShouldStartAtFive int 234 } 235 236 // Make sure that VariableThatShouldStartAtFive is set to five 237 // before each test 238 func (suite *ExampleTestSuite) SetupTest() { 239 suite.VariableThatShouldStartAtFive = 5 240 } 241 242 // All methods that begin with "Test" are run as tests within a 243 // suite. 244 func (suite *ExampleTestSuite) TestExample() { 245 suite.Equal(suite.VariableThatShouldStartAtFive, 5) 246 } 247 248 // In order for 'go test' to run this suite, we need to create 249 // a normal test function and pass our suite to suite.Run 250 func TestExampleTestSuite(t *testing.T) { 251 suite.Run(t, new(ExampleTestSuite)) 252 } 253 ``` 254 255 ------ 256 257 Installation 258 ============ 259 260 To install Testify, use `go get`: 261 262 go get github.com/stretchr/testify 263 264 This will then make the following packages available to you: 265 266 github.com/stretchr/testify/assert 267 github.com/stretchr/testify/mock 268 github.com/stretchr/testify/http 269 270 Import the `testify/assert` package into your code using this template: 271 272 ```go 273 package yours 274 275 import ( 276 "testing" 277 "github.com/stretchr/testify/assert" 278 ) 279 280 func TestSomething(t *testing.T) { 281 282 assert.True(t, true, "True is true!") 283 284 } 285 ``` 286 287 ------ 288 289 Staying up to date 290 ================== 291 292 To update Testify to the latest version, use `go get -u github.com/stretchr/testify`. 293 294 ------ 295 296 Contributing 297 ============ 298 299 Please feel free to submit issues, fork the repository and send pull requests! 300 301 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.