Skip to content

Testing Exercises

Build your testing skills with exercises covering table-driven tests, mocking, and HTTP handler testing.

ExerciseDifficultyTimeDescription
Table-Driven TestsEasy15 minWrite comprehensive table-driven tests
Mocking with InterfacesMedium20 minCreate mocks for testing dependencies
HTTP Handler TestingMedium25 minTest HTTP handlers with httptest
  • Table-driven tests: Organizing test cases efficiently
  • Subtests: Using t.Run() for organized test output
  • Mocking: Creating test doubles using interfaces
  • httptest: Testing HTTP handlers without a real server
  • Test coverage: Measuring and improving test coverage

Before starting these exercises, you should be comfortable with:

  • Go interfaces
  • Basic testing with go test
  • Chapter 10: Testing Strategies
  1. Use descriptive test case names that explain what’s being tested
  2. Test edge cases and error conditions, not just happy paths
  3. Keep tests independent - each test should set up its own state