Documentation
¶
Overview ¶
Package testr provides a minimal extension to the standard library's testing.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrIntentional = errors.New("testr: intentional error")
ErrIntentional is a sentinel error represents intentional error in the test.
Functions ¶
func Must ¶
func Must(err error)
Must is a helper that wraps a call to a function returning error and panics if the error is non-nil. It is intended for use in variable initializations such as
t := testr.MustV(json.Unmarshal(b, &v))
Example ¶
package main import ( "encoding/json" "fmt" "github.com/smoothprogrammer/testr" ) func main() { var v string testr.Must(json.Unmarshal([]byte(`"testr"`), &v)) fmt.Println(v) }
Output: testr
func MustV ¶
MustV is a helper that wraps a call to a function returning (V, error) and panics if the error is non-nil. It is intended for use in variable initializations such as
t := testr.MustV(json.Marshal(v))
Example ¶
package main import ( "encoding/json" "fmt" "github.com/smoothprogrammer/testr" ) func main() { b := testr.MustV(json.Marshal("testr")) fmt.Println(string(b)) }
Output: "testr"
Types ¶
type Assertion ¶
type Assertion struct {
// contains filtered or unexported fields
}
Assertion provides assertion methods around T.
func (*Assertion) Equal ¶
Equal asserts that the actual object are equal to the expected object. It can take options.
Example ¶
assert := testr.New(t) // using *testing.T assert.Equal(nil, nil) // PASS assert.Equal(false, true) // FAIL assert.Equal("nil", nil) // FAIL assert.Equal([]string{"hello\nworld"}, 0) // FAIL
Output: false != expected:true "nil" != expected:<nil> []string{"hello\nworld"} != expected:0
func (*Assertion) ErrorAs ¶
ErrorAs asserts that the actual error as the expected target. ErrorAs uses errors.As so it can use any perks that errors.As provides. It can take options.
Example ¶
assert := testr.New(t) // using *testing.T var e customError assert.ErrorAs(customError("err"), &e) // PASS assert.ErrorAs(errFoo, &e) // FAIL
Output: error(foo) != expected:as(*testr_test.customError)
func (*Assertion) ErrorIs ¶
ErrorIs asserts that the actual error are equal to the expected error. ErrorIs uses errors.Is so it can use any perks that errors.Is provides. It can take options.
Example ¶
assert := testr.New(t) // using *testing.T assert.ErrorIs(nil, nil) // PASS assert.ErrorIs(errFoo, errFoo) // PASS assert.ErrorIs(errWrapFoo, errFoo) // PASS assert.ErrorIs(errFoo, nil) // FAIL assert.ErrorIs(errFoo, errBar) // FAIL assert.ErrorIs(errWrapFoo, errBar) // FAIL
Output: error(foo) != expected:<nil> error(foo) != expected:error(bar) error(wrap foo) != expected:error(bar)
type Option ¶
type Option func(*option)
Option represents options for the Assertion's methods.
func WithFailNow ¶
func WithFailNow() Option
WithFailNow marks the test state as fail and stop the execution if the assertion is fail.
Example ¶
assert := testr.New(t) // using *testing.T assert.ErrorIs(errFoo, nil, testr.WithFailNow(), testr.WithMessage("using t.FailNow"), )
Output: error(foo) != expected:<nil> // using t.FailNow
func WithMessage ¶
WithMessage appends the message to the end of the output if the assertion is fail.
Example ¶
assert := testr.New(t) // using *testing.T assert.Equal(false, true, testr.WithMessage("assert equality")) assert.ErrorIs(errFoo, nil, testr.WithMessage("assert err is nil")) var e customError assert.ErrorAs(errFoo, &e, testr.WithMessage("assert err as customError")) assert.Panic(func() {}, testr.WithMessage("assert function is panic"))
Output: false != expected:true // assert equality error(foo) != expected:<nil> // assert err is nil error(foo) != expected:as(*testr_test.customError) // assert err as customError func() != expected:panic() // assert function is panic