go-table-driven-tests

Standardizes Go unit tests using table-driven patterns with gomock and testify assertions.

Updated May 7, 2026
One-click install
npx skills add https://github.com/PremModhaOfficial/NFR-pipeline --skill go-table-driven-tests-premmodhaofficial
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: go-table-driven-tests
Source: https://github.com/PremModhaOfficial/NFR-pipeline/tree/main/skills/go-table-driven-tests
Command: npx skills add https://github.com/PremModhaOfficial/NFR-pipeline --skill go-table-driven-tests-premmodhaofficial

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Writing Go unit tests often leads to inconsistent structure, missing error-code assertions, and unsafe parallelization across a codebase. This Skill enforces a single table-driven test pattern for handlers, services, and repositories so every test follows the same struct shape, naming, mock setup, and assertion style. ## Core Features & Use Cases - Table-Driven Test Structure: Defines the standard test struct with name, inputs, setupMock, wantErr, and wantCode fields, plus the t.Run subtest loop pattern. - gomock Per-Test Setup: Creates a fresh gomock.Controller per subtest and wires mock expectations through a setupMock closure to avoid data races. - Assertion and Naming Conventions: Covers require vs assert usage, ErrorAs/ErrorIs error-code checks, t.Cleanup teardown, safe t.Parallel usage, and the Test<Function><Scenario><Expected> naming format. - Use Case: When adding a CreateUser service method, generate five table-driven cases (happy path, validation error, duplicate email conflict, repository failure) with per-case gomock expectations and AppError code assertions. ## Quick Start Ask the AI to write table-driven unit tests with gomock mocks and testify assertions for a specific Go service or handler function.

Frequently Asked Questions about go-table-driven-tests

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I write table-driven tests in Go?▼

Define a slice of anonymous structs with name, input, setupMock, wantErr, and wantCode fields, then loop with t.Run(tc.name, ...) executing the function under test per case. Use require for fatal assertions and assert for value comparisons.

How to use gomock with table-driven tests?▼

Create a fresh gomock.Controller inside each subtest, then set expectations through a setupMock closure field on each test case. This prevents data races and ensures every case, including happy paths, verifies its expected mock calls.

When should I use require vs assert in Go tests?▼

Use require when a failed check should stop the test immediately, such as require.Error or require.NoError before dereferencing results. Use assert for value comparisons like assert.Equal where the test can safely continue after a failure.

Can I use t.Parallel with gomock subtests?▼

Yes, but only when test cases share no mutable state such as databases or package-level variables. Create a new gomock.Controller per parallel subtest, since sharing one controller across parallel tests causes data races.

Why is checking wantErr alone not enough in Go tests?▼

Knowing only that an error occurred does not verify correct error classification. Use require.ErrorAs to unwrap the error into an AppError and assert its specific Code, or use assert.ErrorIs for sentinel errors.