Automatically generates mock code for unit tests from a Go interface (see below for examples.) Generic interfaces are supported.
This project is actively maintained. Commits are infrequent because it reached a certain degree of stability and no additional features are needed at this time.
$ go install github.com/vibridi/gomock/v3@latest
Go 1.26.2
Type gomock help for detailed usage tips. Mainly: gomock { help | [options] filename }
In short, it supports the following flags:
-f FILEallows to specify the input file where your interface is declared. If not provided, it's assumed the input file is the first argument after other options.-o FILEif set, tells the program to write the output toFILE. Otherwise it just prints to stdout. You can always capture the output with a pipe. E.g. if you are on MacOS, you could dogomock -f myfile.go | pbcopy-i IDENTIFIERif the input file contains more than one interface declaration, you can use the-iflag to tell the program which one to parse. If not set, the program defaults to the first encountered interface.-xif set, static functions are exported (usually those whose name begins withwithandnew)-uif set, allows to output default functions andWith*functions with unnamed arguments.-dif set, outputs top-levelwithFuncfunction identifiers with the name of the service. This is useful to avoid "function redeclared" errors when mocks of interfaces with identical method names exist in the same package. For example, if the service name isMyServicethenwithFuncDoSomethingbecomeswithFuncMyServiceDoSomething.-pif set, the package name and the service name are merged together, except when the name is explicitly qualified. For example, if the service name isMyServiceand the package name isfoothenNewMockMyServicebecomesNewMockFooMyService.--localif set, doesn't qualify output mock types with the package name. It qualifies them by default. The default behavior is to always output named arguments, as some IDEs reference them in code completion.--structif set, prints the output in struct style, instead of options style (see below for further details).--name NAMEallows to override the interface name used in output types withNAME.--pkgs MAPPING [ --pkgs MAPPING ]maps package names to custom import aliases.MAPPINGmust in the format 'package=alias'. For examplegomock --pkgs foo=foo2changesfoo.Foofrom the source file tofoo2.Foo.--utype MAPPING [ --utype MAPPING ]allows to manually specify the underlying type of a named type. If the--pkgsoption is specified, theMAPPING's keys must be the aliased package name. For examplegomock --pkgs foo=foo2 --utype foo2.Foo=int--help, -hprints a help message.--version, -vprints the version number.
- The option
-qis removed. It's assumed that mocked types are always qualified with their package name. The option--localcan be used instead to opt out (i.e. to not qualify them). - The option
--struct-typehas been renamed to--struct. It has the same effect.
This tool is able to resolve composed interfaces, however all declarations must live
in the same directory or sub-directories relative to the main file. To see this in action, run make example-compose.
You can try out the tool on any of your interfaces.
The _examples folder in this repository contains a few interface types usefult to try out the tool:
$ gomock -f _example/_example.go
The _example.go file declares a type TestInterface interface { ... }.
The default options-style mock code generated by gomock looks like this:
type mockTestInterface struct {
options mockTestInterfaceOptions
}
type mockTestInterfaceOptions struct {
funcGet func() string
funcSet func(v string)
}
var defaultMockTestInterfaceOptions = mockTestInterfaceOptions{
funcGet: func() string {
return ""
},
funcSet: func(string) {
return
},
}
type mockTestInterfaceOption func(*mockTestInterfaceOptions)
func withFuncGet(f func() string) mockTestInterfaceOption {
return func(o *mockTestInterfaceOptions) {
o.funcGet = f
}
}
func withFuncSet(f func(string) ) mockTestInterfaceOption {
return func(o *mockTestInterfaceOptions) {
o.funcSet = f
}
}
func (m *mockTestInterface) Get() string {
return m.options.funcGet()
}
func (m *mockTestInterface) Set(v string) {
return
}
func newMockTestInterface(opt ...mockTestInterfaceOption) TestInterface {
opts := defaultMockTestInterfaceOptions
for _, o := range opt {
o(&opts)
}
return &mockTestInterface{
options: opts,
}
}
That can be used in unit tests as such:
myMock := newMockTestInterface(
withFuncGet(f func() string {
return "test-value"
}),
)
// myMock.Get() returns "test-value"
// inject mock into constructor that expects a TestInterface
obj := NewObject(myMock)
Running gomock with the --struct option generates the mock code in struct style:
$ gomock -f _example/_example.go --struct
This will print:
type mockTestInterface struct {
GetFunc func() string
SetFunc func(v string)
}
func (m *mockTestInterface) Get() string {
if m.GetFunc != nil {
return m.GetFunc()
}
return ""
}
func (m *mockTestInterface) Set(v string) {
if m.SetFunc != nil {
m.SetFunc(v)
}
}
That can be used in unit tests as such:
myMock := mockTestInterface{
GetFunc: func() string {
return "test-value"
},
}
// myMock.Get() returns "test-value"
// inject mock into constructor that expects a TestInterface
obj := NewObject(myMock)
- Gabriele V. - Initial work and maintenance
Currently there are no other contributors
This project is licensed under the MIT License - see the LICENSE.md file for details