Giter VIP home page Giter VIP logo

monkey's Introduction

Go语言猴子补丁框架 🙉 🐒

test workflow

Go 语言猴子补丁(monkey patching)框架。

本项目对 Bouke 的项目做了优化,不同协程可以独立 patch 同一个函数而互不影响。从而可以并发运行单元测试。

工作原理请参考我的系列文章:

Bouke 已经不再维护原项目,所以只能开一个新项目了🤣。

有兴趣的同学也可以加微信 taoshu-in 讨论,拉你进群。

快速入门

首先,引入 monkey 包

go get github.com/go-kiss/monkey

然后,调用 monkey.Patch 方法 mock 指定函数。

package main

import (
	"fmt"

	"github.com/go-kiss/monkey"
)

func sum(a, b int) int { return a + b }

func main() {
	monkey.Patch(sum, func(a b int) int { return a - b })
	fmt.Println(sum(1,2)) // 输出 -1
}

更多用法请参考使用示例测试用例

注意事项

  1. Monkey 需要关闭 Go 语言的内联优化才能生效,比如测试的时候需要:go test -gcflags=-l
  2. Monkey 需要在运行的时候修改内存代码段,因而无法在一些对安全性要求比较高的系统上工作。
  3. Monkey 不应该用于生产系统,但用来 mock 测试代码还是没有问题的。
  4. Monkey 目前仅支持 amd64 指令架构。支持 linux 和 macos。目前 windows 平台还有问题。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.