Closed
Description
Go version
n/a
Output of go env
in your module/workspace:
n/a
What did you do?
I visisted https://go.dev/doc/tutorial/add-a-test and I read how to create a test for Go code.
What did you see happen?
I saw it advise test construction using (*testing.T).Fatalf
per the excerpts below:
func TestHelloName(t *testing.T) {
name := "Gladys"
want := regexp.MustCompile(`\b`+name+`\b`)
msg, err := Hello("Gladys")
if !want.MatchString(msg) || err != nil {
t.Fatalf(`Hello("Gladys") = %q, %v, want match for %#q, nil`, msg, err, want)
}
}
func TestHelloEmpty(t *testing.T) {
msg, err := Hello("")
if msg != "" || err == nil {
t.Fatalf(`Hello("") = %q, %v, want "", error`, msg, err)
}
}
What did you expect to see?
Given the design guidance with tests to "keep going", I would expect these tests to be written as below using (*testing.T).Errorf
:
func TestHelloName(t *testing.T) {
name := "Gladys"
want := regexp.MustCompile(`\b`+name+`\b`)
msg, err := Hello("Gladys")
if !want.MatchString(msg) || err != nil {
t.Errorf(`Hello("Gladys") = %q, %v, want match for %#q, nil`, msg, err, want)
}
}
func TestHelloEmpty(t *testing.T) {
msg, err := Hello("")
if msg != "" || err == nil {
t.Errorf(`Hello("") = %q, %v, want "", error`, msg, err)
}
}
It seems like it would be useful to have the training material aligned with community development expectations.