接口测试-moco框架+实例

moco

1.什么是moco框架

是开发在开发的过程中,需要依赖一部分的接口,但是对方没有提供或者环境等等情况

2.moco下载安装

安装jdk1.8并配置环境变量
http://central.maven.org/maven2/com/github/dreamhead/moco-runner/1.0.0/moco-runner-1.0.0-standalone.jar
下载moco-runner到本地

3.mock目录结构

moco-runner-1.0.0-standalone.jar 放到文件夹中
同级目录存放 configure.json(get或者post数据)

D:\moco-runner\
    ├─moco-runner-1.0.0-standalone.jar
    └─configure.json

4.启动服务

java -jar moco-runner-1.0.0-standalone.jar start -p 8888 -c configure.json
java -jar jar包的路径 http -p 运行端口 -c 要运行的文件路径

如果启动失败了,请检查json数据是否正确

json格式很重要
单词的拼写也很重要

5.实际事例

把get或者post放到configure.json中

1.get接口实例

一段json数据,json的格式

[{
		"key1": "values",
		"key2": "values"
	},
	{
		"key1": "values",
		"key2": "values"
	}
]

数据实例

[
  {
    "description": "一个简单的get请求",
    "request": {
      "method": "get",
      "uri": "/login"
    },
    "response": {
      "text": "我是login get method",
      "headers":{
        "Content-Type":"text/html;charset=utf-8"
      }
    }
  },
  {
    "description": "带参数的get请求,p1和p2是两个参数",
    "request": {
      "method": "get",
      "uri": "/reg",
      "queries": {
        "p1": "v1",
        "p2": "v2"
      }
    },
    "response": {
      "text": "带参数的get请求",
      "headers":{
        "Content-Type":"text/html;charset=utf-8"
      }
    }
  },
  {
    "description": "get请求返回json类型数据",
    "request": {
      "method": "get",
      "uri": "/login_json"
    },
    "response": {
      "json": {
        "key":"value",
        "请求方式是get":"响应结果为json类型"
      },
      "headers": {
        "Content-Type": "application/json;charset=utf-8"
      }
    }
  }
]

浏览器地址栏输入:

http://127.0.0.1:8888/login
http://127.0.0.1:8888/reg?p1=v1&p2=v2
http://127.0.0.1:8888/login_json



2.post接口实例

数据实例

[
  {
    "description": "post请求,请求参数为json格式,响应格式为json",
    "request": {
      "method": "post",
      "uri": "/post_json",
      "json": {
        "login_status": "successful"
      }
    },
    "response": {
      "json": {
        "login": "ok"
      },
      "headers": {
        "Content-Type": "application/json;charset=utf-8"
      }
    }
  },
  {
    "description": "post请求,请求及响应都为json,并且请求带cookies",
    "request": {
      "method": "post",
      "uri": "/post_cookie",
      "json": {
        "login_status": "successful"
      },
      "cookies":{
        "user_id":"xsdaqawea"
      }
    },
    "response": {
      "json": {
        "login": "ok"
      },
      "headers": {
        "Content-Type": "application/json;charset=utf-8"
      }
    }
  },
  {
    "description": "post请求,请求及响应都为json,并且请求带cookies和headers",
    "request": {
      "method": "post",
      "uri": "/post_cookie_headers",
      "json": {
        "login_status": "successful"
      },
      "cookies": {
        "user_id": "xsdaqawea"
      },
      "headers":{
        "Content-Type":"application/json"
      }
    },
    "response": {
      "json": {
        "login": "ok"
      },
      "headers": {
        "Content-Type": "application/json;charset=utf-8"
      }
    }
  },
  {
    "description": "post请求,请求和响应为form,入参是form形式,返回是json数据",
	"request": {
		"method": "post",
		"url": "/login_form",
		"forms": {
			"username": "zhangshan",
			"password": "123456"
		},
        "headers": {
        "content-type": "application/x-www-form-urlencoded"
		}
    },
	"response": {
		"json": {
			"error_code": 0,
			"reason": "successed",
			"username": "zhangshan",
			"checkstatus": "on"
		},
		"status": 200
	}
  }

]

注意:post中
headers:请求头,根据是form还是json格式的请求来填写
from格式:"content-type": "application/x-www-form-urlencoded"
json格式:"content-type": "application/json"
请求参数格式以及数据,对应headers的content-type
form格式关键字为forms
json格式关键字为json

post请求的不能直接通过地址访问,写个requests访问下数据吧
import requests
url1 = 'http://127.0.0.1:8888/post_json'
json1 = {"login_status": "successful"}
res1 = requests.post(url=url1, json=json1)

print(res1.json())  

url2 = 'http://127.0.0.1:8888/post_cookie'
cookies2 = {"user_id": "xsdaqawea"}
json2 = {"login_status": "successful"}

res2 = requests.post(url=url2, cookies=cookies2, json=json2)
print(res2.json())  

url3 = 'http://127.0.0.1:8888/post_cookie_headers'
cookies3 = {"user_id": "xsdaqawea"}
headers3 = {"Content-Type": "application/json"}
json3 = {"login_status": "successful"}

res3 = requests.post(url=url3,cookies=cookies3,headers=headers3,json=json3)
print(res3.json())

url4 = 'http://127.0.0.1:8888/login_form'
data4 = {"username": "zhangshan","password": "123456"}
headers4 = {"Content-Type": "application/x-www-form-urlencoded"}

res4 = requests.post(url=url4,data=data4,headers=headers4)
print(res4.json())

3.定制重定向

{
    "description":"重定向到指定网站",
    "request":{
      "method":"get",
      "uri":"/login_redirect"
    },
    "redirectTo":"https://www.baidu.com"
  }

浏览器地址栏输入:

http://127.0.0.1:8888/login_redirect
自动重定向到baidu

6.mock工作原理

posted @ 2019-12-04 14:08  张珊33  阅读(1112)  评论(2编辑  收藏  举报