背景:后端接口测试。现在后端提供一个接口以及接口的入参格式约定。现在希望从前端去组合参数条件,生成全覆盖的参数列表,然后向后端发送请求。以达到测试接口的目的。
技术栈: node exprss
文件目录:
-FanTest
--config
---saleTarget.json
--index.js
--moudlues.json
--saleTarget.json
index.js
主入口文件
const request = require('request')
const fs = require('fs')
const modules = JSON.parse(fs.readFileSync('modules.json'))
const sendQuest = function(moduleName, apiName, params) {
return new Promise((resolve, reject) => {
let start = new Date()
request.post(
'http://localhost:1601/sps/' + moduleName + '/' + apiName,
{
form: params
},
function(error, response, body) {
let end = new Date()
if (!error && response.statusCode === 200) {
body = JSON.parse(body)
if (body.success) {
resolve([moduleName, apiName, ' - ok -', end - start + 'ms'].join('-'))
} else {
resolve([moduleName, apiName, ' - error -', end - start + 'ms'].join('-'))
}
}
}
)
})
}
for (let moduleName in modules) {
const file = moduleName + '.json'
const api = JSON.parse(fs.readFileSync(file))
for (let apiName in api) {
api[apiName].forEach(param => {
sendQuest(moduleName, apiName, param).then(rs => {
console.info(rs)
})
})
}
}
模块列表 modules.json
一个模块对应一个json文件
{
"saleTarget": "saleTarget"
}
某个模块的前部接口及参数名
{
"queryAdjustTableData": [
{
"dataDimensionList": "brand",
"idList": "7817,106554,22186",
"roleType": "DEPT_MANAGER",
"dept3Id": "1541",
"year": "2018",
"monthList": "1,2,3,4,5,6,7,8,9,10,11,12",
"dateDimension": "M",
"versionType": "assessment"
},
{
"dataDimensionList": "brand",
"idList": "7817,106554,22186",
"roleType": "DEPT_MANAGER",
"dept3Id": "1541",
"year": "2018",
"monthList": "1,2,3,4,5,6,7,8,9,10,11,12",
"dateDimension": "M",
"versionType": "assessment"
},
{
"dataDimensionList": "brand",
"idList": "7817,106554,22186",
"roleType": "DEPT_MANAGER",
"dept3Id": "1541",
"year": "2018",
"monthList": "1,2,3,4,5,6,7,8,9,10,11,12",
"dateDimension": "M",
"versionType": "assessment"
}
]
}
config/saleTarget.json
对某个模块的参数的配置
{
"queryAdjustTableData": [
{
"dataDimensionList": "brand",
"idList": "7817,106554,22186",
"roleType": {"type":"in","range":["DEPT_MANAGER","asdfsd"]},
"dept3Id":{"dataType":"number"},
"year": {"dataType":"number"},
"monthList": "1,2,3,4,5,6,7,8,9,10,11,12",
"dateDimension": {"type":"in","range":["DEPT_MANAGER","asdfsd"]},
"versionType": "assessment"
}
]
}
期望功能:
- 自动生成前端代码
- 自动向后端发请求,并验证访问结果。