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. 《DSP using MATLAB》示例Example 9.7

    代码: %% ------------------------------------------------------------------------ %% Output Info about ...

  2. flask第二十篇——模板【3】

    请关注公众号:自动化测试实战 现在我们通过查询字符串的方式给render_template传参,我们就要用到flask库的flask.request.args.get()函数先获取参数,在index. ...

  3. 《selenium2 python 自动化测试实战》(11)——selenium安装版本

    先和大家说一下selenium环境的问题,大家可以在cmd里先看一下自己的selenium版本: 回车,就可以安装了. 本来想和大家说如何跳过验证码进行登录的,结果好多朋友加我问我环境配置的问题,所以 ...

  4. Django基于Form之登录和注册

    1.创建Forms文件,内容略多,大家将就着看,不懂请留言 #!/usr/bin/env python # -*- coding: utf8 -*- #__Author: "Skiler H ...

  5. 基于模k可逆计数的数字锁相环fpga实现

    参考http://wenku.baidu.com/view/59420cb069dc5022aaea00bd.html 实现结构是参考的上边的实例,我用的全同步实现,实现过程中发现一些现象,做下记录. ...

  6. Mysql-Proxy 读写分离的各种坑,特别是复制延迟时

    延迟问题读写分离不能回避的问题之一就是延迟,可以考虑Google提供的SemiSyncReplicationDesign补丁. 端口问题MySQL-Proxy缺省使用的是4040端口,如果你想透明的把 ...

  7. C/C++中一些不太注意到的小知识点--[锦集]

    C/C++中一些不太注意到的小知识点--[锦集] C/C++小知识点--[锦集] "="和"<=" 的优先级 1.( (file_got_len = re ...

  8. jQuery的页面初始化操作写法

    $(document).ready(function(){ alert("第一种方法."); }); $(function(){ alert("第二种方法.") ...

  9. RK3288 usb 摄像头旋转

    系统:Android 5.1 下面实现了摄像头 180 度旋转,旋转角度只需修改 orientation. diff --git a/hardware/rockchip/camera/CameraHa ...

  10. jeecg中List页面的高级查询

    1.普通的高级查询 <t:datagrid name="orderworthList" title="订单价值统计" actionUrl="or ...