moco框架的使用

一、moco框架基本介绍

mock测试:mock测试就是在测试过程中,对于某些不容易构造或者不容易获取的对象,用一个虚拟的对象来创建以便测试的测试方法。一般可分为模拟调用方和被调用方2种方式。

mock用来模拟接口,这里mock用的是moco框架,moco框架是github上的一个开源项目,可模拟http,https,Socket协议。moco有几种使用方法,这里介绍的是standolone用法,更多用法可参考https://github.com/dreamhead/moco/blob/master/moco-doc/usage.md

Usage

You have several ways to use Moco. One is API, which you can use in your unit test. The other is that run Moco as standalone. Currently, you put all your configuration in JSON file.

On the other hand, Moco has several different ways to integrate with some tools: Maven plugin, Gradle plugin and shell support

二、moco的启动及demo

1、moco的启动

1)、到github上下载moco-runner-0.12.0-standalone.jar 包【这里下载了当前最新版本】

下载地址https://github.com/dreamhead/moco

2)、moco的启动

在命令行中执行如下命令:

java -jar moco-runner-0.12.0-standalone.jar  协议类型  -p 端口号 -c json配置文件

2、demo

1)将jar包放在某个目录下。

2)json配置文件:demo.json

[
  {
    "description":"这是我们的第一个mock例子",
    "request":{
      "uri":"/demo"
    },
    "response":
    {
      "text":"Hello,Moco"
    }
  }
]

3)在命令行中执行

java -jar ./moco-runner-0.12.0-standalone.jar  http -p 8888 -c  demo.json

备注:这个例子中 moco-runner-0.12.0-standalone.jar 与demo.json文件在同一目录下

执行结果如下:说明启动成功了

2

4)在浏览器中输入http://127.0.0.1:8888/demo 进行访问,得到响应结果

三、常用请求

以下是json配置文件内容

1、get请求

[
  {
    "description":"不带参数的get请求",
    "request":{
      "uri":"/withGetDemo",
      "method":"get"
    },
    "response":{
      "text":"这是不带参数的get请求"
    }
  },
  {
    "description":"带参数的get请求,p1,p2分别的参数1,参数2,名称可随便起,个数也可随便加",
    "request":{
      "uri":"/wihtGetDemobyParam",
      "method":"get",
      "queries":{
        "p1":"hh",
        "p2":"good"
      }
    },
    "response":{
      "text":"this is a get method with paramter"
    }
  }
]

2、post请求

[
  {
    "description":"post 请求",
    "request":{
      "uri":"/postDemo",
      "method":"post"
    },
    "response":{
      "text":"This is post request"
    }
  },
  {
    "description":"带参数的post请求",
    "request":{
      "uri":"/postDemoWithParam",
      "method":"post",
      "forms":{
        "param1":"one",
        "param2":"two"
      }
    },
    "response":{
      "text":"this is post request with param"
    }
  }
]

3、post请求,(请求参数为json格式、请求带cookies)

[
  {
    "description":"这是一个带cookies的Post请求",
    "request":{
      "uri":"/postDemoWithCookies",
      "cookies":{
        "login":"true"
      },
      "json":{
        "name":"hi",
        "age":"3"
      }
    },
    "response":{
      "status":"200",
      "json":{
        "name":"success",
        "status":"1"
      }
    }
  }
]

4、请求带header

[
  {
    "description":"带header请求",
    "request": {
      "uri": "/withHeader",
      "method": "post",
      "headers": {
        "content-type": "application/json"
      },
      "json": {
        "name": "xiaoming",
        "age": "18"
      }
    },
      "response":{
        "json":{
          "message":"success",
          "status":"1"
        }
      }
    }
]

5、请求重定向

[
  {
    "description":"重定向到百度",
    "request":{
      "uri":"/redirect",
      "method":"get"
    },
    "redirectTo":"http://www.baidu.com"
  },
  {
    "description":"这是被重定向的请求",
    "request":{
      "uri":"/toRedirect"
    },
    "response":{
      "text":"this is the redirect page"
    }
  },
  {
    "description":"重定向到自己的网页上",
    "request":{
      "uri":"/myStation"
    },
    "redirectTo":"/toRedirect"
  }
]

 

  • 7
    点赞
  • 36
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值