我希望在mocha中多次执行相同的测试用例,在测试的代码块之前,将设置执行条件的数量。
下面是我的示例代码:
describe('describe block ',function() {
var a;
before('before hook', function () {
a =[1,2,3,4];
console.log('outside before '+allorders);
})
a.forEach(()=>{
it("Check it", function () {
console.log('HI');
});
});
});我如何实现这一点?
发布于 2021-04-08 13:35:28
现在我要做的是将我的测试包装成一个函数,然后使用输入运行该测试。一个非常简单的例子如下所示
const { expect } = require("chai")
const myTest = (input) => {
it('will do something with the input', () => {
expect(input).to.be.true;
});
}
describe('all my tests', () => {
[true,false,true].forEach(val => {
myTest(val)
})
});如果您想在“前”块中设置数组,则如下所示
const { expect } = require("chai");
let testCases;
const myTest = (input) => {
it('will do something with the input', () => {
expect(input).to.be.true;
});
}
describe('all my tests', () => {
before(() => {
testCases = [true, false, true];
});
testCases.forEach(val => {
myTest(val)
})
});在这种情况下,测试将运行3次,并检查输入是否为真。所以它会在第一次和最后一次失败后第二次失败。这是一个很小的例子,但我认为它澄清了你如何完成你想要做的事情
https://stackoverflow.com/questions/67002513
复制相似问题