我有一个代码,其中某些测试在CI环境中总是失败。我想根据环境条件禁用它们。
如何在运行时执行期间以编程方式跳过mocha测试?
我有一个代码,其中某些测试在CI环境中总是失败。我想根据环境条件禁用它们。
如何在运行时执行期间以编程方式跳过mocha测试?
当前回答
您可以通过在describe或it块前面放置x或在其后放置.skip来跳过测试。
xit('should work', function (done) {});
describe.skip('features', function() {});
您还可以通过在测试上放置.only来运行单个测试。例如
describe('feature 1', function() {});
describe.only('feature 2', function() {});
describe('feature 3', function() {});
在这种情况下,只有特征2块会运行。
似乎没有一种通过编程方式跳过测试的方法,但是您可以在beforeEach语句中进行某种检查,并且只在设置了标志的情况下运行测试。
beforeEach(function(){
if (wrongEnvironment){
runTest = false
}
}
describe('feature', function(){
if(runTest){
it('should work', function(){
// Test would not run or show up if runTest was false,
}
}
}
其他回答
您可以通过在describe或it块前面放置x或在其后放置.skip来跳过测试。
xit('should work', function (done) {});
describe.skip('features', function() {});
您还可以通过在测试上放置.only来运行单个测试。例如
describe('feature 1', function() {});
describe.only('feature 2', function() {});
describe('feature 3', function() {});
在这种情况下,只有特征2块会运行。
似乎没有一种通过编程方式跳过测试的方法,但是您可以在beforeEach语句中进行某种检查,并且只在设置了标志的情况下运行测试。
beforeEach(function(){
if (wrongEnvironment){
runTest = false
}
}
describe('feature', function(){
if(runTest){
it('should work', function(){
// Test would not run or show up if runTest was false,
}
}
}
我们在测试环境中有一些不可靠的测试,有时会使用以下方法关闭这些测试:
mocha --config ./config/parallelrc.cjs --parallel --jobs 3 -- tests/spec/**/index.js -g @flaky -i
我们在测试描述中标记flaky测试@flaky,并设置特殊的-g规则,这意味着mocha只运行带有@flaky标签的测试,接下来使用-i -它意味着反转,因此mocha只运行测试而不是@flaky。
所以,我认为这对你很有用)
这并不是真正使用摩卡的功能,而是调整它以获得我想要的行为。
我想在量角器摩卡测试中跳过所有后续的“it’s”,但有一个“it’s”失败了。这是因为一旦旅程测试的一个步骤失败,几乎可以肯定其他步骤也会失败,并且可能需要很长时间,如果他们使用浏览器等待元素出现在页面上等,可能会占用构建服务器。
当只是运行标准摩卡测试(不是量角器)时,这可以通过全局beforeEach和afterEach钩子来实现,在测试的父(描述)上附加一个' skipfollow '标志,如下所示:
beforeEach(function() {
if(this.currentTest.parent.skipSubsequent) {
this.skip();
}
});
afterEach(function() {
if (this.currentTest.state === 'failed') {
this.currentTest.parent.skipSubsequent = 'true'
}
})
当尝试使用量角器和摩卡时,“this”的范围发生了变化,上面的代码不起作用。你最终会得到一个错误消息,比如'error calling done()',量角器会停止。
相反,我最终得到了下面的代码。不是最漂亮的,但它最终用this.skip()替换了其余测试函数的实现。如果/当摩卡的内部结构在后续版本中发生变化时,这可能会停止工作。
这是通过调试和检查摩卡内部的一些试验和错误发现的……不过,当测试失败时,有助于更快地完成浏览器测试套件。
beforeEach(function() {
var parentSpec = this.currentTest.parent;
if (!parentSpec.testcount) {
parentSpec.testCount = parentSpec.tests.length;
parentSpec.currentTestIndex = 0;
} else {
parentSpec.currentTestIndex = parentSpec.currentTestIndex + 1;
}
if (parentSpec.skipSubsequent) {
parentSpec.skipSubsequent = false;
var length = parentSpec.tests.length;
var currentIndex = parentSpec.currentTestIndex;
for (var i = currentIndex + 1; i < length; i++) {
parentSpec.tests[i].fn = function() {
this.skip();
};
}
}
});
afterEach(function() {
if (this.currentTest.state === 'failed') {
this.currentTest.parent.skipSubsequent = 'true'
}
});
mocha test/ --grep <pattern>
https://mochajs.org/
您可以使用我的包mocha-assume以编程方式跳过测试,但只能从测试之外跳过。你可以这样使用它:
assuming(myAssumption).it("does someting nice", () => {});
Mocha-assume只会在myAssumption为真时运行你的测试,否则它会跳过它(使用它。skip)并传递一个好的消息。
下面是一个更详细的例子:
describe("My Unit", () => {
/* ...Tests that verify someAssuption is always true... */
describe("when [someAssumption] holds...", () => {
let someAssumption;
beforeAll(() => {
someAssumption = /* ...calculate assumption... */
});
assuming(someAssumption).it("Does something cool", () => {
/* ...test something cool... */
});
});
});
以这种方式使用它,可以避免级联故障。当某些假设不成立时,测试“do something cool”总是会失败——但这个假设已经在上面测试过了(在验证某些假设总是正确的测试中)。
因此,测试失败不会给您任何新信息。事实上,它甚至是一个假阳性:测试失败并不是因为“一些很酷的东西”没有工作,而是因为测试的先决条件没有得到满足。使用摩卡,假设你经常可以避免这种误报。