Mocktail
Naive code generator that creates mock implementation using testify.mock
Install / Use
/learn @traefik/MocktailREADME
Mocktail
<p align="center"> <picture> <source media="(prefers-color-scheme: dark)" srcset="./mocktail-dark.png"> <source media="(prefers-color-scheme: light)" srcset="./mocktail.png"> <img alt="Mocktail logo" src="./mocktail.png"> </picture> </p>Naive code generator that creates mock implementation using testify.mock.
Unlike mockery, Mocktail generates typed methods on mocks.
For an explanation of why we created Mocktail, you can read this article.
How to use
- Create a file named
mock_test.goinside the package that you can to create mocks. - Add one or multiple comments
// mocktail:MyInterfaceinside the filemock_test.go.
package example
// mocktail:MyInterface
How to Install
Go Install
You can install Mocktail by running the following command:
go install github.com/traefik/mocktail@latest
Pre-build Binaries
You can use pre-compiled binaries:
- To get the binary just download the latest release for your OS/Arch from the releases page
- Unzip the archive.
- Add
mocktailin yourPATH.
Notes
It requires testify >= v1.7.0
Mocktail can only generate mock of interfaces inside a module itself (not from stdlib or dependencies)
The // mocktail comments must be added to a file named mock_test.go only,
comments in other files will not be detected
Examples
package a
import (
"context"
"time"
)
type Pineapple interface {
Juice(context.Context, string, Water) Water
}
type Coconut interface {
Open(string, int) time.Duration
}
type Water struct{}
package a
import (
"context"
"testing"
)
// mocktail:Pineapple
// mocktail:Coconut
func TestMock(t *testing.T) {
var s Pineapple = newPineappleMock(t).
OnJuice("foo", Water{}).TypedReturns(Water{}).Once().
Parent
s.Juice(context.Background(), "", Water{})
var c Coconut = newCoconutMock(t).
OnOpen("bar", 2).Once().
Parent
c.Open("a", 2)
}
Exportable Mocks
If you need to use your mocks in external packages just add flag -e:
mocktail -e
In this case, mock will be created in the same package but in the file mock_gen.go.
Related Skills
node-connect
335.2kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
xurl
335.2kA CLI tool for making authenticated requests to the X (Twitter) API. Use this skill when you need to post tweets, reply, quote, search, read posts, manage followers, send DMs, upload media, or interact with any X API v2 endpoint.
frontend-design
82.5kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
335.2kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
