mirror of https://github.com/stretchr/testify.git
commit
8e57a8565a
272
README.md
272
README.md
|
@ -31,53 +31,57 @@ The `assert` package provides some helpful methods that allow you to write bette
|
||||||
|
|
||||||
See it in action:
|
See it in action:
|
||||||
|
|
||||||
func TestSomething(t *testing.T) {
|
```go
|
||||||
|
func TestSomething(t *testing.T) {
|
||||||
|
|
||||||
// assert equality
|
// assert equality
|
||||||
assert.Equal(t, 123, 123, "they should be equal")
|
assert.Equal(t, 123, 123, "they should be equal")
|
||||||
|
|
||||||
// assert inequality
|
// assert inequality
|
||||||
assert.NotEqual(t, 123, 456, "they should not be equal")
|
assert.NotEqual(t, 123, 456, "they should not be equal")
|
||||||
|
|
||||||
// assert for nil (good for errors)
|
// assert for nil (good for errors)
|
||||||
assert.Nil(t, object)
|
assert.Nil(t, object)
|
||||||
|
|
||||||
// assert for not nil (good when you expect something)
|
// assert for not nil (good when you expect something)
|
||||||
if assert.NotNil(t, object) {
|
if assert.NotNil(t, object) {
|
||||||
|
|
||||||
// now we know that object isn't nil, we are safe to make
|
// now we know that object isn't nil, we are safe to make
|
||||||
// further assertions without causing any errors
|
// further assertions without causing any errors
|
||||||
assert.Equal(t, "Something", object.Value)
|
assert.Equal(t, "Something", object.Value)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
```
|
||||||
|
|
||||||
* 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.
|
* 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.
|
||||||
* 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.
|
* 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.
|
||||||
|
|
||||||
if you assert many times, use the below:
|
if you assert many times, use the below:
|
||||||
|
|
||||||
func TestSomething(t *testing.T) {
|
```go
|
||||||
assert := assert.New(t)
|
func TestSomething(t *testing.T) {
|
||||||
|
assert := assert.New(t)
|
||||||
|
|
||||||
// assert equality
|
// assert equality
|
||||||
assert.Equal(123, 123, "they should be equal")
|
assert.Equal(123, 123, "they should be equal")
|
||||||
|
|
||||||
// assert inequality
|
// assert inequality
|
||||||
assert.NotEqual(123, 456, "they should not be equal")
|
assert.NotEqual(123, 456, "they should not be equal")
|
||||||
|
|
||||||
// assert for nil (good for errors)
|
// assert for nil (good for errors)
|
||||||
assert.Nil(object)
|
assert.Nil(object)
|
||||||
|
|
||||||
// assert for not nil (good when you expect something)
|
// assert for not nil (good when you expect something)
|
||||||
if assert.NotNil(object) {
|
if assert.NotNil(object) {
|
||||||
|
|
||||||
// now we know that object isn't nil, we are safe to make
|
// now we know that object isn't nil, we are safe to make
|
||||||
// further assertions without causing any errors
|
// further assertions without causing any errors
|
||||||
assert.Equal("Something", object.Value)
|
assert.Equal("Something", object.Value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
```
|
||||||
|
|
||||||
`http` package
|
`http` package
|
||||||
--------------
|
--------------
|
||||||
|
@ -91,58 +95,60 @@ The `mock` package provides a mechanism for easily writing mock objects that can
|
||||||
|
|
||||||
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:
|
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:
|
||||||
|
|
||||||
package yours
|
```go
|
||||||
|
package yours
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
"github.com/stretchr/testify/mock"
|
"github.com/stretchr/testify/mock"
|
||||||
)
|
)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Test objects
|
Test objects
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// MyMockedObject is a mocked object that implements an interface
|
// MyMockedObject is a mocked object that implements an interface
|
||||||
// that describes an object that the code I am testing relies on.
|
// that describes an object that the code I am testing relies on.
|
||||||
type MyMockedObject struct{
|
type MyMockedObject struct{
|
||||||
mock.Mock
|
mock.Mock
|
||||||
}
|
}
|
||||||
|
|
||||||
// DoSomething is a method on MyMockedObject that implements some interface
|
// DoSomething is a method on MyMockedObject that implements some interface
|
||||||
// and just records the activity, and returns what the Mock object tells it to.
|
// and just records the activity, and returns what the Mock object tells it to.
|
||||||
//
|
//
|
||||||
// In the real object, this method would do something useful, but since this
|
// In the real object, this method would do something useful, but since this
|
||||||
// is a mocked object - we're just going to stub it out.
|
// is a mocked object - we're just going to stub it out.
|
||||||
//
|
//
|
||||||
// NOTE: This method is not being tested here, code that uses this object is.
|
// NOTE: This method is not being tested here, code that uses this object is.
|
||||||
func (m *MyMockedObject) DoSomething(number int) (bool, error) {
|
func (m *MyMockedObject) DoSomething(number int) (bool, error) {
|
||||||
|
|
||||||
args := m.Mock.Called(number)
|
args := m.Mock.Called(number)
|
||||||
return args.Bool(0), args.Error(1)
|
return args.Bool(0), args.Error(1)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Actual test functions
|
Actual test functions
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// TestSomething is an example of how to use our test object to
|
// TestSomething is an example of how to use our test object to
|
||||||
// make assertions about some target code we are testing.
|
// make assertions about some target code we are testing.
|
||||||
func TestSomething(t *testing.T) {
|
func TestSomething(t *testing.T) {
|
||||||
|
|
||||||
// create an instance of our test object
|
// create an instance of our test object
|
||||||
testObj := new(MyMockedObject)
|
testObj := new(MyMockedObject)
|
||||||
|
|
||||||
// setup expectations
|
// setup expectations
|
||||||
testObj.On("DoSomething", 123).Return(true, nil)
|
testObj.On("DoSomething", 123).Return(true, nil)
|
||||||
|
|
||||||
// call the code we are testing
|
// call the code we are testing
|
||||||
targetFuncThatDoesSomethingWithObj(testObj)
|
targetFuncThatDoesSomethingWithObj(testObj)
|
||||||
|
|
||||||
// assert that the expectations were met
|
// assert that the expectations were met
|
||||||
testObj.Mock.AssertExpectations(t)
|
testObj.Mock.AssertExpectations(t)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
```
|
||||||
|
|
||||||
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).
|
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).
|
||||||
|
|
||||||
|
@ -153,38 +159,40 @@ The `suite` package provides functionality that you might be used to from more c
|
||||||
|
|
||||||
An example suite is shown below:
|
An example suite is shown below:
|
||||||
|
|
||||||
// Basic imports
|
```go
|
||||||
import (
|
// Basic imports
|
||||||
"testing"
|
import (
|
||||||
"github.com/stretchr/testify/assert"
|
"testing"
|
||||||
"github.com/stretchr/testify/suite"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
"github.com/stretchr/testify/suite"
|
||||||
|
)
|
||||||
|
|
||||||
// Define the suite, and absorb the built-in basic suite
|
// Define the suite, and absorb the built-in basic suite
|
||||||
// functionality from testify - including a T() method which
|
// functionality from testify - including a T() method which
|
||||||
// returns the current testing context
|
// returns the current testing context
|
||||||
type ExampleTestSuite struct {
|
type ExampleTestSuite struct {
|
||||||
suite.Suite
|
suite.Suite
|
||||||
VariableThatShouldStartAtFive int
|
VariableThatShouldStartAtFive int
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make sure that VariableThatShouldStartAtFive is set to five
|
// Make sure that VariableThatShouldStartAtFive is set to five
|
||||||
// before each test
|
// before each test
|
||||||
func (suite *ExampleTestSuite) SetupTest() {
|
func (suite *ExampleTestSuite) SetupTest() {
|
||||||
suite.VariableThatShouldStartAtFive = 5
|
suite.VariableThatShouldStartAtFive = 5
|
||||||
}
|
}
|
||||||
|
|
||||||
// All methods that begin with "Test" are run as tests within a
|
// All methods that begin with "Test" are run as tests within a
|
||||||
// suite.
|
// suite.
|
||||||
func (suite *ExampleTestSuite) TestExample() {
|
func (suite *ExampleTestSuite) TestExample() {
|
||||||
assert.Equal(suite.T(), suite.VariableThatShouldStartAtFive, 5)
|
assert.Equal(suite.T(), suite.VariableThatShouldStartAtFive, 5)
|
||||||
}
|
}
|
||||||
|
|
||||||
// In order for 'go test' to run this suite, we need to create
|
// In order for 'go test' to run this suite, we need to create
|
||||||
// a normal test function and pass our suite to suite.Run
|
// a normal test function and pass our suite to suite.Run
|
||||||
func TestExampleTestSuite(t *testing.T) {
|
func TestExampleTestSuite(t *testing.T) {
|
||||||
suite.Run(t, new(ExampleTestSuite))
|
suite.Run(t, new(ExampleTestSuite))
|
||||||
}
|
}
|
||||||
|
```
|
||||||
|
|
||||||
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)
|
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)
|
||||||
|
|
||||||
|
@ -192,36 +200,38 @@ For more information on writing suites, check out the [API documentation for the
|
||||||
|
|
||||||
`Suite` object has assertion methods:
|
`Suite` object has assertion methods:
|
||||||
|
|
||||||
// Basic imports
|
```go
|
||||||
import (
|
// Basic imports
|
||||||
"testing"
|
import (
|
||||||
"github.com/stretchr/testify/suite"
|
"testing"
|
||||||
)
|
"github.com/stretchr/testify/suite"
|
||||||
|
)
|
||||||
|
|
||||||
// Define the suite, and absorb the built-in basic suite
|
// Define the suite, and absorb the built-in basic suite
|
||||||
// functionality from testify - including assertion methods.
|
// functionality from testify - including assertion methods.
|
||||||
type ExampleTestSuite struct {
|
type ExampleTestSuite struct {
|
||||||
suite.Suite
|
suite.Suite
|
||||||
VariableThatShouldStartAtFive int
|
VariableThatShouldStartAtFive int
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make sure that VariableThatShouldStartAtFive is set to five
|
// Make sure that VariableThatShouldStartAtFive is set to five
|
||||||
// before each test
|
// before each test
|
||||||
func (suite *ExampleTestSuite) SetupTest() {
|
func (suite *ExampleTestSuite) SetupTest() {
|
||||||
suite.VariableThatShouldStartAtFive = 5
|
suite.VariableThatShouldStartAtFive = 5
|
||||||
}
|
}
|
||||||
|
|
||||||
// All methods that begin with "Test" are run as tests within a
|
// All methods that begin with "Test" are run as tests within a
|
||||||
// suite.
|
// suite.
|
||||||
func (suite *ExampleTestSuite) TestExample() {
|
func (suite *ExampleTestSuite) TestExample() {
|
||||||
suite.Equal(suite.VariableThatShouldStartAtFive, 5)
|
suite.Equal(suite.VariableThatShouldStartAtFive, 5)
|
||||||
}
|
}
|
||||||
|
|
||||||
// In order for 'go test' to run this suite, we need to create
|
// In order for 'go test' to run this suite, we need to create
|
||||||
// a normal test function and pass our suite to suite.Run
|
// a normal test function and pass our suite to suite.Run
|
||||||
func TestExampleTestSuite(t *testing.T) {
|
func TestExampleTestSuite(t *testing.T) {
|
||||||
suite.Run(t, new(ExampleTestSuite))
|
suite.Run(t, new(ExampleTestSuite))
|
||||||
}
|
}
|
||||||
|
```
|
||||||
|
|
||||||
------
|
------
|
||||||
|
|
||||||
|
@ -240,18 +250,20 @@ This will then make the following packages available to you:
|
||||||
|
|
||||||
Import the `testify/assert` package into your code using this template:
|
Import the `testify/assert` package into your code using this template:
|
||||||
|
|
||||||
package yours
|
```go
|
||||||
|
package yours
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestSomething(t *testing.T) {
|
func TestSomething(t *testing.T) {
|
||||||
|
|
||||||
assert.True(t, true, "True is true!")
|
assert.True(t, true, "True is true!")
|
||||||
|
|
||||||
}
|
}
|
||||||
|
```
|
||||||
|
|
||||||
------
|
------
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue