go-test-gen

Installation
SKILL.md

Go Unit Test Generator

Generate unit tests for Go service and repository layers.

Critical Rules

  • NO mock.Any/mock.Anything - Use explicit arguments
  • NO mock.MatchedBy - Prefer concrete structs
  • ALL mocks require call counts - .Once(), .Twice(), etc.
  • Use wantErr error - Not wantErr bool
  • Test naming: TestPackageName_FunctionName with cases like "Success_with_valid_input"
  • NO handler/API tests - Service and repository layers only

Workflow

  1. Identify layer: service or repository
  2. Read function to test
  3. For service tests: See references/service-tests.md
  4. For repository tests: See references/repository-tests.md
  5. Generate test following the template
  6. Verify all mock expectations have call counts

Testing Strategies

  • Unit tests: Test individual functions in isolation with mocks
  • Table-driven tests: Use for multiple input/output scenarios
  • Test coverage: Aim for 80%+ meaningful coverage
  • Mock generation: Use //go:generate mockery --name InterfaceName --output ../mock/package
  • Test structure: Define all mocks at the top of test functions for clarity, do not put mocked repository as function args
  • Database testing: Use sqlmock with DATA-DOG/go-sqlmock for database layer testing with consistent structure
  • Handler layer testing: DO NOT generate unit tests for handler layer (API controllers) - focus testing on service and repository layers only

Quick Reference

Service Layer

  • Mock dependencies at function top
  • Table-driven structure
  • Explicit mock expectations
  • Specific error assertions

Repository Layer

  • Use sqlmock with DATA-DOG/go-sqlmock
  • regexp.QuoteMeta() for queries
  • Verify mock.ExpectationsWereMet()
  • Specific errors with apierror.Get()
Related skills

More from sultanfarizbythen/skills

Installs
9
First Seen
Mar 13, 2026