自动化测试里面参数化思想是非常重要的,同一类型的测试,只需维护不同的数据就可以了。
/**
* Create by dell on 2021/4/9
* Author :wencheng
* describe :
* */
// 导入数据文件 example.json,并保存在 testData 变量中
import testDatas from '../data/example.json'
describe('接口数据驱动化', function () {
beforeEach("获取token", function () {
cy.token()
})
//遍历case
for (const data in testDatas) {
it(`${testDatas[data].casename}`, function () {
//获取token
var token = sessionStorage.getItem("Token")
var url = testDatas[data].url
var method = testDatas[data].method
var header = testDatas[data].headers
var body = testDatas[data].body
var status_code = testDatas[data].status_code
var header = JSON.stringify(header)
var body = JSON.stringify(body)
//发送请求
cy.requests2(url, method, header, body, token, status_code)
});
}
})
//封装http请求
Cypress.Commands.add("requests2", function (url, method, header, body, token, status_code) {
cy.log('测试地址: ' + url)
cy.log('请求参数: ' + body)
cy.log('请求方法: ' + method)
cy.log('请求头部: ' + header)
var headers = JSON.parse(header.replace('token', token))
if (method.toUpperCase() == "GET") {
cy.request({
url: url, method: method, headers: headers, body: body
}).then(function (resp) {
cy.log("返回结果:" + JSON.stringify(resp.body))
expect(resp.status).to.eq(status_code)
})
} else if (method.toUpperCase() == "POST") {
var randomName = require("chinese-random-name");
var randomname = randomName.names.get3()
var bodys = body.replace('randomname', randomname)
var body = JSON.parse(bodys)
cy.request({
url: url, method: method, headers: headers, body: body
}).then(function (resp) {
cy.log("返回结果:" + JSON.stringify(resp.body))
expect(resp.status).to.eq(status_code)
})
}
})
[
{
"casename": "获取配置列表信息",
"url": "/configures/",
"method": "GET",
"headers": {
"Content-Type": "application/json",
"authorization": "JWT token"
},
"body": "",
"status_code": 200
},
{
"casename": "获取项目(多个)列表数据",
"url": "/projects/",
"method": "GET",
"headers": {
"Content-Type": "application/json",
"authorization": "JWT token"
},
"body": "",
"status_code": 200
},
{
"casename": "返回所有项目ID和名称",
"url": "/projects/names/",
"method": "GET",
"headers": {
"Content-Type": "application/json",
"authorization": "JWT token"
},
"body": "",
"status_code": 200
},
{
"casename": "创建项目",
"url": "/projects/",
"method": "POST",
"headers": {
"Content-Type": "application/json",
"authorization": "JWT token"
},
"body": {"name": "randomname", "leader": "张三", "tester": "李四","programmer": "王五", "publish_app": "测测测", "desc": "randomdesc"},
"status_code": 201
}
]
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家的支持。
本文分享自 自动化测试 To share 微信公众号,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文参与 腾讯云自媒体同步曝光计划 ,欢迎热爱写作的你一起参与!