使用Mocha Chai并行运行测试服

时间:2019-06-19 07:01:04

标签: node.js mocha performance-testing chai

在这种情况下,我需要动态生成describe块,并使所有测试套件并行运行。我已经尝试过使用mocha parallelmocha-parallel-tests,但是它不是并行进行的。我在下面附上了我的代码段。请看一下,给我这个用​​例的解决方案。

const common = require("./common.test");
global.fetch = require('node-fetch');
var config = require('./config');
var chai = require("chai"); ''
var chaiHttp = require('chai-http');
var parallel = require('async-parallel');
chai.use(chaiHttp);
var expect = chai.expect;

//generate Users Accounts
let arrayIndex = 0;
var creadentialsArray = Array(config.numberOfUsers).fill('').map((user, index) => {
    let data = JSON.parse(JSON.stringify(config.userCredentials[arrayIndex]));
    data.sessionId = index + 1;
    if (arrayIndex + 1 === config.userCredentials.length) {
        arrayIndex = 0;
    } else {
        arrayIndex++;
    }
    return data;
});


parallel.each(creadentialsArray, async (loginDetails) => {

    describe(`Session ${loginDetails.sessionId}`, () => {
        let token = "";


        let should = chai.should();
        before(async () => {
            console.log(new Date(), " Session start time");

        });
        beforeEach(done => {
            console.log("delay for each API")
            setTimeout(done, config.interAPIDelay)
        });


        // let d1 = new Date(),
        //     d2 = new Date();
        // d2 = d2.setMinutes(d1.getMinutes() + 30).toISOString();

        // var timer = setInterval(() => {
        Array(config.loopCount).fill('').forEach((iteration, index) => {

            console.log('loop count ', index);
            it("get status", async () => {

                //get token
            console.log(new Date(), "first API start time");

                token = await common.userAuthentication(loginDetails);
                // console.log(token);
                chai.request(config.API_BASE)
                    .post(`api/USER1@DUKE.AI/status`)
                    .set("Authorization", token)
                    .send({ "from": "2019-06-03", "to": "2019-03-03", "num_docs": "" })
                    .then(function (res) {
                        console.log(new Date(), " end time");
                        expect(res).to.have.status(200);
                    });
            });

            it("promo code checking", async () => {
                // console.log("username ", loginDetails.Username);
                chai.request(config.API_BASE)
                    .post(`api/promocodecheck`)
                    .send({ "promoCode": `craig@duke-ai.com` })
                    .then(function (res) {
                        // console.log(new Date(), " end time");
                        expect(res).to.have.status(200);
                    });
            });
        })
        //     if(new Date().toISOString === d2) {
        //         clearInterval(timer);
        //     }
        // },10)
    });
})

0 个答案:

没有答案