Documentation
¶
Overview ¶
Package check is a rich testing extension for Go's testing package.
For details about the project, see:
http://labix.org/gocheck
Index ¶
- Variables
- func List(suite any, runConf *RunConf) []string
- func ListAll(runConf *RunConf) []string
- func ObjectsAreEqual(expected, actual any) bool
- func Suite(suite any) any
- func TestingT(testingT *testing.T)
- type C
- func (c *C) Assert(obtained any, checker Checker, args ...any)
- func (c *C) Check(obtained any, checker Checker, args ...any) bool
- func (c *C) Contains(obtained, expected any, args ...any) bool
- func (c *C) DeepEquals(obtained, expected any, args ...any) bool
- func (c *C) Empty(obtained any, args ...any) bool
- func (c *C) Equal(obtained, expected any, args ...any) bool
- func (c *C) Equals(obtained, expected any, args ...any) bool
- func (c *C) EqualsFloat32(obtained, expected any, args ...any) bool
- func (c *C) EqualsMore(obtained, expected any, args ...any) bool
- func (c *C) Error(args ...any)
- func (c *C) ErrorIs(obtained, expected any, args ...any) bool
- func (c *C) ErrorMatches(obtained, expected any, args ...any) bool
- func (c *C) Errorf(format string, args ...any)
- func (c *C) ExpectFailure(reason string)
- func (c *C) Fail(mess ...any)
- func (c *C) FailNow(mess ...any)
- func (c *C) Failed() bool
- func (c *C) False(obtained any, args ...any) bool
- func (c *C) Fatal(args ...any)
- func (c *C) Fatalf(format string, args ...any)
- func (c *C) FitsTypeOf(obtained, expected any, args ...any) bool
- func (c *C) GetTestLog() string
- func (c *C) Greater(obtained, expected any, args ...any) bool
- func (c *C) GreaterOrEqual(obtained, expected any, args ...any) bool
- func (c *C) HasLen(obtained, expected any, args ...any) bool
- func (c *C) HasLenLessThan(obtained, expected any, args ...any) bool
- func (c *C) HasLenMoreThan(obtained, expected any, args ...any) bool
- func (c *C) Implements(obtained, expected any, args ...any) bool
- func (c *C) IsEmpty(obtained any, args ...any) bool
- func (c *C) IsFalse(obtained any, args ...any) bool
- func (c *C) IsNil(obtained any, args ...any) bool
- func (c *C) IsTrue(obtained any, args ...any) bool
- func (c *C) Len(obtained, expected any, args ...any) bool
- func (c *C) Less(obtained, expected any, args ...any) bool
- func (c *C) LessOrEqual(obtained, expected any, args ...any) bool
- func (c *C) LessOrEqualThan(obtained, expected any, args ...any) bool
- func (c *C) LessThan(obtained, expected any, args ...any) bool
- func (c *C) Log(args ...any)
- func (c *C) Logf(format string, args ...any)
- func (c *C) Matches(obtained, expected any, args ...any) bool
- func (c *C) MkDir() string
- func (c *C) MoreOrEqualThan(obtained, expected any, args ...any) bool
- func (c *C) MoreThan(obtained, expected any, args ...any) bool
- func (c *C) Nil(obtained any, args ...any) bool
- func (c *C) NotContains(obtained, expected any, args ...any) bool
- func (c *C) NotDeepEquals(obtained, expected any, args ...any) bool
- func (c *C) NotEmpty(obtained any, args ...any) bool
- func (c *C) NotEqual(obtained, expected any, args ...any) bool
- func (c *C) NotEquals(obtained, expected any, args ...any) bool
- func (c *C) NotEqualsFloat32(obtained, expected any, args ...any) bool
- func (c *C) NotEqualsMore(obtained, expected any, args ...any) bool
- func (c *C) NotHasLen(obtained, expected any, args ...any) bool
- func (c *C) NotMatches(obtained, expected any, args ...any) bool
- func (c *C) NotNil(obtained any, args ...any) bool
- func (c *C) Output(calldepth int, s string) error
- func (c *C) PanicMatches(obtained, expected any, args ...any) bool
- func (c *C) Panics(obtained, expected any, args ...any) bool
- func (c *C) ResetTimer()
- func (c *C) SetBytes(n int64)
- func (c *C) Skip(reason string)
- func (c *C) StartTimer()
- func (c *C) StopTimer()
- func (c *C) Succeed()
- func (c *C) SucceedNow()
- func (c *C) T() *testing.T
- func (c *C) TestName() string
- func (c *C) True(obtained any, args ...any) bool
- type CheckTestResult
- type Checker
- type CheckerInfo
- type CommentInterface
- type RunConf
Constants ¶
This section is empty.
Variables ¶
var NoEqualsFloat32LessThanMaxFloat32Error = "Comparing incomparable values as float32: one of parameters is less than -1 * math.MaxFloat32 / 2"
var NoEqualsFloat32MoreThanMaxFloat32Error = "Comparing incomparable values as float32: one of parameters is more than math.MaxFloat32 / 2"
var NoEqualsMoreStringError = "Difference: first (string) parameter does not equals (string) second, expect 'equals'"
----------------------------------------------------------------------- EqualsMore checker.
var NoLessOrEqualThanStringError = "Difference: first (string) parameter > (string) second, expect less or equal"
----------------------------------------------------------------------- LessOrEqualThan checker.
var NoLessThanStringError = "Difference: first (string) parameter <= (string) second, expect less"
var NoMoreOrEqualThanStringError = "Difference: first (string) parameter > (string) second, expect less or equal"
var NoMoreThanStringError = "Difference: first (string) parameter <= (string) second, expect more"
Functions ¶
func List ¶
List returns the names of the test functions in the given suite that will be run with the provided run configuration.
func ListAll ¶
ListAll returns the names of all the test functions registered with the Suite function that will be run with the provided run configuration.
func ObjectsAreEqual ¶ added in v0.2.0
ObjectsAreEqual determines if two objects are considered equal.
This function does no assertion of any kind.
Types ¶
type C ¶
type C struct {
// contains filtered or unexported fields
}
func (*C) Assert ¶
Assert ensures that the first value matches the expected value according to the provided checker. If they do not match, an error is logged, the test is marked as failed, and the test execution stops.
Some checkers may not need the expected argument (e.g. IsNil).
Extra arguments provided to the function are logged next to the reported problem when the matching fails.
func (*C) Check ¶
Check verifies if the first value matches the expected value according to the provided checker. If they do not match, an error is logged, the test is marked as failed, and the test execution continues.
Some checkers may not need the expected argument (e.g. IsNil).
Extra arguments provided to the function are logged next to the reported problem when the matching fails.
func (*C) DeepEquals ¶ added in v0.2.0
func (*C) EqualsFloat32 ¶ added in v0.2.0
func (*C) EqualsMore ¶ added in v0.2.0
func (*C) Error ¶
Error logs an error into the test error output and marks the test as failed. The provided arguments are assembled together into a string with fmt.Sprint.
func (*C) ErrorMatches ¶ added in v0.2.0
func (*C) Errorf ¶
Errorf logs an error into the test error output and marks the test as failed. The provided arguments are assembled together into a string with fmt.Sprintf.
func (*C) ExpectFailure ¶
ExpectFailure informs that the running test is knowingly broken for the provided reason. If the test does not fail, an error will be reported to raise attention to this fact. This method is useful to temporarily disable tests which cover well known problems until a better time to fix the problem is found, without forgetting about the fact that a failure still exists.
func (*C) Fail ¶
Fail marks the currently running test as failed.
Something ought to have been previously logged so the developer can tell what went wrong. The higher level helper functions will fail the test and do the logging properly.
func (*C) FailNow ¶
FailNow marks the currently running test as failed and stops running it. Something ought to have been previously logged so the developer can tell what went wrong. The higher level helper functions will fail the test and do the logging properly.
func (*C) Fatal ¶
Fatal logs an error into the test error output, marks the test as failed, and stops the test execution. The provided arguments are assembled together into a string with fmt.Sprint.
func (*C) Fatalf ¶
Fatlaf logs an error into the test error output, marks the test as failed, and stops the test execution. The provided arguments are assembled together into a string with fmt.Sprintf.
func (*C) FitsTypeOf ¶ added in v0.2.0
func (*C) GetTestLog ¶
GetTestLog returns the current test error output.
func (*C) GreaterOrEqual ¶ added in v0.2.2
func (*C) HasLenLessThan ¶ added in v0.2.0
func (*C) HasLenMoreThan ¶ added in v0.2.0
func (*C) Implements ¶ added in v0.2.0
func (*C) LessOrEqual ¶ added in v0.2.2
func (*C) LessOrEqualThan ¶ added in v0.2.0
func (*C) Log ¶
Log logs some information into the test error output. The provided arguments are assembled together into a string with fmt.Sprint.
func (*C) Logf ¶
Log logs some information into the test error output. The provided arguments are assembled together into a string with fmt.Sprintf.
func (*C) MkDir ¶
Create a new temporary directory which is automatically removed after the suite finishes running.
func (*C) MoreOrEqualThan ¶ added in v0.2.0
func (*C) NotContains ¶ added in v0.2.0
func (*C) NotDeepEquals ¶ added in v0.2.0
func (*C) NotEqualsFloat32 ¶ added in v0.2.0
func (*C) NotEqualsMore ¶ added in v0.2.0
func (*C) NotMatches ¶ added in v0.2.0
func (*C) Output ¶
Output enables *C to be used as a logger in functions that require only the minimum interface of *log.Logger.
func (*C) PanicMatches ¶ added in v0.2.0
func (*C) ResetTimer ¶
func (c *C) ResetTimer()
ResetTimer sets the elapsed benchmark time to zero. It does not affect whether the timer is running.
func (*C) SetBytes ¶
SetBytes informs the number of bytes that the benchmark processes on each iteration. If this is called in a benchmark it will also report MB/s.
func (*C) Skip ¶
Skip skips the running test for the provided reason. If run from within SetUpTest, the individual test being set up will be skipped, and if run from within SetUpSuite, the whole suite is skipped.
func (*C) StartTimer ¶
func (c *C) StartTimer()
StartTimer starts timing a test. This function is called automatically before a benchmark starts, but it can also used to resume timing after a call to StopTimer.
func (*C) StopTimer ¶
func (c *C) StopTimer()
StopTimer stops timing a test. This can be used to pause the timer while performing complex initialization that you don't want to measure.
func (*C) Succeed ¶
func (c *C) Succeed()
Succeed marks the currently running test as succeeded, undoing any previous failures.
func (*C) SucceedNow ¶
func (c *C) SucceedNow()
SucceedNow marks the currently running test as succeeded, undoing any previous failures, and stops running the test.
type CheckTestResult ¶ added in v0.0.7
type CheckTestResult struct { Succeeded int Failed int Skipped int Panicked int FixturePanicked int ExpectedFailures int Missed int // Not even tried to run, related to a panic in the fixture. RunError error // Houston, we've got a problem. WorkDir string // If KeepWorkDir is true }
func Run ¶
func Run(suite any, runConf *RunConf) *CheckTestResult
Run runs the provided test suite using the provided run configuration.
func RunAll ¶
func RunAll(runConf *RunConf) *CheckTestResult
RunAll runs all test suites registered with the Suite function, using the provided run configuration.
func (*CheckTestResult) Add ¶ added in v0.0.7
func (r *CheckTestResult) Add(other *CheckTestResult)
func (*CheckTestResult) Passed ¶ added in v0.0.7
func (r *CheckTestResult) Passed() bool
func (*CheckTestResult) String ¶ added in v0.0.7
func (r *CheckTestResult) String() string
type Checker ¶
type Checker interface { Info() *CheckerInfo Check(params []any, names []string) (result bool, error string) }
The Checker interface must be provided by checkers used with the Assert and Check verification methods.
var Contains Checker = &contains{ &CheckerInfo{Name: "Contains", Params: []string{"obtained", "expected"}}, }
var DeepEquals Checker = &deepEqualsChecker{ &CheckerInfo{Name: "DeepEquals", Params: []string{"obtained", "expected"}}, }
The DeepEquals checker verifies that the obtained value is deep-equal to the expected value. The check will work correctly even when facing slices, interfaces, and values of different types (which always fail the test).
For example:
c.Assert(value, DeepEquals, 42) c.Assert(array, DeepEquals, []string{"hi", "there"})
var Equals Checker = &equalsChecker{ &CheckerInfo{Name: "Equals", Params: []string{"obtained", "expected"}}, }
The Equals checker verifies that the obtained value is equal to the expected value, according to usual Go semantics for ==.
For example:
c.Assert(value, Equals, 42)
var EqualsFloat32 Checker = &equalsFloat32{ &CheckerInfo{Name: "EqualsFloat32", Params: []string{"get", "should be equal"}}, }
var EqualsMore Checker = &equalsMore{ &CheckerInfo{Name: "EqualsMore", Params: []string{"get", "should be equal"}}, }
var ErrorIs Checker = &errorIs{ &CheckerInfo{Name: "ErrorIs", Params: []string{"obtained", "expected"}}, }
ErrorIs asserts that at least one of the errors in err's chain matches target. This is a wrapper for errors.Is.
var ErrorMatches Checker = errorMatchesChecker{ &CheckerInfo{Name: "ErrorMatches", Params: []string{"value", "regex"}}, }
The ErrorMatches checker verifies that the error value is non nil and matches the regular expression provided.
For example:
c.Assert(err, ErrorMatches, "perm.*denied")
var FitsTypeOf Checker = &fitsTypeChecker{ &CheckerInfo{Name: "FitsTypeOf", Params: []string{"obtained", "sample"}}, }
The FitsTypeOf checker verifies that the obtained value is assignable to a variable with the same type as the provided sample value.
For example:
c.Assert(value, FitsTypeOf, int64(0)) c.Assert(value, FitsTypeOf, os.Error(nil))
var HasLen Checker = &hasLenChecker{ &CheckerInfo{Name: "HasLen", Params: []string{"obtained", "n"}}, }
The HasLen checker verifies that the obtained value has the provided length. In many cases this is superior to using Equals in conjunction with the len function because in case the check fails the value itself will be printed, instead of its length, providing more details for figuring the problem.
For example:
c.Assert(list, HasLen, 5)
var HasLenLessThan Checker = &hasLenLessThan{ &CheckerInfo{Name: "HasLenLessThan", Params: []string{"obtained", "n"}}, }
The hasLenLessThan checker verifies that the obtained value has the length less than provided one. In many cases this is superior to using Equals in conjunction with the len() function because in case the check fails the value itself will be printed, instead of its length, providing more details for figuring the problem. Also it converts last parameter from any int* to int, that may be useful for use configured/calculated values.
For example:
c.Assert(list, HasLenLessThan, 5)
var HasLenMoreThan Checker = &hasLenMoreThan{ &CheckerInfo{Name: "HasLenMoreThan", Params: []string{"obtained", "n"}}, }
The HasLenMoreThan checker verifies that the obtained value has the length more than provided one. In many cases this is superior to using Equals in conjunction with the len() function because in case the check fails the value itself will be printed, instead of its length, providing more details for figuring the problem. Also it converts last parameter from any int* to int, that may be useful for use configured/calculated values.
For example:
c.Assert(list, HasLenMoreThan, 5)
var Implements Checker = &implementsChecker{ &CheckerInfo{Name: "Implements", Params: []string{"obtained", "ifaceptr"}}, }
The Implements checker verifies that the obtained value implements the interface specified via a pointer to an interface variable.
For example:
var e os.Error c.Assert(err, Implements, &e)
var IsEmpty Checker = &isEmpty{ &CheckerInfo{Name: "IsEmpty", Params: []string{"value"}}, }
IsEmpty asserts that the specified object is empty. I.e. nil, "", false, 0 or either a slice or a channel with len == 0.
assert.Empty(t, obj)
see github.com/stretchr/testify for details
var IsFalse Checker = &isFalseChecker{ &CheckerInfo{Name: "IsFalse", Params: []string{"value"}}, }
The IsFalse checker tests whether the obtained value is false. See https://github.com/iostrovok/go-convert/blob/master/bool.go for details about bool values.
Example:
c.Assert(err, isFalse)
var IsNil Checker = &isNilChecker{ &CheckerInfo{Name: "IsNil", Params: []string{"value"}}, }
The IsNil checker tests whether the obtained value is nil.
For example:
c.Assert(err, IsNil)
var IsTrue Checker = &isTrueChecker{ &CheckerInfo{Name: "IsTrue", Params: []string{"value"}}, }
The IsTrue checker tests whether the obtained value is true. See https://github.com/iostrovok/go-convert/blob/master/bool.go for details about bool values.
Example:
c.Assert(err, IsTrue)
var LessOrEqualThan Checker = &lessOrEqualThan{ &CheckerInfo{Name: "MoreOrEqualThan", Params: []string{"get", "should be more or equal than"}}, }
The LessOrEqualThan checker tests whether the obtained value is less than value.
For example:
c.Assert(v1, LessOrEqualThan, 23) -> v1 <= v2 c.Assert(v1, LessOrEqualThan, "my string") -> v1 <= v2 defaults conversion for checking: Int, Int8, Int16, Int32, Int64 => Int64 Uint, Uint8, Uint16, Uint32, Uint64 => Uint64 float32 => float32 []byte, string => string float64 => float64
var LessThan Checker = &lessThan{ &CheckerInfo{Name: "LessThan", Params: []string{"get", "should be less"}}, }
var Matches Checker = &matchesChecker{ &CheckerInfo{Name: "Matches", Params: []string{"value", "regex"}}, }
The Matches checker verifies that the string provided as the obtained value (or the string resulting from obtained.String()) matches the regular expression provided.
For example:
c.Assert(err, Matches, "perm.*denied")
var MoreOrEqualThan Checker = &moreOrEqualThan{ &CheckerInfo{Name: "MoreOrEqualThan", Params: []string{"get", "should be more or equal than"}}, }
var MoreThan Checker = &moreThan{ &CheckerInfo{Name: "MoreThan", Params: []string{"get", "should be more"}}, }
var NotContains Checker = ¬Contains{ &CheckerInfo{Name: "NotContains", Params: []string{"obtained", "expected"}}, }
The NotContains is the opposite the Contains checker
var NotEmpty Checker = ¬Empty{ &CheckerInfo{Name: "NotEmpty", Params: []string{"value"}}, }
NotEmpty asserts that the specified object is NOT empty. I.e. not nil, "", false, 0 or either a slice or a channel with len == 0.
if NotEmpty(t, obj) { Equal(t, "two", obj[1]) }
see github.com/stretchr/testify for details
var NotEquals Checker = ¬EqualsChecker{ &CheckerInfo{Name: "NotEquals", Params: []string{"obtained", "expected"}}, }
The NotEquals checker is the opposite of the Equals checker.
var NotNil Checker = ¬NilChecker{ &CheckerInfo{Name: "NotNil", Params: []string{"value"}}, }
The NotNil checker verifies that the obtained value is not nil.
For example:
c.Assert(iface, NotNil)
This is an alias for Not(IsNil), made available since it's a fairly common check.
var PanicMatches Checker = &panicMatchesChecker{ &CheckerInfo{Name: "PanicMatches", Params: []string{"function", "expected"}}, }
The PanicMatches checker verifies that calling the provided zero-argument function will cause a panic with an error value matching the regular expression provided.
For example:
c.Assert(func() { f(1, 2) }, PanicMatches, `open.*: no such file or directory`).
var Panics Checker = &panicsChecker{ &CheckerInfo{Name: "Panics", Params: []string{"function", "expected"}}, }
The Panics checker verifies that calling the provided zero-argument function will cause a panic which is deep-equal to the provided value.
For example:
c.Assert(func() { f(1, 2) }, Panics, &SomeErrorType{"BOOM"}).
type CheckerInfo ¶
See the Checker interface.
func (*CheckerInfo) Info ¶
func (info *CheckerInfo) Info() *CheckerInfo
type CommentInterface ¶
type CommentInterface interface {
CheckCommentString() string
}
CommentInterface must be implemented by types that attach extra information to failed checks. See the Commentf function for details.
func Commentf ¶
func Commentf(format string, args ...any) CommentInterface
Commentf returns an infomational value to use with Assert or Check calls. If the checker test fails, the provided arguments will be passed to fmt.Sprintf, and will be presented next to the logged failure.
For example:
c.Assert(v, Equals, 42, Commentf("Iteration #%d failed.", i))
Note that if the comment is constant, a better option is to simply use a normal comment right above or next to the line, as it will also get printed with any errors:
c.Assert(l, Equals, 8192) // Ensure buffer size is correct (bug #123)
Source Files
¶
Directories
¶
Path | Synopsis |
---|---|
Package pretty provides pretty-printing for Go values.
|
Package pretty provides pretty-printing for Go values. |
Package text provides rudimentary functions for manipulating text in paragraphs.
|
Package text provides rudimentary functions for manipulating text in paragraphs. |
colwriter
Package colwriter provides a write filter that formats input lines in multiple columns.
|
Package colwriter provides a write filter that formats input lines in multiple columns. |