moco框架使用

2019-11-18  本文已影响0人  陌椰_5702

一、moco介绍

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

moco框架是github上的一个开源项目,可模拟http,https,Socket协议。

二、moco启动

1.下载源码:https://github.com/dreamhead/moco

2.下载jar包,放在项目路径下

3.在项目路径下新建json文件

4.执行命令

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

比如 java -jar moco-runner-1.0.0-standalone.jar http -p 8880 -c post.json

下面讲实战demo:

1.在post.json中输入要mock的数据


[

{

"description":"这是我们的第一个mock例子",

"request":{

"uri":"/post"

    },

"response":

{

"text":"Hello,Moco"

    }

}

]

2.在浏览器输入
http://127.0.0.1:8880/post

image.png

三、基本请求数据格式

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":"这个是带get method with paramter"
    }
  }
]

第一种不带参数的就不在此赘述了,
第二种可以使用接口工具比如postman来模拟 ,以下就表示成功了。

image.png

2.post请求,form格式的请求参数

[
  {
    "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 并且是form格式的"
    }
  }
]

3.post请求,请求格式为json并且带cookie

[
  {
    "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 并且是form格式的"
    }
  }
]

4.post请求,请求包含header

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

5.请求重定向

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

6.返回json格式的数据

[
  {
    "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": {
      "json": {
        "code": "C0",
        "msg": "查询成功",
        "data": {
          "name": "周**",
          "cid": "22************011",
          "respCode": "0",
          "respDesc": "一致,成功",
          "detail": {
            "name": "周**",
            "cid": "22************011",
            "driveIssueDate": "A",
            "driveValidStartDate": "A",
            "firstIssueDate": "A",
            "validDate": "-B",
            "driveCardStatus": "A",
            "allowDriveCar ": "C小型汽车 ",
            "driveLicenseType ": "A ",
            "gender ": "1 / 男性 "
          }

        }
      }
    }

  }
]

以上就是moco框架的基本使用~有问题留言哦~

上一篇 下一篇

猜你喜欢

热点阅读