如何以编程方式跳过摩卡testing?

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

如何以编程方式在运行时执行期间跳过mocha中的testing?

你可以跳过testing,把一个x放在describe或it块的前面,或者把一个.skip放在它后面。

 xit('should work', function (done) {}); describe.skip('features', function() {}); 

您也可以通过在testing中放置一个.only来运行单个testing。 例如

 describe('feature 1', function() {}); describe.only('feature 2', function() {}); describe('feature 3', function() {}); 

在这种情况下,只有特征2块会运行。

似乎没有一种以编程方式跳过testing的方法,但是您可以在beforeEach语句中进行某种检查,并且只在设置标志时才运行testing。

 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, } } } 

有一种无法通过程序跳过testing的方式:

 $ cat test.js describe('foo', function() { before(function() { this.skip(); }); it('foo', function() { // will not run console.log('This will not be printed'); }); }); $ mocha test.js foo - foo 0 passing (9ms) 1 pending 

这在https://github.com/mochajs/mocha/issues/1901中讨论。;

这个答案适用于ES6

代替:

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

你要:

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

如果条件为假,则有条件地跳过描述块中的所有testing。

或者,而不是:

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

你要:

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

如果条件是错误的,这有条件地跳过一个testing。

这取决于如何以编程方式跳过testing。 如果在运行任何testing代码之前可以确定跳过条件,则可以根据需要根据需要调用itit.skip 。 例如,如果环境variablesONE被设置为任何值,这将跳过一些testing:

 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!"); }); }); 

如果你想检查的条件只能在testing时间确定,那就更复杂了。 如果你不想访问任何不严格地说是testingAPI的一部分,那么你可以这样做:

 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!"); }); }); 

尽pipe我的第一个例子是将testing标记为正式跳过(又名“挂起”),但是我刚刚展示的方法只是避免执行实际testing,但testing不会被标记为正式跳过。 他们将被标记为通过。 如果你绝对想让他们跳过,我不知道有什么方法可以访问那些不正确地说是testingAPI的一部分的部分:

 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!"); }); }); 

正如你所描述的,我使用Mocha的运行时跳过相同的场景。 这是来自文档的复制粘贴:

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

正如你所看到的,它会跳过基于环境的testing。 我自己的条件是if(process.env.NODE_ENV === 'continuous-integration')

您可以使用我的包mocha-assume以编程方式跳过testing,但只能从testing之外跳过。 你这样使用它:

 assuming(myAssumption).it("does someting nice", () => {}); 

摩卡假设只会运行你的testingmyAssumptiontrue ,否则会跳过它(使用it.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... */ }); }); }); 

以这种方式使用它,可以避免级联失败。 假设"Does something cool"假设不成立,那么testing"Does something cool"总是会失败 – 但是这个假设已经在上面进行了testing(在Tests that verify someAssuption is always true" )。

所以testing失败不会给你任何新的信息。 事实上,这甚至是一个假阳性:testing并没有失败,因为“有些酷”没有奏效,而是因为testing的先决条件并不令人满意。 与mocha-assume你可以经常避免这种误报。

说我想跳过我的参数化testing,如果我的testing描述包含string“富”,我会这样做:

 // Skip parametrized test if description contains the string "foo" (test.description.indexOf("foo") === -1 ? it : it.skip)("should test something", function (done) { // Code here }); // Parametrized tests describe("testFoo", function () { test({ description: "foo" // This will skip }); test({ description: "bar" // This will be tested }); }); 

就你而言,我相信如果你想检查环境variables,你可以使用NodeJS的:

 process.env.ENV_VARIABLE 

例如(警告:我没有testing这一点的代码!),也许是这样的:

 (process.env.NODE_ENV.indexOf("prod") === -1 ? it : it.skip)("should...", function(done) { // Code here }); 

您可以将ENV_VARIABLE设置为无论您正在closures哪个位置,然后使用该值跳过或运行testing。 (仅供参考,NodeJS的process.env的文档在这里: https : //nodejs.org/api/process.html#process_process_env )

我不会完全相信这个解决scheme的第一部分,我find并testing了答案,并且完美地通过这个资源跳过基于简单条件的testing: https : //github.com/mochajs/mocha/issues / 591

希望这可以帮助! 🙂

这不是真正使用摩卡的function,而是调整它来获得我想要的行为。

我想在我的量angular器摩卡testing中跳过任何后续的“它”,一个“失败”。 这是因为一旦旅程testing的一步失败了,几乎可以肯定的是其余的将会失败,如果使用浏览器等待元素出现在页面上,可能需要很长时间才能生成构build服务器。

当运行标准的摩卡testing(不是量angular器)时,可以通过在全局beforeEach和afterEach挂钩上附加一个'skipSubsequent'标志到testing的父(describe)来实现,如下所示:

  beforeEach(function() { if(this.currentTest.parent.skipSubsequent) { this.skip(); } }); afterEach(function() { if (this.currentTest.state === 'failed') { this.currentTest.parent.skipSubsequent = 'true' } }) 

当使用量angular器和摩卡来尝试这个时,“this”的范围已经改变,上面的代码不起作用。 最后会出现一个错误消息,如'错误调用done()'和量angular器暂停。

相反,我结束了下面的代码。 不是最漂亮的,但它最终用一个this.skip()replace剩余testing函数的实现。 如果/当摩卡的内部随着更高的版本改变,这可能会停止工作。

通过debugging和检查摩卡的内部结构,通过一些反复试验,发现了这一点……当testing失败的时候,有助于尽快完成浏览器testing套件。

 beforeEach(function() { var parentSpec = this.currentTest.parent; if (!parentSpec.testcount) { parentSpec.testCount = parentSpec.tests.length; parentSpec.currentTestIndex = 0; } else { parentSpec.currentTestIndex = parentSpec.currentTestIndex + 1; } if (parentSpec.skipSubsequent) { parentSpec.skipSubsequent = false; var length = parentSpec.tests.length; var currentIndex = parentSpec.currentTestIndex; for (var i = currentIndex + 1; i < length; i++) { parentSpec.tests[i].fn = function() { this.skip(); }; } } }); afterEach(function() { if (this.currentTest.state === 'failed') { this.currentTest.parent.skipSubsequent = 'true' } }); 
 mocha test/ --grep <pattern> 

https://mochajs.org/

正如@danielstjules 在这里回答的,有一种方法可以跳过testing。 @本主题的作者已经从github.com mochajs的讨论中复制了答案,但是在可用的mocha版本中没有任何信息。

我正在使用grunt-mocha-test模块在我的项目中集成摩卡testingfunction。 跳到最后(现在)版本 – 0.12.7带我摩卡版本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(); } ... 

请不要。 在环境中不一致的testing应通过您的构build基础结构进行确认。 当CI构build的testing数量与本地testing数量不同时,可能会非常迷茫。

另外,它也可以重复使用。 如果在服务器上运行不同的testing,并且本地可以让testing失败并通过CI,反之亦然。 没有强制function,我没有办法快速,准确地纠正失败的构build。

如果您必须closures环境之间的testing,而不是有条件地运行testing,请标记testing并使用filter来消除在特定构build目标中不起作用的testing。 这样每个人都知道发生了什么事情,并且使他们的期望变得糟糕。 这也让大家知道testing框架中存在不一致,有人可能会有一个解决scheme,让他们再次正常运行。 如果你只是静音testing,他们可能不知道有问题。