gomic
cli tool to generate golang's mock for test.
Features
- Manage mocks with a configuration file, so it is easy to update mocks when interfaces are updated
- Generated mock is simple and generic.
EXPECT
and RETURN
style isn't supported. You can set mock function freely.
- Provide simple fake implementation which only returns default values
Install
gomic is written with Golang and binary is distributed at release page, so installation is easy and no dependency is needed.
If you want to build yourself, run the following command.
$ go get -u github.com/suzuki-shunsuke/gomic/cmd/gomic
Getting Started
- Write an interface.
type (
OS interface {
Getwd() (string, error)
Mkdir(name string, perm os.FileMode) error
}
)
- Generate the configuration file.
$ gomic init
- Edit the configuration file
---
items:
- src:
file: os.go
interface: OS
name: OSMock
dest:
package: examples
file: os_mock.go
- src:
package: io
interface: ReadCloser
name: ReadCloserMock
dest:
package: examples
file: readcloser_mock.go
- Generate mocks.
$ gomic gen
Note that gen
command overwrites the existing file without confirmation.
Configuration
---
# file path must be absolute or relative to the configuration file path.
default:
# vendor_dir is path of the parent directory of `vendor`.
# vendor_dir should be absolute path or relative to configuration file's parent directory.
# By default vendor_dir is configuration file's parent directory.
# vendor_dir: ""
items:
- src:
# package or file or dir are required
# package is a source package path.
# file is a source file path.
# dir is a source directory path.
package: github.com/suzuki-shunsuke/gomic/examples
# file: examples/example.go
# source interface name. This is required.
interface: Hello
# generated mock name
name: HelloMock
# vendor_dir: ""
dest:
# generated file's package name
# If package is not set, gomic tries to get package name with file's parent directory path.
package: example
# output file path
# file is required.
# Currently it is not supported to output multiple mocks in the same file.
# The parent directory must exist.
file: example/example_mock.go
Note
gen
command overwrites the existing file without confirmation
- Before run the
gen
command, packages which the interface depends on should be installed at GOPATH or vendor directory. gomic supports vendor directory.
- Currently it is not supported to output multiple mocks in the same file
- Generated code is not formatted. So we recommend to format them by
gofmt
.
Examples
See examples .
Other Mocking Libraries
Change Log
Please see Releases.
License
MIT