Documentation ¶
Overview ¶
The asserts package provides a powerful and convenient set of assertions. Here expected and obtained values are compared. Individual descriptions as well as the output help to quickly find failed tests.
In the beginning of a test function a new assertion instance is created with:
assert := asserts.NewTestingAssertion(t, shallFail)
Inside the test an assert looks like:
assert.Equal(expected, obtained, "obtained value has to be like expected")
If shallFail is set to true a failing assert also lets fail the Go test. Otherwise the failing is printed but the tests continue.
Index ¶
- func ValueDescription(value interface{}) string
- type Assertion
- type Failable
- type Failer
- type Test
- type Tester
- func (t Tester) HasPanic(pf func()) (ok bool)
- func (t Tester) IsAbout(obtained, expected, extend float64) bool
- func (t Tester) IsAssignable(obtained, expected interface{}) bool
- func (t Tester) IsEqual(obtained, expected interface{}) bool
- func (t Tester) IsImplementor(obtained, expected interface{}) (bool, error)
- func (t Tester) IsMatching(obtained, regex string) (bool, error)
- func (t Tester) IsNil(obtained interface{}) bool
- func (t Tester) IsSubstring(obtained, full string) bool
- func (t Tester) IsTrue(obtained bool) bool
- func (t Tester) Len(obtained interface{}) (int, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ValueDescription ¶
func ValueDescription(value interface{}) string
ValueDescription returns a description of a value as string.
Types ¶
type Assertion ¶
type Assertion interface { // Logf can be used to display useful information during testing. Logf(format string, args ...interface{}) // True tests if obtained is true. True(obtained bool, msgs ...string) bool // False tests if obtained is false. False(obtained bool, msgs ...string) bool // Nil tests if obtained is nil. Nil(obtained interface{}, msgs ...string) bool // NotNil tests if obtained is not nil. NotNil(obtained interface{}, msgs ...string) bool // Equal tests if obtained and expected are equal. Equal(obtained, expected interface{}, msgs ...string) bool // Different tests if obtained and expected are different. Different(obtained, expected interface{}, msgs ...string) bool // About tests if obtained and expected are near to each other // (within the given extend). About(obtained, expected, extend float64, msgs ...string) bool // Substring tests if obtained is a substring of the full string. Substring(obtained, full string, msgs ...string) bool // Match tests if the obtained string matches a regular expression. Match(obtained, regex string, msgs ...string) bool // ErrorMatch tests if the obtained error as string matches a // regular expression. ErrorMatch(obtained error, regex string, msgs ...string) bool // Implementor tests if obtained implements the expected // interface variable pointer. Implementor(obtained, expected interface{}, msgs ...string) bool // Assignable tests if the types of expected and obtained are assignable. Assignable(obtained, expected interface{}, msgs ...string) bool // Unassignable tests if the types of expected and obtained are // not assignable. Unassignable(obtained, expected interface{}, msgs ...string) bool // Empty tests if the len of the obtained string, array, slice // map or channel is 0. Empty(obtained interface{}, msgs ...string) bool // NotEmpty tests if the len of the obtained string, array, slice // map or channel is greater than 0. NotEmpty(obtained interface{}, msgs ...string) bool // Length tests if the len of the obtained string, array, slice // map or channel is equal to the expected one. Length(obtained interface{}, expected int, msgs ...string) bool // Panics checks if the passed function panics. Panics(pf func(), msgs ...string) bool // Fail always fails. Fail(msgs ...string) bool }
Assertion defines the available test methods.
func NewAssertion ¶
NewAssertion creates a new Assertion instance.
func NewPanicAssertion ¶
func NewPanicAssertion() Assertion
NewPanicAssertion creates a new Assertion instance which panics if a test fails.
func NewTestingAssertion ¶
NewTestingAssertion creates a new Assertion instance for use with the testing package. The *testing.T has to be passed as failable, the first argument. shallFail controls if a failing assertion also lets fail the Go test.
type Failable ¶
type Failable interface {
FailNow()
}
Failable allows an assertion to signal a fail to an external instance like testing.T or testing.B.
type Failer ¶
type Failer interface { // Logf can be used to display useful information during testing. Logf(format string, args ...interface{}) // Fail will be called if an assert fails. Fail(test Test, obtained, expected interface{}, msgs ...string) bool }
failer describes a type controlling how an assert reacts after a failure.
type Tester ¶
type Tester struct{}
Tester is a helper which can be used in own Assertion implementations.
func (Tester) IsAssignable ¶
IsAssignable checks if the types of obtained and expected are assignable.
func (Tester) IsImplementor ¶
IsImplementor checks if obtained implements the expected interface variable pointer.
func (Tester) IsMatching ¶
IsMatching checks if the obtained string matches a regular expression.
func (Tester) IsSubstring ¶
IsSubstring checks if obtained is a substring of the full string.