添加到qunit开始测试的超时进入运行时

时间:2013-01-24 13:51:51

标签: javascript unit-testing qunit

我正在为模块添加一些qunit测试用例。很少有测试用例具有同步过程,我根据文档使用标准stop()start()

我的问题是,是不是将setTimeout(function () { start();}, 1000);的额外1秒添加到测试运行的运行时间,使结果准确?

我有点不满意在运行时添加了+ 1000ms作为testuite外部,在使用该模块的真实应用程序中,该过程完成而没有在此处添加1000ms来执行测试。

因此,当我将此接口传递给技术较少的测试人员时,我必须在测试的标题中解释,在添加它们之前从该测试中减去1000,或者计算整体速度等等。[我基本上想要一种方法额外超时自动从结果中删除]

enter image description here

以下模块代码:

    define("tests/admin.connections.tests", ["mods/admin.connections", "datacontext"], function (connections, datacontext) {

    module("ADMIN PAGE CONNECTION LIST MODULE", {
        setup: function () {
            //ok(true, "once extra assert per test for Search Modules");
        }
    });

    test('Module is available?', function () {
        equal(_.isUndefined(connections), false, "connections js module exists");
        equal(_.isObject(connections), true, "connections js module is valid object");
    });

    test('HTML and CSS loading correctly? [Subtract 1 second from time to get the real time lapsed]', function () {
        function testHtml(html) {
            var d = document.createElement('htmlTestDiv');
            d.innerHTML = html;
            return d.innerHTML.replace(/\s+/g, ' ');;
        }
        stop();
        $.get('http://media.concepglobal.com/cbaddons/templates/connections.html', function (data) {
            equal(testHtml(connections.html), data.replace(/\s+/g, ' '), 'Html of the module was correctly loaded');
        });
        $.get('http://media.concepglobal.com/cbaddons/styles/connections.css', function (data) {
            equal(testHtml(connections.css), data.replace(/\s+/g, ' '), 'CSS of the module was correctly loaded');
        });
        setTimeout(function () { start();}, 1000);        
    });

    test('getConnectionsByUserId Async Method [Subtract 1 second from time to get the real time lapsed]', function () {
        function getConnectionsByUserId(successCallback) {
            amplify.request("getConnectionsByUserId", { uid: '0' }, function (data) {
                connections.userConnectionsCallback(data);
                successCallback();
            });
        }
        stop();
        getConnectionsByUserId(function () {
            var connectionsReturnedData = connections.connectionListViewModel.connections();
            expect(2);
            ok(_.isArray(connectionsReturnedData), 'Valid array has been returned for connections: ' + connectionsReturnedData);
            equal(connectionsReturnedData[0].type(), "sitecore", 'First returned object has a type property of "' + connectionsReturnedData[0].type() + '" and we expected it to be "sitecore"');
        });
        setTimeout(function () { start(); }, 1000);
    });

});

1 个答案:

答案 0 :(得分:3)

QUnit将当前运行的测试保存在QUnit.config.current中,这允许您在测试执行期间更改测试。

你可能想要的是在超时后重置测试的计时器。

我创建了一个小例子来展示我的意思(see on jsFiddle):

asyncTest("Long running test with 2s timeout", function () {
    expect(1);
    ok(true);
    setTimeout(function () { 
        QUnit.config.current.started = +new Date();
        start();
    }, 2000);
});

就像超时结束时重置计时器一样。这导致在执行的内容方面更准确的运行时间。现在只有总时间显示实际用于运行所有测试的时间。

Long running test with timer reset