Top AI Repos — open-source AI, indexed and scored
Top AI Repos tracks AI repositories on GitHub and answers two different questions about each one: is it moving right now, and would you bet a product on it.
Top AI Repos tracks AI repositories on GitHub and answers two different questions about each one: is it moving right now, and would you bet a product on it.
A simple and easy-to-use Go mocking library derived from ByteDance's internal best practices
| Date | Stars |
|---|---|
| 2026-07-24 | 902 |
| 2026-07-25 | 901 |
| 2026-07-28 | 901 |
| 2026-07-30 | 901 |
| 2026-08-06 | 901 |
Today
— stars today
This week
— stars this week
This month
— stars this month
Momentum
0.0
growth rate 0.00%/day
# Mockey
English | [中文](README_cn.md)
[](https://github.com/bytedance/mockey/releases)
[](https://github.com/bytedance/mockey/blob/main/LICENSE-APACHE)
[](https://goreportcard.com/report/github.com/bytedance/mockey)
[](https://codecov.io/github/bytedance/mockey)
[](https://github.com/bytedance/mockey/issues)
[](https://github.com/bytedance/mockey/issues?q=is%3Aissue+is%3Aclosed)
Mockey is a simple and easy-to-use golang mock library, which can quickly and conveniently mock functions and variables. At present, it is widely used in the unit test writing of ByteDance services (7k+ repos) and is actively maintained. In essence, it rewrites function instructions at runtime similarly to [monkey](https://github.com/bouk/monkey) or [gomonkey](https://github.com/agiledragon/gomonkey).
Mockey makes it easy to replace functions, methods and variables with mocks reducing the need to specify all dependencies as interfaces.
> 1. Mockey requires **inlining and compilation optimization to be disabled** during compilation, or it won't work. See the [FAQs](#how-to-disable-inline-and-compile-optimization) for details.
> 2. It is strongly recommended to use it together with the [goconvey](https://github.com/smartystreets/goconvey) library in unit tests.
## Install
```
go get github.com/bytedance/mockey@latest
```
## Quick Guide
### Simplest example
```go
package main
import (
"fmt"
"math/rand"
. "github.com/bytedance/mockey"
)
func main() {
Mock(rand.Int).Return(1).Build() // mock `rand.Int` to return 1
fmt.Printf("rand.Int() always return: %v\n", rand.Int()) // Try if it's still random?
}
```
### Unit test example
```go
package main_test
import (
"math/rand"
"testing"
. "github.com/bytedance/mockey"
. "github.com/smartystreets/goconvey/convey"
)
// Win function to be tested, input a number, win if it's greater than random number, otherwise lose
func Win(in int) bool {
return in > rand.Int()
}
func TestWin(t *testing.T) {
PatchConvey("TestWin", t, func() {
Mock(rand.Int).Return(100).Build() // mock
res1 := Win(101) // execute
So(res1, ShouldBeTrue) // assert
res2 := Win(99) // execute
So(res2, ShouldBeFalse) // assert
})
}
```
## Features
- Mock functions and methods
- Basic
- Simple / generic / variadic function or method (value or pointer receiver)
- Supporting hook function
- Supporting `PatchConvey` and `PatchRun` (automatically release mocks after each test case)
- Providing `GetMethod` to handle special cases (e.g., unexported types, unexported method, and methods in nested structs)
- Advanced
- Interface mocking (experimental feature)
- Conditional mocking
- Sequence returning
- Decorator pattern (execute the original function after mocking)
- Goroutine filtering (inclusion, exclusion, targeting)
- Acquire `Mocker` for advanced usage (e.g., getting the execution times of target/mock function)
- Mock variable
- Common variable
- Function variable
## Compatibility
### OS Support
- Mac OS(Darwin)
- Linux
- Windows
### Arch Support
- AMD64
- ARM64
### Version Support
- Go 1.13+
## Basic Features
### Simple function/method
Use `Mock` to mock function/method, use `Return` to specify the return value, and use `Build` to make the mock effective:
```go
package main
import (
"fmt"
. "github.com/bytedance/mockey"
)
func Foo(in string) string {
return in
}
type A struct{}
func (a A) Foo(in string) string { return in }
type B struExcerpt of 30,204 characters
Read on GitHub95
53
2
Willem Jiang
1
1
1
Would you bet a product on this? Bounded 0–100 and slow moving.
matched fp:cf8852bd42eaacd1, topic:testing