一、mocha自动化可以通过this.ctx来传递
也可以通过外层的变量来传递
示例
/**
* 正则接口用例
* @type {AxiosStatic | {CancelTokenSource: CancelTokenSource, CancelStatic: CancelStatic, AxiosProxyConfig: AxiosProxyConfig, Canceler: Canceler, AxiosStatic: AxiosStatic, AxiosRequestConfig: AxiosRequestConfig, AxiosTransformer: AxiosTransformer, Cancel: Cancel, AxiosInstance: AxiosInstance, AxiosError: AxiosError, Method: Method, AxiosPromise: AxiosPromise, CancelTokenStatic: CancelTokenStatic, AxiosBasicCredentials: AxiosBasicCredentials, ResponseType: ResponseType, CancelToken: CancelToken, AxiosInterceptorManager: AxiosInterceptorManager, AxiosResponse: AxiosResponse, AxiosAdapter: AxiosAdapter, readonly default: AxiosStatic}}
*/
const axios = require('axios');
const path = require('../../config/global/path');
const regularCase = require('../../config/case/regularcase');
const utilFormat = require('../../utils/format');
const addContext = require('mochawesome/addContext');
const defaults = require('../../config/global/defaults');
const compare = require('../../utils/compare');
const random = require('../../utils/random');
const check = require('../../utils/checkchinese');
describe('regexp', function () {
let obj;
beforeEach(function () {
obj = null;
})
afterEach(function () {
addContext(this, utilFormat.add(obj));
});
context('regexpList', function () {
it('list', async function () {
let temp;
let name = this._runnable.title;
await axios.get(path.regularPathList.list, {headers: {'Cookie': defaults.session}})
.then(function (res) {
obj = res;
temp = res.data.data;
for (let j of regularCase.regexp) {
if (j['requestName'] === name) {
compare.compare(j['res'], res.data);
}
}
})
.catch(function (error) {
should.ifError(error);
})
this.ctx = temp;
})
for (let i of regularCase.parmsData) {
let tempId;
it('save', async function () {
let name = this._runnable.title;
if (this.ctx === [] || this.ctx === undefined) {
should.be.false('list: Response faild, regexp list may be incorrect')
}
if (i.regExpType === 'INNER') {
i.regExpInfo = this.ctx[random.random(this.ctx.length)].regExpInfo;
i.innerRegExpType = this.ctx[random.random(this.ctx.length)].innerRegExpType;
}
await axios.post(path.regularPathList.save, i, {headers: {'Cookie': defaults.session}})
.then(function (res) {
obj = res;
tempId = res.data.data;
for (let j of regularCase.regexp) {
if (j['requestName'] === name) {
compare.compare(j['res'], res.data);
}
}
})
.catch(function (error) {
should.ifError(error);
})
})
it('edit', async function () {
let name = this.runnable.title;
if (check.check(tempId) === true) {
true.should.be.false('save: Response failed,the value of ID contains chinese')
}
let url = path.regularPathList.save + '/' + tempId;
let data = {};
for (let j of regularCase.editData) {
if (i.regExpType === j.regExpType && i.regExpType === 'INNER') {
j.regExpInfo = this.ctx[random.random(this.ctx.length)].regExpInfo;
j.innerRegExpType = this.ctx[random.random(this.ctx.length)].innerRegExpType;
j.regExpName = i.regExpName;
data = j;
break;
}
if (i.regExpType === j.regExpType && i.regExpType === 'CUSTOM') {
j.regExpName = i.regExpName;
data = j;
break;
}
}
data.id = tempId;
await axios.put(url, data, {headers: {'Cookie': defaults.session}})
.then(function (res) {
obj = res;
for (let j of regularCase.regexp) {
if (j['requestName'] === name) {
compare.compare(j['res'], res.data);
}
}
})
.catch(function (error) {
should.ifError(error);
})
})
it('get', async function () {
let name = this.runnable.title;
let url = path.regularPathList.get + encodeURI(i.regExpName);
await axios.get(url, {headers: {'Cookie': defaults.session}})
.then(function (res) {
obj = res;
for (let j of regularCase.regexp) {
if (j['requestName'] === name) {
compare.compare(j['res'], res.data);
}
}
})
.catch(function (error) {
should.ifError(error);
})
})
it('delete', async function () {
let name = this._runnable.title;
let url = path.regularPathList.save + '/' + tempId;
await axios.delete(url, {headers: {'Cookie': defaults.session}})
.then(function (res) {
obj = res;
for (let j of regularCase.regexp) {
if (j['requestName'] === name) {
compare.compare(j['res'], res.data);
}
}
})
.catch(function (error) {
should.ifError(error);
})
})
}
})
})
代码中定义了tempId变量和temp变量,temp传递给了this.ctx,而tempId是直接在下面接口中进行引用了
接口的调用顺序上,只要把删除接口放到最后就行,这样就能覆盖新增、编辑、查询、删除接口了
本文介绍了使用Mocha和axios进行接口自动化测试的方法,涉及正则表达式接口的增删改查操作。通过beforeEach和afterEach设置上下文,利用this.ctx传递数据,并在不同测试用例间共享变量。测试流程包括list、save、edit、get和delete接口,确保接口功能完整覆盖。
86





