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

Sov*_*iut 16 unit-testing mocha.js node.js chai

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

it 'should not retrieve documents without an authorized user', (done) ->
    done()
Run Code Online (Sandbox Code Playgroud)

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

false.should.equal true # force failure
Run Code Online (Sandbox Code Playgroud)

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

有没有官方方法在摩卡故意失败像这样的占位符测试?

Jam*_*bes 28

截至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")
    })
})
Run Code Online (Sandbox Code Playgroud)

正在运行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
Run Code Online (Sandbox Code Playgroud)


Lou*_*uis 21

标记测试,没有准备好进行测试尚未官方的方法是使用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");
    });
});
Run Code Online (Sandbox Code Playgroud)

上面的代码放入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)
Run Code Online (Sandbox Code Playgroud)

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

  • 为此,我不知道跳过。但是,看完文档后,这似乎只是从字面上“跳过”测试的一种方法。虽然有用,但我认为OP希望采用规范的方式使其失败,即您在此处的代码-`throw new Error(“ fail”);`在这种情况下,我看不到框架本身的任何方式去做这个。其他一些语言/框架具有此功能(例如:scala具有???占位符) (2认同)

Cod*_*ike 7

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

it 'should not retrieve documents without an authorized user', (done) ->
    done('not implemented')
Run Code Online (Sandbox Code Playgroud)

会导致测试失败并输出:

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


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


Jus*_*aat 5

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

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

chai断言的例子

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

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

describe("some code", function(){
    it("should fail something", function(){
        todo();
    });
});
Run Code Online (Sandbox Code Playgroud)

使用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();
    });
});
Run Code Online (Sandbox Code Playgroud)

  • 只是为了节省人们的点击,看起来这个拉请求被暂时接受,所以你现在可以使用.fail(). (4认同)