package main

import (
"fmt"
"github.com/bouk/monkey"
"os"
"os/exec"
"reflect"
"testing"
) // 假如我们要测试函数 call
func call(cmd string) (int, string) {
bytes, err := exec.Command("sh", "-c", cmd).CombinedOutput()
output := string(bytes)
if err != nil {
return 1, reportExecFailed(output)
}
return 0, output
} // 上面的函数会调用它,这个函数一定要mock掉!
func reportExecFailed(msg string) string {
os.Exit(1) // 讨人嫌的副作用
return msg
} func TestExecSussess(t *testing.T) {
// 恢复 patch 修改
// 实际使用中会把 UnpatchAll 放到 teardown 函数里
// 不过在 go 自带的 testing 里就这么处理了
defer monkey.UnpatchAll()
// mock 掉 exec.Command 返回的 *exec.Cmd 的 CombinedOutput 方法
monkey.PatchInstanceMethod(
reflect.TypeOf((*exec.Cmd)(nil)),
"CombinedOutput", func(_ *exec.Cmd) ([]byte, error) {
return []byte("results"), nil
},
)
// mock 掉 reportExecFailed 函数
monkey.Patch(reportExecFailed, func(msg string) string {
return msg
}) rc, output := call("any")
if rc != 0 {
t.Fail()
}
if output != "results" {
t.Fail()
}
} func TestExecFailed(t *testing.T) {
defer monkey.UnpatchAll()
// 上次 mock 的是执行成功的情况,这一次轮到执行失败
monkey.PatchInstanceMethod(
reflect.TypeOf((*exec.Cmd)(nil)),
"CombinedOutput", func(_ *exec.Cmd) ([]byte, error) {
return []byte(""), fmt.Errorf("sth bad happened")
},
)
monkey.Patch(reportExecFailed, func(msg string) string {
return msg
}) rc, output := call("any")
if rc != 1 {
t.Fail()
}
if output != "" {
t.Fail()
}
}
=====================================
package main

import (
"fmt"
"github.com/bouk/monkey"
"strings"
) func main() {
var guard *monkey.PatchGuard
guard = monkey.Patch(fmt.Println, func(a ...interface{}) (n int, err error) {
s := make([]interface{}, len(a))
for i, v := range a {
s[i] = strings.Replace(fmt.Sprint(v), "hell", "*bleep*", -1)
}
// 以下代码等价于
// guard.Unpatch()
// defer guard.Restore()
// return fmt.Println(s...)
guard.Unpatch()
n, err = fmt.Println(s...)
guard.Restore()
return
})
fmt.Println("what the hell?") // what the *bleep*?
fmt.Println("what the hell?") // what the *bleep*?
}

go unit test-monkey的更多相关文章

  1. 【初学python】使用python调用monkey测试

    目前公司主要开发安卓平台的APP,平时测试经常需要使用monkey测试,所以尝试了下用python调用monkey,代码如下: import os apk = {'j': 'com.***.test1 ...

  2. ABP(现代ASP.NET样板开发框架)系列之12、ABP领域层——工作单元(Unit Of work)

    点这里进入ABP系列文章总目录 基于DDD的现代ASP.NET开发框架--ABP系列之12.ABP领域层——工作单元(Unit Of work) ABP是“ASP.NET Boilerplate Pr ...

  3. ABP源码分析十:Unit Of Work

    ABP以AOP的方式实现UnitOfWork功能.通过UnitOfWorkRegistrar将UnitOfWorkInterceptor在某个类被注册到IOCContainner的时候,一并添加到该类 ...

  4. Failed to stop iptables.service: Unit iptables.service not loaded.

    redhat 7 [root@lk0 ~]# service iptables stop Redirecting to /bin/systemctl stop iptables.service Fai ...

  5. Monkey Patch/Monkey Testing/Duck Typing/Duck Test

    Monkey Patch Monkey Testing Duck Typing Duck Test

  6. VS2012 Unit Test 个人学习汇总(含目录)

    首先,给出MSDN相关地址:http://msdn.microsoft.com/en-us/library/Microsoft.VisualStudio.TestTools.UnitTesting.a ...

  7. VS2012 Unit Test —— 我对IdleTest库动的大手术以及对Xml相关操作进行测试的方式

    [1]我的IdleTest源码地址:http://idletest.codeplex.com/ [2]IdleTest改动说明:2013年10月份在保持原有功能的情况下对其动了较大的手术,首先将基本的 ...

  8. VS2012 Unit Test——Microsoft Fakes入门

    如题,本文主要作为在VS2012使用Fakes的入门示例,开发工具必须是VS2012或更高版本. 关于Fakes的MSDN地址:http://msdn.microsoft.com/en-us/libr ...

  9. monkey命令选项参考

    基本参数:     --help              打印帮助消息 -v  可以在命令行中出现多次,每次一个-V选项都会增加monkey向命令行打印输出的详细级别.默认的级别0只会打印启动信息. ...

  10. MTU(Maximum transmission unit) 最大传输单元

    最大传输单元(Maximum transmission unit),以太网MTU为1500. 不同网络MTU如下: 如果最大报文数据大小(MSS)超过MTU,则会引起分片操作.   路径MTU: 网路 ...

随机推荐

  1. Linux安装vsftpd总结

    我使用的是CentOS6安装的vsftpd,转载请注明出处,以下是我的记录: #查看是否已经安装了vsfptd vsftpd -v #安装 yum -y install vsftpd #创建:chro ...

  2. apache与nginx对比优势及劣势

    1.nginx相对于apache的优点:轻量级,同样起web 服务,比apache占用更少的内存及资源抗并发,nginx 处理请求是异步非阻塞的,而apache 则是阻塞型的,在高并发下nginx 能 ...

  3. maven创建多模块项目

    在eclipse下构建maven项目,该项目由多个子模块组成. 1.创建一个父项目 NEW -->project-->maven-->maven Project,点击下一步,进入ne ...

  4. drone 学习二 pipeline 说明

    1. 基本语法 pipeline: backend: image: golang commands: - go build - go test frontend: image: node comman ...

  5. jspm 安装试用

    1. 安装 yarn global add jspm or npm install -g jspm 2. 创建项目使用 mkdir appdmeo jspm init 3. 安装依赖 jspm ins ...

  6. Beyond Compare3 注册密钥和添加到右键菜单

    本人使用的是 Beyond Compare 3 ,版本 3.3.8(build 16340),密钥应收Beyond Compare 3都能够使用. 如想查看密钥,请参考本文末尾的隐藏内容 Beyond ...

  7. FastAdmin 的 API 可以分级吗?

    FastAdmin 的 API 可以分级吗? 有小伙伴问 FastAdmin 的API 可以分别吗,使用 / 出现错误. Karson 的说明是: 完全支持的,默认是使用.进行分隔的,如果需要/,请开 ...

  8. printf()_scanf()_取余运算符与取模运算符

    基本的输入和输出函数的用法 printf();四种用法 1.printf("字符串\n"); 2.printf("输出控制符",输出参数); 3.printf( ...

  9. BOM的编制与管理

    Bill of Material BOM英文全称 Bill of Material,即“物料清单”,也称产品结构表.在制造业管理信息系统中,经常会提到BOM.物料清单是指产品所需零部件明细表及其结构. ...

  10. 紫金桥OPC接口使用技巧

    OPC接口使用技巧 OPC接口是由OPC基金会制定的,基于DCOM技术的,用于控制系统软件之间进行数据通讯的接口规范.由于其开放性和高效性,现在已被广泛应用于自动化控制领域及生产信息管理中.紫金桥软件 ...