Testing Exercises
Testing Exercises
Section titled “Testing Exercises”Build your testing skills with exercises covering table-driven tests, mocking, and HTTP handler testing.
Exercises in This Section
Section titled “Exercises in This Section”| Exercise | Difficulty | Time | Description |
|---|---|---|---|
| Table-Driven Tests | Easy | 15 min | Write comprehensive table-driven tests |
| Mocking with Interfaces | Medium | 20 min | Create mocks for testing dependencies |
| HTTP Handler Testing | Medium | 25 min | Test HTTP handlers with httptest |
Key Concepts Covered
Section titled “Key Concepts Covered”- 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
Prerequisites
Section titled “Prerequisites”Before starting these exercises, you should be comfortable with:
- Go interfaces
- Basic testing with
go test - Chapter 10: Testing Strategies
- Use descriptive test case names that explain what’s being tested
- Test edge cases and error conditions, not just happy paths
- Keep tests independent - each test should set up its own state