TopGit tracks golang/mock on GitHub. The project has 9.4k stars. GoMock is a mocking framework for the Go programming language.
Snapshot summary built from the project's own GitHub metadata — there's no written TopGit review yet. The page will update automatically when a full review is published.
WHY NO REVIEW YET
TopGit writes full reviews for the most-starred, most-requested repositories. This page is a snapshot until then — see the READ ME tab for the original README in full.
Update, June 2023: This repo and tool are no longer maintained.
Please see go.uber.org/mock for a maintained fork instead.
gomock is a mocking framework for the Go programming language. It
integrates well with Go's built-in testing package, but can be used in other
contexts too.
Installation
Once you have installed Go, install the mockgen tool.
Note: If you have not done so already be sure to add $GOPATH/bin to your
PATH.
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:
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:
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 of elements of the form
Repository=MockSensorRepository,Endpoint=MockSensorEndpoint, where
Repository is the interface name and MockSensorRepository is the desired
mock name (mock factory method and mock recorder will be named after the mock).
If one of the interfaces has no custom name specified, then default naming
convention will be used.
-self_package: The full package import path for the generated code. The
purpose of this flag is to prevent import cycles in the generated code by
trying to include its own package. This can happen if the mock's package is
set to one of its inputs (usually the main one) and the output is stdio so
mockgen cannot detect the final output package. Setting this flag will then
tell mockgen which import to exclude.
-copyright_file: Copyright file used to add copyright header to the resulting source code.
-debug_parser: Print out parser results only.
-exec_only: (reflect mode) If set, execute this reflection program.
-prog_only: (reflect mode) Only generate the reflection program; write it to stdout and exit.
-write_package_comment: Writes package documentation comment (godoc) if true. (default true)
For an example of the use of mockgen, see the sample/ directory. In simple
cases, you will need only the -source flag.
Building Mocks
type Foo interface {
Bar(x int) int
}
func SUT(f Foo) {
// ...
}
func TestFoo(t *testing.T) {
ctrl := gomock.NewController(t)
// Assert that Bar() is invoked.
defer ctrl.Finish()
m := NewMockFoo(ctrl)
// Asserts that the first and only call to Bar() is passed 99.
// Anything else will fail.
m.
EXPECT().
Bar(gomock.Eq(99)).
Return(101)
SUT(m)
}
If you are using a Go version of 1.14+, a mockgen version of 1.5.0+, and are
passing a *testing.T into gomock.NewController(t) you no longer need to call
ctrl.Finish() explicitly. It will be called for you automatically from a self
registered Cleanup function.
Building Stubs
type Foo interface {
Bar(x int) int
}
func SUT(f Foo) {
// ...
}
func TestFoo(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
m := NewMockFoo(ctrl)
// Does not make any assertions. Executes the anonymous functions and returns
// its result when Bar is invoked with 99.
m.
EXPECT().
Bar(gomock.Eq(99)).
DoAndReturn(func(_ int) int {
time.Sleep(1*time.Second)
return 101
}).
AnyTimes()
// Does not make any assertions. Returns 103 when Bar is invoked with 101.
m.
EXPECT().
Bar(gomock.Eq(101)).
Return(103).
AnyTimes()
SUT(m)
}
Modifying Failure Messages
When a matcher reports a failure, it prints the received (Got) vs the
expected (Want) value.
Got: [3]
Want: is equal to 2
Expected call at user_test.go:33 doesn't match the argument at index 1.
Got: [0 1 1 2 3]
Want: is equal to 1
Modifying Want
The Want value comes from the matcher's String() method. If the matcher's
default output doesn't meet your needs, then it can be modified as follows:
This modifies the gomock.Eq(15) matcher's output for Want: from is equal to 15 to is equal to fifteen.
Modifying Got
The Got value comes from the object's String() method if it is available.
In some cases the output of an object is difficult to read (e.g., []byte) and
it would be helpful for the test to print it differently. The following
modifies how the Got value is formatted:
No homepage URL was recorded for golang/mock in TopGit's last sync. The README tab above frequently contains screenshots and demo links, or check the repository description on GitHub.
Does golang/mock have any tags?
TopGit's last sync did not record any GitHub topics for golang/mock. GitHub topics appear in the right sidebar of a repository page; that's the authoritative place to check.
How active is development on golang/mock?
The most recent commit recorded on golang/mock was 2.7 years ago, based on the GitHub push timestamp. The repository has 604 forks — one of the better signals of community interest.
How many stars does golang/mock have?
golang/mock has 9.4k GitHub stars — refresh the page for the live number, or check github.com/golang/mock. TopGit mirrors GitHub's count but does not claim minute-by-minute accuracy.
What language is golang/mock written in?
golang/mock is written primarily in Go. GitHub's language field is based on the largest share of bytes in the default branch.
What license does golang/mock use?
golang/mock is released under the Apache-2.0 license. Always verify the LICENSE file directly on GitHub for the authoritative terms — license strings can be edited out of sync with a project's actual stance.
Where do I read more about golang/mock?
This TopGit page is a snapshot — the READ ME tab shows the project's own README content (links stripped, images preserved). The GitHub repository at github.com/golang/mock is the definitive source.
Read full README in the tab above.
Curious whether mock is right for you?
Let ChatGPT, Claude, or Perplexity look into it — click below and see what AI actually says about mock.