独家测试

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

排他性功能允许您通过附加到函数来运行指定的套件或测试用例.only()。这是仅执行特定套件的示例:

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

注意:仍将执行所有嵌套套件。

以下是执行单个测试用例的示例:

describe('Array', function() {
describe('#indexOf()', function() {
  it.only('should return -1 unless present', function() {
    // ...
  });
  it('should return the index when present', function() {
    // ...
  });
});
});

在v3.0.0之前,.only()使用字符串匹配来决定执行哪些测试。从v3.0.0开始,情况就不再如此。在v3.0.0或更高版本中,.only()可以多次使用来定义要运行的测试子集:

describe('Array', function() {
describe('#indexOf()', function() {
  it.only('should return -1 unless present', function() {
    // this test will be run
  });
  it.only('should return the index when present', function() {
    // this test will also be run
  });
  it('should return -1 if called with a non-Array context', function() {
    // this test will not be run
  });
});
});

您也可以选择多个套房:

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

测试将优先

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

注意:钩子(如果存在)仍将执行。

注意不要使用.only()版本控制的用法,除非你真的是这个意思!为此,可以使用--forbid-only持续集成测试命令(或git precommit hook)中的选项运行mocha 。