Skip to content

Instantly share code, notes, and snippets.

@yakuter
Created December 5, 2024 06:23
Show Gist options
  • Save yakuter/cf920900ff307bb53f4a28b868b0252e to your computer and use it in GitHub Desktop.
Save yakuter/cf920900ff307bb53f4a28b868b0252e to your computer and use it in GitHub Desktop.
Testing Exit, Fatal, and Panic in Golang 3/3
package main
import (
"testing"
)
func TestPanic(t *testing.T) {
defer func() {
r := recover()
if r == nil {
t.Errorf("Expected panic, but none occurred")
} else if r != "something went wrong" {
t.Errorf("Unexpected panic message: %v", r)
}
}()
// Function that triggers panic
causePanic()
}
func causePanic() {
panic("something went wrong")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment