golang /mock

GoMock is a mocking framework for the Go programming language.

★ 9,316⑂ 601👁 3⑃ 319● 388▤ 13
GoApache-2.0Created 2015-06-12
TA的项目
下载:v1.6.0v1.6.0 ↓1.6.02021-06-11
+0
日增长
9,316
stars
601
forks
3
watchers
319
prs
388
issues
13
releases

README

gomock

Update, June 2023: _This repo and tool are no longer maintained.
Please see go.uber.org/mock for a maintained fork instead._

[![Build Status][ci-badge]][ci-runs] [![Go Reference][reference-badge]][reference]

gomock is a mocking framework for the [Go programming language][golang]. It
integrates well with Go's built-in testing package, but can be used in other
contexts too.

 Installation

Once you have [installed Go][golang-install], install the mockgen tool.

Note: If you have not done so already be sure to add $GOPATH/bin to your
PATH.

To get the latest released version use:

 Go version < 1.16

bash
GO111MODULE=on go get github.com/golang/mock/[email protected]


 Go 1.16+

bash
go install github.com/golang/mock/[email protected]


If you use mockgen in your CI pipeline, it may be more appropriate to fixate
on a specific mockgen version. You should try to keep the library in sync with
the version of mockgen used to generate your mocks.

 Running mockgen

mockgen has two modes of operation: source and reflect.

 Source mode

Source mode generates mock interfaces from a source file.
It is enabled by using the -source flag. Other flags that
may be useful in this mode are -imports and -aux_files.

Example:

bash
mockgen -source=foo.go [other options]


 Reflect mode

Reflect mode generates mock interfaces by building a program
that uses reflection to understand interfaces. It is enabled
by passing two non-flag arguments: an import path, and a
comma-separated list of symbols.

You can use "." to refer to the current path's package.

Example:

bash
mockgen database/sql/driver Conn,Driver

 Convenient for go:generate.
mockgen . Conn,Driver


 Flags

The mockgen command is used to generate source code for a mock
class given a Go source file containing interfaces to be mocked.
It supports the following flags:

- -source: A file containing interfaces to be mocked.

- -destination: A file to which to write the resulting source code. If you
  don't set this, the code is printed to standard output.

- -package: The package to use for the resulting mock class
  source code. If you don't set this, the package name is mock_ concatenated
  with the package of the input file.

- -imports: A list of explicit imports that should be used in the resulting
  source code, specified as a comma-separated list of elements of the form
  foo=bar/baz, where bar/baz is the package being imported and foo is
  the identifier to use for the package in the generated source code.

- -aux_files: A list of additional files that should be consulted to
  resolve e.g. embedded interfaces defined in a different file. This is
  specified as a comma-separated list of elements of the form
  foo=bar/baz.go, where bar/baz.go is the source file and foo is the
  package name of that file used by the -source file.

- -build_flags: (reflect mode only) Flags passed verbatim to go build.

- -mock_names: A list of custom names for generated mocks. This is specified
  as a comma-separated list

Top issues

Latest release

v1.6.0 v1.6.0 2021-06-11
Changelog

317c030 Best effort guesses for output package path (547)
c59ba11 add ARM to support apple M1 chip to releaser (562)
58935d8 add a basic CONTRIBUTING.md (535)
a5582fc add docs on 1.16 install and adding to PATH (534)
0cd3aaf add flags documentation (539)
64b0b80 add notes and error helper for vendor+reflect error (567)
e303461 add type information to error messages (559)
0cdccf5 feat add InAnyOrder matcher (546)
82ce4a7 feat validate Do & DoReturn args (558)
93308c3 fix broken badge (525)
9336b7e fix error message in parse.go (540)
ab03293 fix ill-formatted message with

Snapshot: 2026-11-16T09:23:14Z · Data license Apache-2.0 · JSON