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

  1. Anonymous Structs: Use an anonymous struct slice cases := []struct { ... } inside the test function.
  2. Subtests: Use t.Run(tc.name, func(t *testing.T) { ... }) inside the loop to isolate failures.
  3. Comprehensive Cases: Include edge cases, error cases, and happy paths in the table.

Examples

Installs
1
First Seen
Apr 5, 2026
prefer-table-driven-tests — mew-ton/coding-guidelines