typestar

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

  1. The name must start with Test and take *testing.T.
  2. t.Errorf records a failure and continues; t.Fatalf stops.
  3. No assertion library: you write the comparison.

Keywords and builtins used here

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.

Type this snippet

Step 1 of 4 in Writing tests, step 1 of 13 in Testing.

Next →