A Go test in Go
A file ending _test.go, a function taking *testing.T, and t.Errorf.
func Slugify(title string) string {
return strings.ReplaceAll(strings.ToLower(title), " ", "-")
}
func TestSlugify(t *testing.T) {
got := Slugify("Language Basics")
want := "language-basics"
if got != want {
t.Errorf("Slugify() = %q, want %q", got, want)
}
}
func TestSlugifyEmpty(t *testing.T) {
if got := Slugify(""); got != "" {
t.Fatalf("expected empty, got %q", got)
}
}
How it works
- The name must start with Test and take
*testing.T. t.Errorfrecords a failure and continues;t.Fatalfstops.- No assertion library: you write the comparison.
Keywords and builtins used here
funcifreturnstring
The run, in numbers
- Lines
- 17
- Characters to type
- 377
- Tokens
- 93
- Three-star pace
- 100 tpm
At the three-star pace of 100 tokens a minute, this run takes about 56 seconds.
Step 1 of 4 in Writing tests, step 1 of 13 in Testing.