Mockgo is a tool that automatically generates mocks for unit testing based on your types.
With the generated mock types, you can record and check method calls. Mocks can inspect the order and arguments of the called methods, and specify the values that methods return. These features support unit testing.
日本語訳/Japanese translation
mockgo はあなたの型に単体テスト用のモックを自動生成するツールです。
生成されたモック型では、メソッドの呼び出しを記録・検査できます。モックは呼び出 されるメソッドの順序や引数を検査でき、メソッドが返す値を指定できます。これらに より単体テストを支援します。
How to install or update.
$ go install github.com/koron/mockgo@latestGenerate mocks for your types: TargetType1 and TargetType2
$ cd ~/go/src/github.com/your/project/package
$ mockgo -pacakge github.com/thirdparity/libarary TargetType1 TargetType2This generate mocks for TargetType1 and TargetType2 in
"github.com/thirdparty/library" package to current directory
(~/go/src/github.com/your/project/package).
$ mockgo {OPTIONS} -package {package name or relative path} [target classes...]-
-fortest- generate mock for plain test, without+mocktag) -
-mocksuffix- addMocksuffix to generated mock types -
-noformat- write mock without formatting (goimports equivalent) -
-revision {num}- mock revision 1~3. 3 is recommended, but default is 1 for compatibility. See mock revision for details. -
-noformat- suppress goimports on generating mock code. -
-output {dir}- specify output directory (default., current directory) -
-package {path or dir}- mandatory, packages for which there are types that generate mocks.When this starts with
./or../, you can use relative path for this. -
-verbose- show verbose/debug messages to stderr
Where [target classes...] accepts two forms of name to specify type.
-
OriginalTypename- Mock type name will be same withOriginalTypenameWhen
-mocksuffixgiven,OriginalTypenameMockis used for mock type. -
OriginalTypename:MockTypename- Specify both original and mock type names-mocksuffixis ignored.
There are three revisions of mock.
-
1 - Very simple and redundant. not recommended.
This don't require any packages.
- GOOD: record call parameters.
- GOOD: specify return parameters.
- BAD: manual check calling parameters.
- BAD: no checks order of methods call.
-
2 - Work with expected call sequence.
This uses a runtime
"github.com/koron/mockgo/mockrt".- GOOD: support all GOOD items in revision 1.
- GOOD: auto check calling parameters.
- GOOD: auto check order of methods call.
- BAD: easily made mistakes when constructing function call sequence.
-
3 - Expected call sequnce with fault-tolerance. (recommended)
This uses a runtime
"github.com/koron/mockgo/mockrt3".- GOOD: support all GOOD items in revision 2.
- GOOD: fault-tolerance on constructing function call sequence.
Usually, when using types provided by another package, you use them as they are. However, with a type aliased mock, you first create an alias for that type in your own package.
//go:build !mock
type Foo = foo.FooThis type alias will be replaced with the mock type only when building with a
specific build tag mock.
//go:build mock
type Foo = FooMockWhen running tests, you specify the tag to switch to the mock.
$ go test -tags mockThis allows you to use the mock without going through an interface. This is called a type aliased mock.
Mockgo is a command that automatically generates a mock, either FooMock or Foo depending on whether it is a mock or not, from the source code of the aforementioned foo.Foo type.
(Original idea from my post in Japanese)
- Create
mockrt3.Qwithmockrt3.NewQ(*testing.T, ...) AddCallto add calles ([]mockrt3.C). A call is consist from parameterPand return valuesR. You can add calls withNewQalso.- Create a mock with
mockrt3.Q - Run test target code with a mock.
- A mock will record failures with
testing.Twhen unexpected calls made by target code.
(TODO: Add example codes)
for mocking interface types, -fortest and -mocksuffix will work well.
$ mockgo -package ./ -outdir . -revision 3 -fortest -mocksuffix Interface1 Interface2This generates mock types Interface1Mock and Interface2Mock without build
tag. Interface1Mock is a mock for Interface1, and Interface2Mock is for
Interface2.
for mocking struct types, no need special options.
$ mockgo -package ../pkgA -outdir . -revision 3 Component1 Component2This generates mock types Component1 and Component2 with mock build tag.
Component1 is a mock for pkgA.Component1, and Component2 is for
pkgA.Component2.