如何在Mocha中故意使空占位符测试失败?

时间:2015-01-26 15:03:42

标签: node.js unit-testing mocha chai

我在NodeJS中编写API并使用Mocha,Chai和SuperTest进行测试。我正在使用一种典型的测试驱动方法来编写测试,然后用工作代码来满足这些测试。但是,由于所有不同排列的测试数量,我已经开始编写空的占位符测试,以便我有所有it('should...')描述,以便在我获得该功能时提醒我要测试什么。例如:

it 'should not retrieve documents without an authorized user', (done) ->
    done()

这个问题是在没有任何断言的情况下调用done()因此测试被认为是传递,所以我添加了以下断言。

false.should.equal true # force failure

但这是一个黑客攻击,Mocha显示失败的原因似乎令人困惑,特别是当其他完整测试可能失败时。

有没有官方方法可以故意在Mocha中失败占位符测试?

4 个答案:

答案 0 :(得分:27)

截至05/19/2018,这是官方方式:https://mochajs.org/#pending-tests

未实施的测试不应fail,应标记为pending

将mocha测试标记为not yet implemented的简洁方法是不将回调函数传递给it处理程序。

describe("Traverse", function(){
    describe("calls the visitor function", function(){
        it("at every element inside an array")
        it("at every level of a tree")
    })
})

运行mocha test会将未实现的测试显示为待处理。

$ mocha test

  Traverse
    calls the visitor function
      - at every element inside an array
      - at every level of a tree


  0 passing (13ms)
  2 pending

答案 1 :(得分:20)

将测试标记为尚未准备好进行测试的官方方法是使用skip,这是一种显示为describeit字段的方法。这是一个例子:

describe("not skipped", function () {
    it("bar", function () {
        throw new Error("fail");
    });

    it.skip("blah", function () {
        throw new Error("fail");
    });
});

describe.skip("skipped", function () {
    it("something", function () {
        throw new Error("fail");
    });
});

以上代码放入test.js文件并以$ mocha --reporter=spec test.js运行时,会生成:

  not skipped
    1) bar
    - blah

  skipped
    - something


  0 passing (4ms)
  2 pending
  1 failing

  1) not skipped bar:
     Error: fail
      at Context.<anonymous> (/tmp/t33/test.js:3:15)
      at callFn (/home/ldd/local/lib/node_modules/mocha/lib/runnable.js:223:21)
      at Test.Runnable.run (/home/ldd/local/lib/node_modules/mocha/lib/runnable.js:216:7)
      at Runner.runTest (/home/ldd/local/lib/node_modules/mocha/lib/runner.js:374:10)
      at /home/ldd/local/lib/node_modules/mocha/lib/runner.js:452:12
      at next (/home/ldd/local/lib/node_modules/mocha/lib/runner.js:299:14)
      at /home/ldd/local/lib/node_modules/mocha/lib/runner.js:309:7
      at next (/home/ldd/local/lib/node_modules/mocha/lib/runner.js:247:23)
      at Object._onImmediate (/home/ldd/local/lib/node_modules/mocha/lib/runner.js:276:5)
      at processImmediate [as _immediateCallback] (timers.js:354:15)

将跳过以-开头的测试名称。此外,在支持颜色的终端中,跳过的测试以蓝色显示(对于失败的测试反对红色,对于传递为绿色)。一个跳过的测试被称为“待定”,因此Mocha将跳过的测试数量报告为“2待定”。

答案 2 :(得分:7)

如果您将字符串或错误传递给done(),则会将其报告为错误。所以:

it 'should not retrieve documents without an authorized user', (done) ->
    done('not implemented')

会导致测试失败并输出:

  

使用非错误调用done():未实现

我喜欢@ Canyon的解决方案,即不传递回调以标记测试“待定”,但在我的情况下,我希望这些占位符使我的CI构建失败,因此使这些实际失败的测试变得更容易。

答案 3 :(得分:5)

这实际上是一个很好的问题,因为我也觉得这非常有用。看完之后,我会考虑创建你自己的包装“todo”或“fail”函数,你可以在整个代码库中重用它。

下面的示例使用名为todo的函数,该函数将打印出“尚未实现”的文本。这可能是一个独立的模块,即使您可以导入和使用所有测试。可能需要稍微修改一下代码......

chai assert中的示例

var assert = require('chai').assert;

function todo() {
    assert(false, "method not yet implemented"); 
};

describe("some code", function(){
    it("should fail something", function(){
        todo();
    });
});

使用chai assert的示例,使用fail选项(尽管看起来不合适)

var assert = require('chai').assert;

function todo() {
    assert.fail(true, true, "method not yet implemented");   //1st 2 args can be anything really
};

describe("some code", function(){
    it("should fail something", function(){
        todo();
    });
});
相关问题