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 8.13

    %% ------------------------------------------------------------------------ %% Output Info about thi ...

  2. 异常Failure to transfer org.apache.maven.plugins:maven-surefire-plugin:pom:2.12.4 from http://maven.aliyun.com/nexus/content/groups/public was ...

    错误异常:Failure to transfer org.apache.maven.plugins:maven-surefire-plugin:pom:2.12.4 from http://maven ...

  3. FastAdmin 线上部署流程 (2018-05-03 更新)

    FastAdmin 线上部署流程 首次部署 建立 git 环境. 建立 composer 环境. 建立 bower 环境. 将远程项目代码 git clone 到服务器上. 执行 composer i ...

  4. 从wiresharp看tcp三次握手

    我们知道,传输层是OSI模型中用户进行数据传输的分层,目前仅有TCP和UDP两种协议可用.TCP为了进行传输控制,引入了三次握手机制,以确保通信连接的建立.道理很简单,我们跟别人打电话聊天时,对方拿起 ...

  5. C++关于变量初始化的琐记

    #include <iostream> using namespace std; class Base{ virtual void func1() { cout<<" ...

  6. 设计WEB数据库(学习)

    设计WEB数据库 1.考虑建模的实际对象 为现实世界的实体和关系建立模型 在上面情况下考虑建表呢? 答:如果有一组属于同一类型的数据,就可以根据这些数据创建表 2.避免保存冗余数据 原因:a.空间的浪 ...

  7. 关于不同应用程序存储IO类型的描述

    介绍 存储系统作为数据的载体,为前端的服务器和应用程序提供读写服务.存储阵列某种意义上来说,是对应用服务器提供数据服务的后端“服务器”.应用服务器对存 储系统发送数据的“读”和“写”的请求.然而,不同 ...

  8. 用活Firewalld防火墙之direct

    原文地址:http://www.excelib.com/article/294/show 学生在前面已经给大家介绍过了Firewalld中direct的作用,使用他可以直接使用iptables.ip6 ...

  9. centos7 yum 安装mysql

    介绍在CentOS7上yum安装数据库服务器MySQL Community Server 5.7的方法. 准备 CentOS7默认安装了和MySQL有兼容性的MariaDB数据库,在我们安装MySQL ...

  10. k8s1.4.3安装实践记录(3)下载基础镜像

    下载基础镜像,因为Google被墙,所以我们用时速云中的镜像来tag docker pull index.tenxcloud.com/google_containers/pause-amd64:3.0 ...