在开发中,需要测试web-api的接口 spring mvc 使用单元测试非常方便,但是,受不了单元测试的启动速度。用python写了一个小脚本,用于测试接口,
测试脚本配置文件
api.yaml
server:
url: http://127.0.0.1:9000/ihome/
api:
name:
#api-v2-neighbor-list.yaml
- api/v2/neighbor/list
api-v2-neighbor-list.yaml
接口配置文件
method:
post
data:
#post 的 body 的json
postSid: a1
userSid: u2
python 脚本
import requests, json, yaml, sys
def apiTest(apiName):
f = open("api.yaml")
obj = yaml.safe_load(f)
f.close()
if apiName != "":
runApi(obj["server"]["url"] + apiName, apiName.replace("/", "-") + ".yaml")
return;
apis = obj['api']["name"]
for api in apis:
runApi(obj["server"]["url"] + api, api.replace("/", "-") + ".yaml")
def runApi(url, dataFile):
headers = {'Content-Type' : 'application/json; charset=UTF-8',
'X-Requested-With' : 'XMLHttpRequest',
'Connection' : 'keep-alive',
'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.110 Safari/537.36'
}