Mock测试方法Moco测试框架学习

一、基本介绍

Mock测试:就是在测试过程中,对于某些不容易构造或者不容易获取的对象,用一个虚拟的对象来创建以便测试的测试方法。

Moco框架是一种实现Mock测试,GitHub上开源的框架

1、获取moco框架 地址:http://repo1.maven.org/maven2/com/github/dreamhead/moco-runner/0.11.0/

学习的是standolone用法,更多用法可参考https://github.com/dreamhead/moco/blob/master/moco-doc/usage.md

或者:https://github.com/dreamhead/moco/blob/master/moco-doc/apis.md

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

下载这个:moco-runner-0.11.0-standalone.jar

二、moco启动demo

·1、创建文件夹将moco-runner-0.11.0-standalone.jar放置文件夹中

 2、创建请求json文件(文件格式不能写错)

[{
  "description": "第一个moco接口框架demo",
  "request": {
    "uri": "/demo"
  },
  "response": {
    "text": "第一个moco框架demo"
  }
}
]

3、启动描述文件(启动命令)

java  -jar ./moco-runner-0.11.0-standalone.jar http -p 8888 -c startup.json

命令描述:

java -jar ./moco-runner-0.11.0-standalone.jar http -p 8888 -c
http 协议类型,其中"-p" 表示端口,-c 表示 json配置文件

4、带参数的get请求和不带参数的get请求

[
  {
    "description": "不带参数的get请求",
    "request": {
      "uri": "/get",
      "method": "get"
    },
    "response": {
      "headers": {
        "Content-Type": "text/html;charset:utf-8"
      },
      "text": "这是一个不带参数的get请求"
    }
  },
  {
    "description": "带参数的get请求",
    "request": {
      "uri": "/getwithparam",
      "method": "get",
      "queries": {
        "name": "zhansan",
        "age": "18"
      }
    },
    "response": {
      "headers": {
        "Content-Type": "text/html;charset:utf-8"
      },
      "text": "这是一个带参数的get请求"
    }
  }
]

6、moco中配置post请求

注意Post请求无法再浏览器中查看请求结果,需要Jmeter或Postman工具

特别注意post请求和get请求uri的命名要规范

[
  {
    "description": "这是一个不带参数的post请求",
    "request": {
      "uri": "/post",
      "method": "post"
    },
    "response": {
      "headers": {
        "content-Type": "text/html;charset:utf-8"
      },
      "text": "这是一个不带参数的post请求"
    }
  },

  {
    "description": "这是一个带参数的post请求",
    "request": {
      "uri": "/postBy",
      "method": "post",
      "forms": {
        "name":"zhansan",
        "sex": "man"
      }
    },
    "response": {
      "headers": {
        "Content-Type": "text/html;charset:gbk"
      },
      "text": "这是一个带参数的post请求"
    }
  }
]

7、带cookies信息的get请求和post请求,注意post请求参数是forms

[
  {
    "description": "这是一个带cookies信息的get请求",
    "request": {
        "uri": "/getWithCookies",
      "method": "get",
      "cookies": {
        "login": "true"
      },
      "queries": {
        "name": "hanSan",
        "age": "20"
      }
    },
    "response": {
      "text": "这是一个带cookies信息的get请求"
    }
  },
  {
  "description": "这是一个带cookies信息的post请求",
  "request": {
    "uri": "/postWithCookies",
    "method": "post",
    "cookies": {
      "login": "true"
    },
    "forms": {
      "name": "liSi",
      "age": "25"
    }
  },
  "response": {
    "text": "这是一个带cookies信息的get请求"
  }
}
]

8、带headers信息的moco请求

[
  {
    "description": "这是一个带headers信息的get请求",
    "request": {
      "uri": "/postWithHeaders",
      "method": "post",
      "headers": {
        "content-type":"application/json"
      },
      "json": {
        "name": "angSan",
        "sex": "man"
      }
    },
    "response": {
      "json": {
        "angSan": "请求成功了",
        "status": "200"
      }
    }
  }
]

注意Jmeter中需要有HTTP请求头信息,且请求参数为json格式

9、请求重定向的实现

通过redirectTo 实现

[
  {
    "description": "这是一个重定向的get请求",
    "request": {
      "uri": "/getRedirectTo",
      "method": "get"
    },
    "redirectTo": "newRui"
  },
  {
    "description": "这是重定向后的请求结果",
    "request": {
      "uri": "/newRui",
      "method": "get"
    },
    "response": {
      "headers":{
        "Content-Type": "text/html;charset:utf-8"
      },
      "text": "重定向成功了"
    }
  }
]
发布了17 篇原创文章 · 获赞 0 · 访问量 175

猜你喜欢

转载自blog.csdn.net/qq_37637691/article/details/87373740
今日推荐