我有一个代码,其中某些测试在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,
         }
    }
}

其他回答

您可以使用我的包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”总是会失败——但这个假设已经在上面测试过了(在验证某些假设总是正确的测试中)。

因此,测试失败不会给您任何新信息。事实上,它甚至是一个假阳性:测试失败并不是因为“一些很酷的东西”没有工作,而是因为测试的先决条件没有得到满足。使用摩卡,假设你经常可以避免这种误报。

我不确定这是否可以称为“程序性跳过”,但是为了有选择性地跳过我们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"
}

可根据情况使用, 例如,声明一个var,当条件失败时,使用this.skip();

注意skip()在箭头函数中不起作用

let shouldRun: boolean;

before(function(){
if ($('#nsErrorIframe').isDisplayed()) {
        driver.switchToFrame($('#nsErrorIframe'));
        if ($('.ns-error-wrapper').isDisplayed()) {
            console.log('PAGE IS NOT AVAILABLE');
            shouldRun = false;
            if ( shouldRun === false) {
                 this.skip();
                }
                }
  }
});

正如@danielstjules在这里回答的,有一种跳过测试的方法。@本主题的作者从github.com mochajs的讨论中复制了答案,但没有关于哪个版本的mocha可用的信息。

我正在使用咕哝摩卡测试模块集成摩卡测试功能在我的项目。跳转到最后一个(现在)版本- 0.12.7带来了mocha 2.4.5版本,实现了this.skip()。

在package。json中

  "devDependencies": {
    "grunt-mocha-test": "^0.12.7",
    ...

然后

npm install

这个钩子让我很开心:

describe('Feature', function() {

    before(function () {

        if (!Config.isFeaturePresent) {

            console.log('Feature not configured for that env, skipping...');
            this.skip();
        }
    });
...

    it('should return correct response on AB', function (done) {

        if (!Config.isABPresent) {

           return this.skip();
        }

        ...

我们可以编写一个整洁的包装器函数来有条件地运行测试,如下所示:

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();
});