当前位置: 首页 > news >正文

Go mockito 使用说明 (github/mockey)

GitHub - bytedance/mockey: a simple and easy-to-use golang mock library

Go mockito 是什么?

 mockey是一个简单易用的golang mock库,可以快速方便的mock函数和变量。目前广泛应用于字节跳动服务的单元测试编写。底层是monkey patch,通过在运行时重写函数指令实现。

  1. 编译时需要关闭inlining和compilation optimization,否则mock可能失败或者报错。有关详细信息,请参阅以下常见问题解答章节。
  2. 在实际编写单元测试的过程中,推荐配合Convey库一起使用。

 

安装

go get github.com/bytedance/mockey@latest

快速指南

import ("fmt""testing". "github.com/bytedance/mockey". "github.com/smartystreets/goconvey/convey"
)func Foo(in string) string {return in
}type A struct{}func (a A) Foo(in string) string { return in }var Bar = 0func TestMockXXX(t *testing.T) {PatchConvey("TestMockXXX", t, func() {Mock(Foo).Return("c").Build()   // mock functionMock(A.Foo).Return("c").Build() // mock methodMockValue(&Bar).To(1)           // mock variableSo(Foo("a"), ShouldEqual, "c")        // assert `Foo` is mockedSo(new(A).Foo("b"), ShouldEqual, "c") // assert `A.Foo` is mockedSo(Bar, ShouldEqual, 1)               // assert `Bar` is mocked})// mock is released automatically outside `PatchConvey`fmt.Println(Foo("a"))        // afmt.Println(new(A).Foo("b")) // bfmt.Println(Bar)             // 0
}

特征

对象

分类

功能细节

函数

基础 mock

普通函数

可变参数函数

普通方法

可变参数方法

嵌套结构体方法

私有类型的导出方法(不同包下)

其他功能

mock 后执行原函数

goroutine 条件过滤

增量改变 mock 行为

获取原函数执行次数

获取 mock 函数执行次数

变量

基础 mock

普通变量

函数变量

 兼容性

OS Support

  • Mac OS(Darwin)
  • Linux
  • Windows

Arch Support

  • AMD64
  • ARM64

Version Support

  • Go 1.13+

License 

 Mockey is distributed under the Apache License, version 2.0. The licenses of third party dependencies of Mockey are explained here.

FAQ 

如何禁用内联和编译优化? 

  1. Command line:go test -gcflags="all=-l -N" -v ./...
  2. Goland:fill -gcflags="all=-l -N" in the Run/Debug Configurations > Go tool arguments dialog box

mock后还是进入了原来的函数?

  1. 尝试使用调试模式。如果能跑通,说明就是问题所在。
  2. 忘记调用了Build(),导致没有实际效果
  3. 目标函数不完全匹配:
    func TestXXX(t *testing.T) {Mock((*A).Foo).Return("c").Build()fmt.Println(A{}.Foo("a")) // enters the original function, because the mock target should be `A.Foo`a := A{}Mock(a.Foo).Return("c").Build()fmt.Println(a.Foo("a")) // enters the original function, because the mock target should be `A.Foo` or extracted from instance `a` using `GetMethod`
    }
  4. 目标函数在其他goroutines中执行:
    func TestXXX(t *testing.T) {PatchConvey("TestXXX", t, func() {Mock(Foo).Return("c").Build()go Foo("a") // the timing of executing 'foo' is uncertain})// when the main goroutine comes here, the relevant mock has been released by 'PatchConvey'. If 'foo' is executed before this, the mock succeeds, otherwise it failsfmt.Println("over")time.Sleep(time.Second)
    }

报错“功能太短无法打补丁”?

  1. 内联或编译优化未禁用:您可以尝试使用调试模式。如果能跑通,说明就是问题所在。请转到常见问题解答的相关部分
  2. 函数真的太短了:意思是目标函数不到一行,导致编译出来的机器码太短。一般两行以上不会出现这个问题
  3. PatchConveyRepeat mocking the same function:以最小单位重复mock同一个函数。如果有这样的需求,请获取Mocker实例并重新mock。
  4. 其他工具mock这个函数:比如monkey或者其他工具mock过这个函数

 

http://www.lryc.cn/news/25093.html

相关文章:

  • Spring Boot+Vue前后端分离项目练习01之网盘项目的搭建
  • 超详细MySQL(免安装版)安装与配置
  • STM32F1,F4,L1系列禁止JTAG和SW引脚方法
  • NVIDIA CUDA初级教程视频学习笔记1
  • CEC2005:星雀优化算法(Nutcracker optimizer algorithm,NOA)求解CEC2005(提供MATLAB代码)
  • 工作实战之密码防重放攻击
  • 如何编写测试用例?
  • 5.排序算法之二:选择排序
  • Ubuntu18系统安装:node及node版本管理工具nvm部署前端项目
  • 统计学 假设检验
  • 【C++】哈希
  • 「TCG 规范解读」PC 平台相关规范(3)
  • 这篇教你搞定Android内存优化分析总结
  • 概率论与数理统计期末小题狂练 11-12两套,12-13-1
  • golang对字符串的处理操作 如何正确理解 rune byte和string
  • 软件项目管理简答题复习(1)
  • 云Windows Server 2022 Datacenter 安装MySQL8解压缩版 mysql-8.0.32-winx64 230301记录
  • 如何使用BeaconEye监控CobaltStrike的Beacon
  • STM32开发(17)----CubeMX配置CRC
  • 【MySQL】基础操作:登录、访问、退出和卸载
  • 【算法经典题集】递推(持续更新~~~)
  • mysql兼容性验证
  • C++回顾(五)—— 构造函数和析构函数
  • 嵌入式学习笔记——概述
  • 化繁为简高效部署 华为云发布部署服务CodeArts Deploy
  • 注意力机制详解系列(四):混合注意力机制
  • Makefiles学习1
  • 日志框架以及如何使用LogBack记录程序
  • 集成RocketChat至现有的.Net项目中,为ChatGPT铺路
  • 王道操作系统课代表 - 考研计算机 第三章 内存管理 究极精华总结笔记