Documentation ¶
Overview ¶
Package catch provides utility functions that work with the standard library's testing capabilities. The functions encapsulate standard tests, and standardise logging for failures. However, package catch does not output anything directly. All messages are formatted using the standard methods on the test object.
As an example, when the following test fails:
func TestFancyCalculation(t *testing.T) { fancyCalculation := 1 + 1 catch.CheckEqual(t, fancyCalculation, 1) }
The log will be:
testing_test.go:285: check failed: fancyCalculation == 1 got : 2 expected : 1
The source files for the test code should be available and unmodified at their original locations when the tests are run. The source code is read and partially parsed to create messages for any failed tests. If the source code is not available, tests can still be run, but the messages for any failed tests will not be complete.
Message Formatting ¶
When a test fails, package catch will create an error message based on the the user's source code. The source code for the helper's caller will be opened, and the expression for the arguments included in the error report.
If the line with error contains a line comment, the line comment will also be included in the error message.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Check ¶
Check does nothing if 'value' is true, but will a report an error if it is false. The error is reported using Errorf, meaning that the error will be logged and the test will be marked as failing.
Example ¶
package main import ( "testing" "gitlab.com/stone.code/catch" ) func main() { // This is setup as a benchmark, but that is just to get a context for the // tests. testing.Benchmark(func(b *testing.B) { // Test setup goes here. // ... fancyResult := 1 + 1 // Make sure that the result is expected catch.Check(b, fancyResult == 2) }) }
Output:
func CheckEqual ¶
func CheckEqual(t TB, got, expected interface{})
CheckEqual does nothing if 'expected' and 'got' are equal, but will a report an error otherwise. The error is reported using Errorf, meaning that the error will be logged and the test will be marked as failing.
Example ¶
package main import ( "testing" "gitlab.com/stone.code/catch" ) func main() { // This is setup as a benchmark, but that is just to get a context for the // tests. testing.Benchmark(func(b *testing.B) { // Test setup goes here. // ... fancyResult := 1 + 1 // Make sure that the result is expected catch.CheckEqual(b, fancyResult, 2) }) }
Output:
func CheckIsNil ¶ added in v1.1.0
func CheckIsNil(t TB, got interface{})
CheckIsNil does nothing if 'got' is a nil chan, func, map, pointer, or slice value, but will a report an error otherwise. The error is reported using Errorf, meaning that the error will be logged and the test will be marked as failing.
This function can only be called with chan, func, map, pointer, or slice values. It will panic otherwise.
Example ¶
package main import ( "testing" "gitlab.com/stone.code/catch" ) func main() { // This is setup as a benchmark, but that is just to get a context for the // tests. testing.Benchmark(func(b *testing.B) { // Test setup goes here. // ... var s []byte // Make sure that the result is expected catch.CheckIsNil(b, s) }) }
Output:
func Require ¶
Require does nothing if 'value' is true, but will a report an error if it is false. The error is reported using Fatalf, meaning that the error will be logged and the test will fail immediately.
Example ¶
package main import ( "os" "testing" "gitlab.com/stone.code/catch" ) func main() { // This is setup as a benchmark, but that is just to get a context for the // tests. testing.Benchmark(func(b *testing.B) { // Open file with test data. Abort the test if the file cannot be // opened. file, err := os.Open("required-test-data.txt") catch.Require(b, err == nil) // No error. Otherwise, test would have been aborted by the previous // call to Require. defer file.Close() // Count the number of lines in the file. fancyResult := 1 + 1 // Make sure that the result is expected. catch.Require(b, fancyResult == 2) }) }
Output:
Example (Fail) ¶
package main import ( "fmt" "testing" "gitlab.com/stone.code/catch" ) func main() { // This is setup as a benchmark, but that is just to get a context for the // tests. testing.Benchmark(func(b *testing.B) { // Perform a terribly complicated calculations. fancyResult := 1 + 2 // Make sure that the result is expected. fmt.Println("Require...") catch.Require(b, fancyResult == 2) fmt.Println("done.") }) }
Output: Require...
func RequireEqual ¶
func RequireEqual(t TB, got, expected interface{})
RequireEqual does nothing if 'expected' and 'got' are equal, but will a report an error otherwise. The error is reported using Fatalf, meaning that the error will be logged and the test will fail immediately.
func RequireIsNil ¶ added in v1.1.0
func RequireIsNil(t TB, got interface{})
RequireIsNil does nothing if 'got' is a nil chan, func, map, pointer, or slice value, but will a report an error otherwise. The error is reported using Fatalf, meaning that the error will be logged and the test will fail immediately.
This function can only be called with chan, func, map, pointer, or slice values. It will panic otherwise.