Vibeleaderboard
Index / tool
Visit github.com
Category
Developer Tools
Rank
Pricing
Open Source
Type
TOOL
Builder
bytedance
GitHub
905 stars
Latest release
v1.4.6
Date

About

A simple Go mocking library derived from ByteDance's internal test practices. Patch functions and methods at runtime for unit testing.

What it does

Mockey lets Go tests control hard-coded dependencies without redesigning production code around interfaces. Tests can provide fixed results or custom hooks, vary behavior by conditions or call order, target selected goroutines, and restore original behavior after a scoped test block.

Why it's ranked here

Its strongest case is practical reach: it handles ordinary, generic, and variadic code, methods, variables, conditional behavior, sequences, and scoped cleanup. That breadth comes with a serious caveat. It changes executable instructions and requires special compiler flags, so it suits controlled test builds better than routine dependency injection.

What's good

Scoped lifecycle helpers automatically restore replacements and support nesting, which reduces cleanup mistakes. Hooks may preserve access to the original implementation. Signature and return checks catch mismatches. Advanced controls include call counts, goroutine targeting, conditional behavior, sequential results, and support for difficult unexported methods.

Tradeoffs

Tests must disable inlining and compiler optimization, otherwise replacements may fail. The implementation depends on unsafe memory access, instruction disassembly, runtime internals, and architecture-specific behavior. Manual restoration must follow last-in, first-out order in some generic cases or crashes may occur. Interface mocking remains experimental.

How to use it well

Use it for Go unit tests around legacy or tightly coupled code where introducing interfaces would be disproportionate. Keep replacements inside scoped lifecycle blocks and compile tests with the required flags. Prefer the lightweight scope helper unless you want GoConvey integration. Mockey does not replace an assertion or test-organization framework.

Technical notes+

internal/monkey/patch.go copies original instructions, builds branch code and a proxy, then writes the hook with stop-the-world memory handling; unpatching restores the saved bytes. internal/fn/analyzer.go and internal/fn/analyzer_1_20.go inspect runtime function shapes and adapt generic or method arguments. internal/fn/generic.go, internal/tool/goroutine.go, and internal/unsafereflect/type.go use unsafe and runtime layout knowledge. internal/tool/check_gcflags.go warns unless tests use -gcflags="all=-N -l", with MOCKEY_CHECK_GCFLAGS=false as an escape hatch. internal/tool/debug.go enables diagnostics through MOCKEY_DEBUG=true. internal/monkey/patch_test.go verifies normal, anonymous, closure, and reflective hooks, proxy calls, and restoration. go.mod declares Go 1.18 and dependencies on GoConvey, x/arch, and x/sys.

Observed

License
Apache License 2.0
Primary language
Go
Install surface
Go module installed with go get from github.com/bytedance/mockey
Interface
Imported Go testing library
Operating systems
Darwin, Linux, and Windows
Architectures
AMD64 and ARM64
Module declaration
go.mod declares Go 1.18
Test structure
Repository text includes an internal monkey patch test file

Read from README.md, go.mod, internal/fn/generic.go, internal/tool/check.go, internal/tool/debug.go, internal/fn/analyzer.go, internal/tool/assert.go, internal/tool/caller.go, internal/monkey/patch.go, internal/tool/reflect.go, internal/tool/goroutine.go, internal/fn/analyzer_1_20.go, internal/monkey/patch_test.go, internal/tool/check_gcflags.go, internal/unsafereflect/type.go.

What it can do

  • Patch functions at runtime

    Function reference and mock implementationMocked function behavior during test execution

  • Patch methods at runtime

    Method reference and mock implementationMocked method behavior during test execution

  • Create mock implementations for unit testing

    Original function or method signaturesMock functions and methods for test isolation

  • Replace function behavior during test execution

    Target function and replacement logicModified function behavior for testing scenarios

  • Isolate code dependencies for testing

    Code with external dependenciesIsolated test environment with mocked dependencies

Tags

gotestingmockinglibrary

Tech Stack

Go

Comments (0)

No comments yet

Editorially curated, with community endorsements as a secondary signal. Corrections welcome.