KJ3*_*KJ3 153
您可以通过在describe或it块前面放置一个x或在.skip其后放置一个来跳过测试.
xit('should work', function (done) {});
describe.skip('features', function() {});
Run Code Online (Sandbox Code Playgroud)
您也可以通过测试来运行单个.only测试.例如
describe('feature 1', function() {});
describe.only('feature 2', function() {});
describe('feature 3', function() {});
Run Code Online (Sandbox Code Playgroud)
在这种情况下,只有特征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,
}
}
}
Run Code Online (Sandbox Code Playgroud)
Gaj*_*jus 95
有一种无记录的以编程方式跳过测试的方法:
// test.js
describe('foo', function() {
before(function() {
this.skip();
});
it('foo', function() {
// will not run
console.log('This will not be printed');
});
});
Run Code Online (Sandbox Code Playgroud)
运行:
$ mocha test.js
foo
- foo
0 passing (9ms)
1 pending
Run Code Online (Sandbox Code Playgroud)
这将在https://github.com/mochajs/mocha/issues/1901中讨论.
dan*_*y74 34
这个答案适用于ES6.
代替:
describe('your describe block', () => {
Run Code Online (Sandbox Code Playgroud)
你要:
(condition ? describe : describe.skip)('your describe block', () => {
Run Code Online (Sandbox Code Playgroud)
如果条件为假,这有条件地跳过描述块中的所有测试.
或者,而不是:
it('your it block', () => {
Run Code Online (Sandbox Code Playgroud)
你要:
(condition ? it : it.skip)('your it block', () => {
Run Code Online (Sandbox Code Playgroud)
如果条件为假,这有条件地跳过一次测试.
Ami*_*.io 32
我使用摩卡运行时跳过与您描述的相同的场景.它是来自文档的复制粘贴:
it('should only test in the correct environment', function() {
if (/* check test environment */) return this.skip();
// make assertions
});
Run Code Online (Sandbox Code Playgroud)
如您所见,它会根据环境跳过测试.我自己的条件是if(process.env.NODE_ENV === 'continuous-integration').
这取决于您希望以编程方式跳过测试.如果在运行任何测试代码之前可以确定跳过的条件,那么您可以根据条件调用it或it.skip根据需要调用.例如,如果环境变量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!");
});
});
Run Code Online (Sandbox Code Playgroud)
如果您要检查的条件只能在测试时确定,则会更复杂一些.如果您不想访问任何严格来说不是测试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!");
});
});
Run Code Online (Sandbox Code Playgroud)
虽然我的第一个例子是将测试标记为正式跳过(也称为"待定"),但我刚才显示的方法将避免执行实际测试,但测试不会被标记为正式跳过.它们将被标记为已通过.如果您绝对想要跳过它们,我不知道是否有任何方法无法访问未正确说出测试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!");
});
});
Run Code Online (Sandbox Code Playgroud)
describe.skip或it.skipdescribe('Array', function() {
it.skip('#indexOf', function() {
// ...
});
});
Run Code Online (Sandbox Code Playgroud)
describe.only或it.onlydescribe('Array', function() {
it.only('#indexOf', function() {
// ...
});
});
Run Code Online (Sandbox Code Playgroud)
有关更多信息,请访问https://mochajs.org/#inclusive-tests
我不确定这是否符合“程序化跳过”的条件,但为了有选择地跳过我们 CI 环境的某些特定测试,我使用了 Mocha 的标记功能(https://github.com/mochajs/mocha/wiki/Tagging)。在describe()或it()消息中,您可以添加像@no-ci 这样的标签。要排除这些测试,您可以在 package.json 中定义特定的“ci 目标”并使用--grep和--invert参数,例如:
"scripts": {
"test": "mocha",
"test-ci" : "mocha --reporter mocha-junit-reporter --grep @no-ci --invert"
}
Run Code Online (Sandbox Code Playgroud)
我们可以编写一个漂亮的干净包装函数来有条件地运行测试,如下所示:
function ifConditionIt(title, test) {
// Define your condition here
return condition ? it(title, test) : it.skip(title, test);
}
Run Code Online (Sandbox Code Playgroud)
然后可以在您的测试中需要并使用它,如下所示:
ifConditionIt('Should be an awesome test', (done) => {
// Test things
done();
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
74355 次 |
| 最近记录: |