包容性测试

优质
小牛编辑
129浏览
2023-12-01

此功能与之相反.only()。通过附加.skip(),您可以告诉Mocha简单地忽略这些套件和测试用例。跳过的任何内容都将被标记为待处理,并按此报告。这是跳过整个套件的示例:

describe('Array', function() {
describe.skip('#indexOf()', function() {
  // ...
});
});

或者特定的测试用例:

describe('Array', function() {
describe('#indexOf()', function() {
  it.skip('should return -1 unless present', function() {
    // this test will not be run
  });
  it('should return the index when present', function() {
    // this test will be run
  });
});
});

最佳实践:使用.skip()而不是评论测试。

您也可以在运行时跳过使用this.skip()。如果测试需要预先无法检测到的环境或配置,则运行时跳过是合适的。例如:

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

上述测试将报告为待定。同样重要的是要注意调用this.skip()将有效中止测试。

Best practice: To avoid confusion, do not execute further instructions in a test or hook after calling this.skip().

Contrast the above test with the following code:

it('should only test in the correct environment', function() {
if (/* check test environment */) {
  // make assertions
} else {
  // do nothing
}
});

Because this test does nothing, it will be reported as passing.

Best practice: Don’t do nothing! A test should make an assertion or use this.skip().

To skip multiple tests in this manner, use this.skip() in a “before” hook:

before(function() {
if (/* check test environment */) {
  // setup code
} else {
  this.skip();
}
});

Before Mocha v3.0.0, this.skip() was not supported in asynchronous tests and hooks.