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
- Simple and flexible API. Complicated API and DSL aren't provided. So learning cost is low and test code is easy to read
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
---
default:
interface_suffix: Mock
items:
- src:
file: os.go
interface: OS
dest:
package: examples
file: os_mock.go
- src:
package: io
interface: ReadCloser
dest:
package: examples
file: readcloser_mock.go
- Generate mocks.
$ gomic gen
Note that gen
command overwrites the existing file without confirmation.
Examples
See examples .
Configuration
---
# file path must be absolute or relative to the configuration file path.
# default is the default settings of each items. item's settings are preferred than default settings.
default:
# vendor_dir: ""
# interface_prefix: Mock
# interface_suffix: Mock
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
# If name is not given, name is "{{interface_prefix}}{{interface}}{{interface_suffix}}".
# If name is given, interface_prefix and interface_suffix are ignored.
# interface_prefix: Mock
# interface_suffix: Mock
# 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: ""
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
.
Other Mocking Libraries
Change Log
Please see Releases.
Contributing
Please see CONTRIBUTING.md .
License
MIT