Back to questionsHow many error lines does
JuniorTheoryCommon
How many error lines does go test -v print for TestSum?
func TestSum(t *testing.T) {
got := 2 + 2
if got != 5 {
t.Fatalf("got %d, want 5", got)
}
if got%2 == 0 {
t.Errorf("got %d is even", got)
}
}1
- Expecting 2:
t.Fatalfstops TestSum on the spot, so thegot%2 == 0check after it never runs. - Expecting 0 because
t.Fatalfends the test: it prints its message first and only then stops the test.
- How would the output change if
t.Fatalfwere replaced witht.Errorf? - When is
t.Fatalfa better choice thant.Errorf?