Mockery is a project that creates mock implementations of Golang interfaces. The mocks generated in this project are based off of the github.com/stretchr/testify suite of testing packages.
When you have an interface like this:
type DB interface {
Get(val string) string
}
and a function that takes this interface:
func getFromDB(db DB) string {
return db.Get("ice cream")
}
You can test getFromDB
by either instantiating a testing database, or you can simply create a mock implementation of DB
using mockery. Mockery can autogenerate a mock implementation that allows us to define assertions on how the mock was used, what to return, and other useful tidbits. We can add a //go:generate
directive above our interface:
//go:generate mockery --name DB
type DB interface {
Get(val string) string
}
inpackage: True # (1)!
with-expecter: True # (2)!
testonly: True # (3)!
- Generate our mocks next to the original interface
- Create expecter methods
- Append
_test.go
to the filename so the mock object is not packaged
$ go generate
05 Mar 23 21:49 CST INF Starting mockery dry-run=false version=v2.20.0
05 Mar 23 21:49 CST INF Using config: .mockery.yaml dry-run=false version=v2.20.0
05 Mar 23 21:49 CST INF Walking dry-run=false version=v2.20.0
05 Mar 23 21:49 CST INF Generating mock dry-run=false interface=DB qualified-name=github.com/vektra/mockery/v2/pkg/fixtures/example_project version=v2.20.0
We can then use the mock object in a test:
import (
"testing"
"github.com/stretchr/testify/assert"
)
func Test_getFromDB(t *testing.T) {
mockDB := NewMockDB(t)
mockDB.EXPECT().Get("ice cream").Return("chocolate").Once()
flavor := getFromDB(mockDB)
assert.Equal(t, "chocolate", flavor)
}
- mockery provides a much more user-friendly API and is less confusing to use
- mockery utilizes
testify
which is a robust and highly feature-rich testing framework - mockery has rich configuration options that allow fine-grained control over how your mocks are generated
- mockery's CLI is more robust, user-friendly, and provides many more options
- mockery supports generics (this may no longer be an advantage if/when gomock supports generics)
-
[![Grafana logo](assets/images/logos/grafana.svg){ class="center" width="100" }](https://github.com/grafana/grafana)
[grafana](https://github.com/grafana/grafana)
-
[![Google logo](assets/images/logos/google.svg){ class="center" width="100" }](https://github.com/google/skia)
[Google Skia](https://github.com/google/skia)
-
[![Google logo](assets/images/logos/google.svg){ class="center" width="100" }](https://github.com/google/syzkaller)
[Google Skyzkaller](https://github.com/google/syzkaller)
-
[![Hashicorp logo](assets/images/logos/hashicorp.svg){ class="center" width="100" }](https://github.com/search?q=org%3Ahashicorp%20mockery&type=code)
[Hashicorp](https://github.com/search?q=org%3Ahashicorp%20mockery&type=code)
-
[![Uber logo](assets/images/logos/uber.svg){ class="center" width="100" }](https://github.com/uber/cadence)
[Uber Cadence](https://github.com/uber/cadence)
-
[![Jaeger logo](assets/images/logos/jaeger.png){ class="center" width="300" }](https://github.com/jaegertracing/jaeger)
[Jaegertracing](https://github.com/jaegertracing/jaeger)
-
[![Splunk logo](assets/images/logos/splunk.svg){ class="center" width="300" }](https://github.com/splunk/kafka-mq-go)
[Splunk kafka-mq-go](https://github.com/splunk/kafka-mq-go)
-
[![Ignite Logo](assets/images/logos/ignite-cli.png){ class="center" width="300" }](https://github.com/ignite/cli)
-
[![Tendermint Logo](assets/images/logos/tendermint.svg){ class="center" width="300" }](https://github.com/tendermint/tendermint)
-
[![Datadog logo](assets/images/logos/datadog.svg){ class="center" width="300" }](https://github.com/DataDog/datadog-agent)
- [![Seatgeek Logo](assets/images/logos/seatgeek.svg)](https://seatgeek.com)
-
[![Amazon logo](assets/images/logos/amazon.svg){ class="center" width="300" }](https://github.com/eksctl-io/eksctl)
[eksctl](https://github.com/eksctl-io/eksctl)
-
[![MongoDB Logo](assets/images/logos/mongodb.svg){ class="center" width="300" }](https://github.com/search?q=org%3Amongodb%20mockery&type=code)
Get Started{ .md-button .md-button--primary .md-button--stretch }