Mocha 测试用例 - 嵌套的 it() 函数是否合乎规范?

Mocha test case - are nested it( ) functions kosher?

我有这种情况,我想在 Mocha 测试中嵌套 it() 测试用例。我确定这是错误的,我没有看到任何建议来做我正在做的事情,但我现在真的不知道更好的方法 -

基本上,我有一个 "parent" 测试,在父测试中有一个包含所有 "child" 测试的 forEach 循环:

it('[test] enrichment', function (done) {

        var self = this;

        async.each(self.tests, function (json, cb) {

            //it('[test] ' + path.basename(json), function (done) {

                var jsonDataForEnrichment = require(json);
                jsonDataForEnrichment.customer.accountnum = "8497404620452729";
                jsonDataForEnrichment.customer.data.accountnum = "8497404620452729";
                var options = {
                    url: self.serverURL + ':' + self.serverPort + '/event',
                    json: true,
                    body: jsonDataForEnrichment,
                    method: 'POST'
                };


               request(options,function (err, response, body) {
                    if (err) {
                        return cb(err);
                    }

                     assert.equal(response.statusCode, 201, "Error: Response Code");
                     cb(null);


                });

            //});

        }, function complete(err) {
            done(err)
        });

    });

如您所见,注释掉了两行单独的行 - 我想将它们包括在内,以便我可以轻松查看每个单独测试的结果,但随后我遇到了触发测试回调的尴尬情况async.each.

的回调

有没有人以前见过这种情况并且知道测试人员可以轻松地在循环中查看每个测试的结果的好解决方案?

我认为动态测试的需求比较普遍(数据驱动测试?),动态 it 和测试用例也很常见。

我认为如果测试可以按顺序执行,那么管理测试用例的完成会更容易。这样您就不必担心管理嵌套的异步 done。由于 request 是异步的(我假设),您的测试用例仍将主要同时执行。

describe('[test] enrichment', function () {

        var self = this;


        _.each(self.tests, function (json, cb) {

            it('[test] ' + path.basename(json), function (done) {

                var jsonDataForEnrichment = require(json);
                jsonDataForEnrichment.customer.accountnum = "8497404620452729";
                jsonDataForEnrichment.customer.data.accountnum = "8497404620452729";
                var options = {
                    url: self.serverURL + ':' + self.serverPort + '/event',
                    json: true,
                    body: jsonDataForEnrichment,
                    method: 'POST'
                };


               request(options,function (error, response, body) {
                    if (error) {
                        cb(error);

                    }
                    else{
                        assert.equal(response.statusCode, 201, "Error: Response Code");
                        cb(null);
                    }

                    done(); 
                });

            });

        }
    });

不要嵌套 it 调用。同步调用它们。

嵌套 it 调用在 Mocha 中永远不会正常。 it 调用也不是异步执行的。 (test 可以是异步的,但是你不能异步call it。)这是一个简单的测试:

describe("level 1", function () {
    describe("first level 2", function () {
        it("foo", function () {
            console.log("foo");
            it("bar", function () {
                console.log("bar");
            });
        });

        setTimeout(function () {
            it("created async", function () {
                console.log("the asyncly created one");
            });
        }, 500);
    });

    describe("second level 2", function () {
        // Give time to the setTimeout above to trigger.
        it("delayed", function (done) {
            setTimeout(done, 1000);
        });
    });
});

如果你 运行 你不会得到嵌套测试 bar 将被忽略,异步创建的测试 (delayed) 也将被忽略。

Mocha 没有为这些类型的调用定义语义。 当我 运行 我在撰写本文时使用最新版本的 Mocha (2.3.3 ), 它只是忽略了它们。我记得早期版本的 Mocha 会识别这些测试,但会将它们附加到错误的 describe 块。