Created
December 5, 2024 06:23
-
-
Save yakuter/cf920900ff307bb53f4a28b868b0252e to your computer and use it in GitHub Desktop.
Testing Exit, Fatal, and Panic in Golang 3/3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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