Skip to content
Back to skills

Golang Testing

ASecurity

Unit test the service by depending on an interface, not a concrete DB client. Your service should receive a repository or store interface: ```go type UserStore interface { GetByID(ctx context.Context, id string) (*User, error) } type Service struct { store UserStore } func NewService(store UserStore) *Service { return &Service{store: store} } ``` Then in tests, replace the real DB with a mock or fake implementation: ```go type mockUserStore struct { user *User err error } func (m *mockUserSt...

  • 584 stars
  • 0 votes
  • 0 copies
  • 2 views
  • Added September 5, 2026
developmentgotestingdatabase

Works with

  • cli

Security analysis

A100/100

Pro scans all 11 files and shows the line behind each finding

Scanned September 5, 2026

npx -y skills add HoangNguyen0403/agent-skills-standard --skill golang-testing --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Golang Testing?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Golang Testing
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/hoangnguyen0403-golang-testing-3c2aa5b1/badge)](https://www.skillsdirectory.com/skills/hoangnguyen0403-golang-testing-3c2aa5b1)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
Unit test the service by depending on an interface, not a concrete DB client.

Your service should receive a repository or store interface:

```go
type UserStore interface {
    GetByID(ctx context.Context, id string) (*User, error)
}

type Service struct {
    store UserStore
}

func NewService(store UserStore) *Service {
    return &Service{store: store}
}
```

Then in tests, replace the real DB with a mock or fake implementation:

```go
type mockUserStore struct {
    user *User
    err  error
}

func (m *mockUserStore) GetByID(ctx context.Context, id string) (*User, error) {
    return m.user, m.err
}
```

Use table-driven tests so you can cover success and failure paths cleanly:

```go
func TestService_GetUser(t *testing.T) {
    t.Parallel()

    tests := []struct {
        name    string
        store   UserStore
        wantErr bool
    }{
        {
            name:    "returns user",
            store:   &mockUserStore{user: &User{ID: "1"}},
            wantErr: false,
        },
        {
            name:    "propagates store error",
            store:   &mockUserStore{err: errors.New("db failed")},
            wantErr: true,
        },
    }

    for _, tt := range tests {
        tt := tt
        t.Run(tt.name, func(t *testing.T) {
            t.Parallel()

            svc := NewService(tt.store)
            _, err := svc.GetUser(context.Background(), "1")

            if (err != nil) != tt.wantErr {
                t.Fatalf("got err=%v, wantErr=%v", err, tt.wantErr)
            }
        })
    }
}
```

This keeps the unit test fast and deterministic because you are testing service logic, not database integration.

If you want to test the real DB wiring, keep that as a separate integration test.

Files in this skill

  • eval-1.baseline.md1.3 KB
  • eval-1.with-skill.md1.7 KB
  • eval-2.baseline.md1.9 KB
  • eval-2.with-skill.md1.7 KB
  • eval-3.baseline.md640 B
  • eval-3.with-skill.md1 KB
  • trigger-1.md152 B
  • trigger-2.md149 B
  • trigger-3.md114 B
  • trigger-4.md140 B

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in development →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.