go-testing-patterns

Standardizes Go unit, integration, and benchmark tests using table-driven patterns, testcontainers, and gomock.

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

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Writing consistent, reliable tests across Go microservices is hard: teams duplicate NATS setup helpers, write flaky async assertions with time.Sleep, and struggle to isolate port interfaces. This Skill provides canonical patterns for table-driven unit tests, testcontainers-based integration tests, embedded NATS messaging tests, and gomock-based port isolation. ## Core Features & Use Cases - Table-Driven Unit Tests: Standard Go test structure with t.Parallel() and subtests for exhaustive case coverage. - Integration Infrastructure: testcontainers-go for PostgreSQL, embedded NATS server with JetStream for messaging tests, and httptest for API Gateway handlers. - Mock Generation & Usage: gomock patterns for isolating port interfaces, plus fixture loading via runtime.Caller and benchmark templates with allocation reporting. - Use Case: When adding a new order service handler, use this Skill to write a JetStream QueueSubscribe test with the embedded NATS server, mock the repository with gomock, and enforce 80% coverage in CI. ## Quick Start Ask the AI to write table-driven unit tests and a testcontainers-based integration test for your Go service's repository and NATS message handlers.

Frequently Asked Questions about go-testing-patterns

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 inputs and expected outputs, then loop with t.Run for each case. Add t.Parallel() at the top of the test and each subtest when there is no shared mutable state.

How to test NATS JetStream handlers in Go?▼

Start an embedded NATS server with JetStream enabled and a temp store directory, create the per-service stream, then register handlers via js.QueueSubscribe with a durable consumer. Publish test messages with js.PublishMsg and assert delivery using require.Eventually.

How do I mock Go interfaces with gomock?▼

Generate mocks with mockgen -source pointing at the port interface file, then create a gomock.Controller in the test and set EXPECT() calls with argument matchers and return values. Inject the mock into the handler under test.

Does testcontainers work for PostgreSQL integration tests in Go?▼

Yes, testcontainers-go runs a PostgreSQL container with a log-based wait strategy, returns a connection string for pgxpool, and terminates via t.Cleanup. Apply migrations after connecting so each test gets an isolated database.

Why are my async Go tests flaky?▼

Flakiness usually comes from using time.Sleep to wait for async operations like NATS message delivery. Replace sleeps with require.Eventually polling at a short interval, and always run tests with the -race flag in CI.

When should I use httptest versus NATS testing in Go microservices?▼

Use httptest only for API Gateway HTTP handlers; domain services receive messages via NATS inbound adapters and should be tested with the embedded NATS server. Domain services do not expose HTTP business endpoints.