一、接口list示例
/**
* 首页接口用例
* @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 defaults = require('../../config/global/defaults');
const path = require('../../config/global/path');
const addContext = require('mochawesome/addContext');
const utilFormat = require('../../utils/format');
const homePageCase = require('../../config/case/homepagecase');
const compare = require('../../utils/compare');
describe('home', function () {
let obj;
beforeEach(function () {
obj = null;
})
afterEach(function () {
addContext(this, utilFormat.add(obj));
});
context('homeList', function () {
it('current', async function () {
let name = this._runnable.title;
await axios.get(path.homePagePathList.current, {
headers: {'Cookie': defaults.session}
})
.then(function (res) {
obj = res;
for (let i of homePageCase.homePage) {
if (i['requestName'] === name) {
compare.compare(i['res'], res.data);
}
}
})
.catch(function (error) {
should.ifError(error);
})
});
it('month', async function () {
let name = this._runnable.title;
await axios.get(path.homePagePathList.month_statistics, {
headers: {'Cookie': defaults.session}
})
.then(function (res) {
obj = res;
for (let i of homePageCase.homePage) {
if (i['requestName'] === name) {
compare.compare(i['res'], res.data);
}
}
})
.catch(function (error) {
should.ifError(error);
})
});
it('result', async function () {
let name = this._runnable.title;
await axios.get(path.homePagePathList.result, {headers: {'Cookie': defaults.session}})
.then(function (res) {
obj = res;
for (let i of homePageCase.homePage) {
if (i['requestName'] === name) {
compare.compare(i['res'], res.data);
}
}
})
.catch(function (error) {
should.ifError(error);
})
});
it('statistics', async function () {
let name = this._runnable.title;
await axios.get(path.homePagePathList.statistics, {headers: {'Cookie': defaults.session}})
.then(function (res) {
obj = res;
for (let i of homePageCase.homePage) {
if (i['requestName'] === name) {
compare.compare(i['res'], res.data);
}
}
})
.catch(function (error) {
should.ifError(error);
})
});
it('task', async function () {
let name = this._runnable.title;
await axios.get(path.homePagePathList.task_statistics, {headers: {'Cookie': defaults.session}})
.then(function (res) {
obj = res;
for (let i of homePageCase.homePage) {
if (i['requestName'] === name) {
compare.compare(i['res'], res.data);
}
}
})
.catch(function (error) {
should.ifError(error);
})
});
it('year', async function () {
let name = this._runnable.title;
await axios.get(path.homePagePathList.year_statistics, {headers: {'Cookie': defaults.session}})
.then(function (res) {
obj = res;
for (let i of homePageCase.homePage) {
if (i['requestName'] === name) {
compare.compare(i['res'], res.data);
}
}
})
.catch(function (error) {
should.ifError(error);
})
});
})
})
二、运行后,会对case中的预期进行比对,没有匹配上的会抛出断言错误
/**
* 首页接口case
* res数组中可以配置接口预期,预期值可以放在一个对象里,也可以分多个对象存放
* @type {[{requestName: string, res: [{userId: string}]}, {requestName: string, res: [{code: string}]}, {requestName: string, res: [{code: string}]}, {requestName: string, res: [{code: string}]}, {requestName: string, res: [{code: string}]}, null]}
*/
exports.homePage = [
{requestName: 'current', res: [{userId: 'test'}]},
{requestName: 'month', res: [{code: '0000'}]},
{requestName: 'result', res: [{code: '0000'}]},
{requestName: 'statistics', res: [{code: '0000'}]},
{requestName: 'task', res: [{code: '0000'}]},
{requestName: 'year', res: [{code: '0000'}]},
]
三、结果截图
失败截图