我有一个代码,其中某些测试在CI环境中总是失败。我想根据环境条件禁用它们。
如何在运行时执行期间以编程方式跳过mocha测试?
我有一个代码,其中某些测试在CI环境中总是失败。我想根据环境条件禁用它们。
如何在运行时执行期间以编程方式跳过mocha测试?
当前回答
要跳过测试,请使用describe。Skip还是it.skip
describe('Array', function() {
it.skip('#indexOf', function() {
// ...
});
});
以包括您可以使用描述的测试。Only或it.only
describe('Array', function() {
it.only('#indexOf', function() {
// ...
});
});
更多信息请访问https://mochajs.org/#inclusive-tests
其他回答
我们在测试环境中有一些不可靠的测试,有时会使用以下方法关闭这些测试:
mocha --config ./config/parallelrc.cjs --parallel --jobs 3 -- tests/spec/**/index.js -g @flaky -i
我们在测试描述中标记flaky测试@flaky,并设置特殊的-g规则,这意味着mocha只运行带有@flaky标签的测试,接下来使用-i -它意味着反转,因此mocha只运行测试而不是@flaky。
所以,我认为这对你很有用)
我不确定这是否可以称为“程序性跳过”,但是为了有选择性地跳过我们CI环境中的某些特定测试,我使用了Mocha的标记功能(https://github.com/mochajs/mocha/wiki/Tagging)。 在describe()或it()消息中,可以添加@no-ci这样的标记。要排除这些测试,可以在包中定义特定的“ci目标”。使用——grep和——invert参数,如下:
"scripts": {
"test": "mocha",
"test-ci" : "mocha --reporter mocha-junit-reporter --grep @no-ci --invert"
}
要跳过测试,请使用describe。Skip还是it.skip
describe('Array', function() {
it.skip('#indexOf', function() {
// ...
});
});
以包括您可以使用描述的测试。Only或it.only
describe('Array', function() {
it.only('#indexOf', function() {
// ...
});
});
更多信息请访问https://mochajs.org/#inclusive-tests
对于您所描述的相同场景,我使用Mocha的运行时跳过。它是从文档中复制粘贴的:
it('should only test in the correct environment', function() {
if (/* check test environment */) return this.skip();
// make assertions
});
如您所见,它跳过了基于环境的测试。我自己的条件是if(process.env。NODE_ENV === '持续集成')。
我们可以编写一个整洁的包装器函数来有条件地运行测试,如下所示:
function ifConditionIt(title, test) {
// Define your condition here
return condition ? it(title, test) : it.skip(title, test);
}
然后可以在您的测试中要求并按以下方式使用:
ifConditionIt('Should be an awesome test', (done) => {
// Test things
done();
});