Mock

23 projects

Mocking in Go is built around interfaces: you depend on a small interface, then substitute a fake in tests. Mock libraries generate those fakes or provide ready-made ones for common dependencies such as databases and HTTP services. They split into code generators for your own interfaces, mocks for specific drivers and protocols, and record-and-replay tools that capture real traffic.

For generating mocks, mockery produces mocks that use testify's mock package, and gomock, now maintained by Uber after the original golang/mock was archived, generates mocks with expectation-based checking. matryer/moq generates simple structs with function fields, which many find easier to read, and counterfeiter and minimock are other generators. For databases, DATA-DOG/go-sqlmock mocks database/sql, pgxmock mocks pgx, and go-txdb runs each test in a rolled-back transaction against a real database. For HTTP, jarcoal/httpmock intercepts requests on the client side, and hoverfly and govcr record and replay real interactions.

Before generating mocks, consider whether a hand-written fake or a real dependency is simpler. net/http/httptest starts a real test server in a few lines, and testcontainers can run a real database. Use mocks for slow, nondeterministic or external boundaries, and prefer moq-style fakes when expectation-heavy mocks make tests brittle. Keep interfaces small and defined where they are used, which makes any of these approaches easier.

mockeryby vektra

Tool to generate Go interfaces.

github.com/vektra/mockery/v3Go 1.26+ 20 dependenciesv3.9.0updated today

7.2k

go-sqlmockby DATA-DOG

Mock SQL driver for testing database interactions.

github.com/DATA-DOG/go-sqlmockGo 1.15+ 1 dependencyv1.5.2updated 3mo ago

6.6k

gomockby uber-go

Mocking framework for the Go programming language.

go.uber.org/mockGo 1.25+ 3 dependenciesv0.6.0updated 2mo ago

3.4k

hoverflyby SpectoLabs

HTTP(S) proxy for recording and simulating REST/SOAP APIs with extensible middleware and easy-to-use CLI.

github.com/SpectoLabs/hoverflyGo 1.27+ 41 dependenciesv1.12.17updated 1d ago

2.5k

moqby matryer

Utility that generates a struct from any interface. The struct can be used in test code as a mock of the interface.

github.com/matryer/moqGo 1.26+ 2 dependenciesv0.8.1updated 19d ago

2.2k

httpmockby jarcoal

Easy mocking of HTTP responses from external resources.

github.com/jarcoal/httpmockGo 1.18+ 1 dependencyv1.4.2updated 27d ago

2.1k

counterfeiterby maxbrunsfeld

Tool for generating self-contained mock objects.

github.com/maxbrunsfeld/counterfeiter/v6Go 1.26+ 5 dependenciesv6.14.0updated 19d ago

1.1k

minimockby gojuno

Mock generator for Go interfaces.

github.com/gojuno/minimock/v3Go 1.23+ 9 dependenciesv3.4.7updated 13d ago

752

go-txdbby DATA-DOG

Single transaction based database driver mainly for testing purposes.

github.com/DATA-DOG/go-txdbGo 1.21+ No dependencies v0.2.1updated 1y ago

749

pgxmockby pashagolub

A mock library implementing pgx - PostgreSQL Driver and Toolkit.

github.com/pashagolub/pgxmock/v6Go 1.25+ 2 dependenciesv5.2.0updated 28d ago

598

xgoby xhd2015

A general pureposed function mocking library.

github.com/xhd2015/xgoGo 1.18+ No dependencies v1.2.9updated 2mo ago

431

govcrby seborama

HTTP mock for Golang: record and replay HTTP interactions for offline testing.

github.com/seborama/govcr/v17Go 1.25+ 10 dependenciesv17.1.0updated 6mo ago

200

go-localstackby elgohr

Tool for using localstack in AWS testing.

github.com/elgohr/go-localstackGo 1.25+ 33 dependenciesupdated 22d ago

88

timexby cabify

A test-friendly replacement for the native time package.

github.com/cabify/timexGo 1.13+ 1 dependencyv1.1.0updated 6y ago

72

fabricatorby Goldziher

Type-safe factories for generating mock and fake data in Go, inspired by factory_boy and interface-forge.

github.com/Goldziher/fabricator/v2Go 1.26+ 2 dependenciesv2.1.0updated 1d ago

30

mockhttpby tv42

Mock object for Go http.ResponseWriter.

updated 12y ago

23

mooncakeby GuilhermeCaruso

A simple way to generate mocks for multiple purposes.

github.com/GuilhermeCaruso/mooncakeGo 1.18+ 2 dependenciesv0.0.2updated 4y ago

18

mockfsby balinomad

Mock filesystem for Go testing with error injection and latency simulation, built on testing/fstest.MapFS.

github.com/balinomad/go-mockfs/v2Go 1.25+ No dependencies v2.0.0updated 1mo ago

9

gomockby vibridi

CLI tool to generate typed and framework-agnostic interface mocks, with support for generics.

github.com/vibridi/gomock/v3Go 1.26+ 2 dependenciesv3.7.1updated 6mo ago

7

gomockerby zhongjie-cai

A live mocker library for functions and methods in Golang for unit testing with fluent syntax, no code-gen needed.

github.com/zhongjie-cai/gomocker/v2Go 1.25+ 1 dependencyv2.1.3updated 5d ago

1

wsmockby sing198

Expressive, zero-boilerplate WebSocket mock server for testing with fault injection and assertions.

github.com/sing198/wsmockGo 1.22+ 1 dependencyupdated 2mo ago

0
g

genmock

Go mocking system with code generator for building calls of the interface methods.

gitlab.com

m

moxie

Generate mock methods on embedded structs.

lesiw.io

Esc

Type to search every entry. ↑↓ to move, Enter to open.