This is a clone/modification of github.com/stretchr/testify, which you should use instead of this. I am going to be making changes to this at some point, so do not use/rely on it being in a good state!
The assert
package provides some helpful methods that allow you to write better test code in Go.
- Prints friendly, easy to read failure descriptions
- Allows for very readable code
- Optionally annotate each assertion with a message
See it in action:
package yours
import (
"testing"
"hawx.me/code/assert"
)
func TestSomething(t *testing.T) {
assert := assert.Wrap(t)
// assert equality
assert(123).Equal(123, "they should be equal")
// assert inequality
assert(456).NotEqual(123, "they should not be equal")
// assert for nil (good for errors)
assert(object).Nil()
// assert for not nil (good when you expect something)
if assert(object).NotNil() {
// now we know that object isn't nil, we are safe to make
// further assertions without causing any errors
assert(object.Value).Equal("Something")
}
}
- Every assert func takes the
testing.T
object as the first argument. This is how it writes the errors out through the normalgo 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.
if you assert many times, use the below:
package yours
import (
"testing"
"hawx.me/code/assert"
)
func TestSomething(t *testing.T) {
assert := assert.New(t)
// assert equality
assert.Equal(123, 123, "they should be equal")
// assert inequality
assert.NotEqual(123, 456, "they should not be equal")
// assert for nil (good for errors)
assert.Nil(object)
// assert for not nil (good when you expect something)
if assert.NotNil(object) {
// now we know that object isn't nil, we are safe to make
// further assertions without causing any errors
assert.Equal("Something", object.Value)
}
}