Can I invoke a function when assertion fails?
E.g,
assert.True(t, condition) // invoke a function such as printing a map
Update: based on the suggestion, i wrote a small example. But does not seem to work.
assert.go
package main
import _ "fmt"
func compute() bool {
return false
}
assert_test.go
package main
import (
"fmt"
"github.com/stretchr/testify/assert"
"testing"
)
func pMap() {
amap := map[int]string{
1: "hello1",
2: "hello2",
}
for i, _ := range amap {
fmt.Println("map = ", i)
}
}
func TestCompute(t *testing.T) {
assert.True(t, compute(), pMap)
}
$go test
--- FAIL: TestCompute (0.00s)
assert_test.go:20:
Error Trace: assert_test.go:20
Error: Should be true
Test: TestCompute
Messages: 0x631a30
FAIL
exit status 1
According to godoc, assert.True returns a bool, so you could wrap that in a conditional. https://godoc.org/github.com/stretchr/testify/assert#True
passed := assert.True(t, compute()) // X will be a boolean true or false if the test passed.
You can use this to print maps /whatever like so:
if passed {
pMap()
} else {
// test failed
}