Udemy
Turn what you know into an opportunity and reach millions around the world.
Learn More
Your cart is empty.
Keep shopping
Go Testing for Beginners: Master Skills with 30 Exercises
2 students

Go Testing for Beginners: Master Skills with 30 Exercises

Master go test, table-driven tests, mocks, and httptest through hands-on practice
Created byM Sakai
Last updated 10/2026
English

What you'll learn

  • You can write basic test functions independently using the standard testing package in Go.
  • You can manage multiple test cases efficiently by implementing the table-driven testing pattern.
  • You can design testable code by using interfaces to swap external dependencies with stubs.
  • You can verify HTTP handler status codes and response bodies using the httptest package.
  • You can measure test coverage with go test -cover to identify and fill gaps in your testing.

Course content

7 sections • 64 lectures • 2h 11m total length
  • Course Overview3:53
  • Course Roadmap3:23
  • Exercise materials and Google Cloud Shell setup3:00
  • Google Cloud Shell documentation and usage limits0:50

Requirements

  • A basic understanding of Go functions, structs, and interfaces.
  • Basic experience using standard commands like go build and go run.

Description

This course contains the use of artificial intelligence.

This is a course for those who say, "I know Go syntax, but I don't know how to write tests."

When you start learning Go, basics like functions, structs, and interfaces come naturally. However, when it’s time to write tests, many people freeze, wondering, "Where do I start?" or "Am I doing this right?" Testing follows specific patterns; once you master these patterns in order, anyone can write them.

In this course, you will systematically learn how to write test code using Go's standard testing package through 30 hands-on exercises. Rather than just watching videos, you will solve problems to ensure that what you understand mentally translates naturally into code.

What You Will Learn

  • Section 1: Basic Test Structure

    Cover the essentials: naming rules for test functions/files, the difference between t.Error and t.Fatal, and how to test functions that return errors.

  • Section 2: Table-Driven Tests

    Learn the widely-used Go pattern for managing multiple test cases in slices. Use t,Run for subtests to quickly identify failures, and learn how to handle boundary values and compare slices or maps.

  • Section 3: Test Organization

    Master techniques for robust testing: setup/teardown with TestMain, resource cleanup with t.Cleanup, and handling temporary files or environment variables with t.TempDir and t.Setenv. We also cover parallel execution with t.Parallel.

  • Section 4: Interfaces & Dependency Injection

    Hard-to-test code often stems from external dependencies (time, files, APIs). Learn to use interfaces for abstraction and swap dependencies with manual stubs. You'll also explore patterns using io.Reader and io.Writer.

  • Section 5: HTTP Testing

    Learn how to test Web applications using httptest.ResponseRecorder for handlers, httptest.NewServer for mocking external APIs, and methods for validating JSON responses.

  • Section 6: Comprehensive Exercise

    The final challenge combines everything. You will implement tests for a simple REST API using table-driven tests, httptest, and stubs, while checking coverage with go test -cover.

Target Audience

  • Those who understand Go syntax but have rarely written tests.

  • Those who can write tests but lack confidence in their approach.

  • Those familiar with terms like "table-driven" or "mocks" but can't yet implement them.

  • Those who want to start adding tests to their team's codebase.

Prerequisites

  • A basic understanding of Go functions, structs, and interfaces.

  • Experience using basic commands like go build and go run.

Practice in Google Cloud Shell: upload the provided ZIP and run the included setup script. No local Go installation is required. A Google account is required; free quotas are 50 hours/week and 5GB. Accounts under 18 or with unknown age cannot use Cloud Shell. A local Go alternative is included. When videos show CodeSandbox, follow the updated startup lecture. Grading requires no API keys or paid Cloud resources.

Who this course is for:

  • Those who understand Go's basic syntax but have little to no experience writing actual tests.
  • Those who can write tests but lack confidence in whether they are following best practices.
  • Those familiar with terms like "table-driven tests" or "mocks" but struggle to implement them.
  • Those who want to start contributing high-quality tests to their team's codebase.