func TestMyFunction(t *testing.T) { // ... test setup code ... // Check that the function returns the expected result result := myFunction() expected := "foo" if result != expected { t.Logf("Unexpected result - got '%s', expected '%s'", result, expected) t.Fail() } // ... more test code ... }
func TestMyFunction(t *testing.T) { // ... test setup code ... // Check that the function doesn't panic defer func() { if r := recover(); r != nil { t.Logf("Function panicked with error: %v", r) t.Fail() } }() myFunction() // ... more test code ... }In this example, the `Logf` function is used to log a message if the tested function panics. The `%v` format specifier is used to insert the panic message into the log. The Go testing package provides the `testing` library that includes the `Logf` function.