2.7 list接口代码示例

 一、接口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'}]},
]

三、结果截图

失败截图

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值