Skip to content
Learn Netverks

Lesson

Step 25/36 69% through track

testing-go

Testing in Go

Last reviewed May 28, 2026 Content v20260528
Track mode
server_compiled
Means
Compiled runner
Reading
~1 min
Level
intermediate

This lesson

This lesson teaches Testing in Go: the syntax, patterns, and safety habits you need before advancing in Go.

Teams still ship Testing in Go in Go codebases—skipping it leaves gaps in debugging and code reviews.

You will apply Testing in Go in contexts like: Kubernetes ecosystem tools, cloud APIs, and CLI utilities.

Write Go in main.go with package main and func main(), click Run on server—the dev runner runs go run main.go; use fmt.Println for output (requires Go toolchain; LEARNING_RUNNER_ENABLED=true).

When you can explain the previous lesson's ideas without copying starter code.

Tests live in *_test.go files with func TestXxx(t *testing.T). Run with go test—built into the toolchain, unlike adding pytest or JUnit separately.

Table-driven tests

tests := []struct{ in, want int }{
    {2, 4}, {3, 9},
}
for _, tt := range tests {
    if got := sq(tt.in); got != tt.want { t.Fatalf(...) }
}

Subtests use t.Run for clearer failure output. Benchmarks use func BenchmarkXxx(b *testing.B).

Playground note

This lesson's editor runs main only—write full test files locally with go test ./... in CI.

Important interview questions and answers

  1. Q: Test file naming?
    A: _test.go suffix in same package (or _test external package for black-box tests).
  2. Q: t.Fatal vs t.Error?
    A: Fatal stops the test immediately; Error marks failure but continues.

Self-check

  1. What command runs tests?
  2. Why table-driven tests?

Tip: Table-driven tests with t.Run subtests scale well—name cases clearly for CI failure output.

Interview prep

Test file naming?

*_test.go in same package or external test package.

Table-driven tests?

Slice of cases iterated in Test—clear failures and easy to extend.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • Table-driven test?
  • t.Helper()?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump