我有一个代码,其中某些测试在CI环境中总是失败。我想根据环境条件禁用它们。

如何在运行时执行期间以编程方式跳过mocha测试?


当前回答

这取决于您希望如何以编程方式跳过测试。如果在运行任何测试代码之前可以确定跳过的条件,那么您可以直接调用它或它。根据需要,根据条件跳过。例如,如果环境变量ONE被设置为任何值,这将跳过一些测试:

var conditions = {
    "condition one": process.env["ONE"] !== undefined
    // There could be more conditions in this table...
};

describe("conditions that can be determined ahead of time", function () {
    function skip_if(condition, name, callback) {
        var fn = conditions[condition] ? it.skip: it;
        fn(name, callback);
    };

    skip_if("condition one", "test one", function () {
        throw new Error("skipped!");
    });

    // async.
    skip_if("condition one", "test one (async)", function (done) {
        throw new Error("skipped!");
    });

    skip_if("condition two", "test two", function () {
        console.log("test two!");
    });

});

如果您想要检查的条件只能在测试时确定,那就有点复杂了。如果你不想访问任何严格来说不是测试API的部分,那么你可以这样做:

describe("conditions that can be determined at test time", function () {
    var conditions = {};
    function skip_if(condition, name, callback) {
        if (callback.length) {
            it(name, function (done) {
                if (conditions[condition])
                    done();
                else
                    callback(done);
            });
        }
        else {
            it(name, function () {
                if (conditions[condition])
                    return;
                callback();
            });
        }
    };

    before(function () {
        conditions["condition one"] = true;
    });

    skip_if("condition one", "test one", function () {
        throw new Error("skipped!");
    });

    // async.
    skip_if("condition one", "test one (async)", function (done) {
        throw new Error("skipped!");
    });

    skip_if("condition two", "test two", function () {
        console.log("test two!");
    });

});

虽然我的第一个例子是将测试标记为正式跳过(又名“pending”),但我刚才展示的方法只是避免执行实际的测试,但测试不会标记为正式跳过。他们将被标记为通过。如果你真的想跳过它们,我不知道有什么方法可以访问不是测试API的部分:

describe("conditions that can be determined at test time", function () {
    var condition_to_test = {}; // A map from condition names to tests.
    function skip_if(condition, name, callback) {
        var test = it(name, callback);
        if (!condition_to_test[condition])
            condition_to_test[condition] = [];
        condition_to_test[condition].push(test);
    };

    before(function () {
        condition_to_test["condition one"].forEach(function (test) {
            test.pending = true; // Skip the test by marking it pending!
        });
    });

    skip_if("condition one", "test one", function () {
        throw new Error("skipped!");
    });

    // async.
    skip_if("condition one", "test one (async)", function (done) {
        throw new Error("skipped!");
    });

    skip_if("condition two", "test two", function () {
        console.log("test two!");
    });

});

其他回答

这个答案确实适用于ES6。

而不是:

describe('your describe block', () => {

你想要的:

(condition ? describe : describe.skip)('your describe block', () => {

如果条件为假,这将有条件地跳过描述块中的所有测试。

或者,代替:

it('your it block', () => {

你想要的:

(condition ? it : it.skip)('your it block', () => {

如果条件为假,则有条件地跳过一个测试。

对于您所描述的相同场景,我使用Mocha的运行时跳过。它是从文档中复制粘贴的:

it('should only test in the correct environment', function() {
  if (/* check test environment */) return this.skip();

  // make assertions
});

如您所见,它跳过了基于环境的测试。我自己的条件是if(process.env。NODE_ENV === '持续集成')。

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

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

可根据情况使用, 例如,声明一个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();
                }
                }
  }
});