prefer-table-driven-tests
Installation
SKILL.md
Summary
Use table-driven tests to verify multiple test cases for a single function. Define a slice of structs containing inputs, expected outputs, and a test name, then iterate over them.
Rationale
- Compactness: Reduces boilerplate code by reusing the setup and assertion logic.
- Readability: Makes it easy to scan all test cases and see the variation in inputs/outputs.
- Extensibility: Adding a new test case is as simple as adding a new line to the slice.
Guidance
- Anonymous Structs: Use an anonymous struct slice
cases := []struct { ... }inside the test function. - Subtests: Use
t.Run(tc.name, func(t *testing.T) { ... })inside the loop to isolate failures. - Comprehensive Cases: Include edge cases, error cases, and happy paths in the table.