9

[SetUp]qUnit 的 nUnits属性的等价物是什么?

4

3 回答 3

16

注册一个 QUnit 回调

var mySetupFunc(details){/* setup code */}
QUnit.testStart(mySetupFunc);

回调详细信息

从 QUnit 版本 1.10.0pre-A 开始,每个注册的回调都将接收一个哈希作为第一个(也是唯一的)参数。我在上面的示例中命名了我的“详细信息”。哈希的内容因回调而异。这是每个哈希中的信息列表。

开始
(所有测试的开始)

{}  /* empty hash */

完成
(所有测试结束)

  • failed: (int) 总测试失败
  • 通过:(int)通过的总测试
  • 总计:(int)总测试运行
  • 运行时:(int)测试运行多长时间(以毫秒为单位)

日志
(在 ok() 方法等中调用)

  • 结果:(布尔值)如果成功则为真,如果失败则为假
  • 消息:(字符串)您传递给 ok() 的任何消息

testStart
(在每个测试开始时调用)

  • name:测试的名称(传递给 test() 或 asyncTest() 的第一个参数)
  • module:模块的名称(如果你没有调用 module() 方法,这将是未定义的)

testDone
(在每次测试结束时调用)

  • name:(字符串)测试的名称(传递给 test() 或 asyncTest() 的第一个参数)
  • 模块:(字符串)模块的名称(如果您从未调用过 module(),则将未定义)
  • failed: (int) 失败的断言计数
  • 通过:(int)成功的断言计数
  • 总计:(int)测试中所有断言的计数

moduleStart
(在每个模块开始时调用)

  • 模块:模块的名称

moduleDone
(在每次测试结束时调用)

  • 模块:(字符串)模块的名称
  • failed:(int)失败的断言计数(模块中所有测试的总数)
  • 通过:(int)成功的断言计数(模块中所有测试的总数)
  • 总计:(int)模块中所有断言的计数

例子

// There's probably a more elegant way of doing this, 
// but these two methods will add a row to a table for each test showing how long
// each test took.  
var profileStartTime = null;

function startTimer(details) {
  profileStartTime = new Date();
}

function stopTimer(details) {
  var stopDate = new Date();
  var duration = stopDate - profileStartTime;
  jQuery('#profiling').append(
    "<tr><td>" 
    + (details.module ? details.module + ":" : "") 
    + details.name 
    + "<\/td><td class='duration'>" 
    + duration 
    + "<\/td><\/tr>");
}

QUnit.testStart(startTimer);
QUnit.testDone(stopTimer);

上面引用的我的 html 表如下所示:

<div style='margin: 10px 0;'>
  <table summary='profiling' class='profiling_table'>
    <thead>
    <tr>
      <th>Test Name</th>
      <th>Duration</th>
    </tr>
    </thead>
    <tbody id='profiling'>
    </tbody>
  </table>
</div>
于 2012-07-23T21:41:10.567 回答
4

QUnit.testStart(name)每当新的测试批断言开始运行时调用。name是测试批次的字符串名称。

有关更多信息,请参阅文档

于 2009-11-05T20:51:11.607 回答
4

Perry Tew 的回答极大地帮助我自己解决了这个问题,如果有人有兴趣,我编写了一个封装的事件对象,它将设置所有事件供您挂钩。见下文:

请注意,这console.log()不适用于所有浏览器!

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.21/jquery-ui.min.js"></script>    
<script src="lib/qunit-1.9.0.js"></script>
<script src="lib/jquery.mockjax.js"></script>

<!-- QUnit Events -->
<script>

    var testSetup = {

        begin       : function (data) /* before any tests start */ {
            console.log("begin: [" + new Date().toLocaleTimeString() + "]");
        },
        moduleStart : function (data) /* before the start of each module */ {
            console.log("-------\n  moduleStart:", data.name);
        },
        testStart   : function (data) /* before the start of each test */ {
            console.log("    testStart:", data.name);
        },
        log         : function (data) /* called after every assertion */ {
            console.log("      log:", data.message);
        },
        testDone    : function (data) /* after each test */ {
            console.log("    testDone:", data);
        },
        moduleDone  : function (data) /* after each module */ {
            console.log("  moduleDone:", data);
        },
        done        : function (data) /* all tests done */ {
            console.log("done:", data);
        },

        init : function () {
            QUnit.begin = testSetup.begin;
            QUnit.moduleStart = testSetup.moduleStart;
            QUnit.testStart = testSetup.testStart;
            QUnit.log = testSetup.log;
            QUnit.testDone = testSetup.testDone;
            QUnit.moduleDone = testSetup.moduleDone;
            QUnit.done = testSetup.done;
            console.log("\n======== QUnit events initialized ==========");
        }
    };

$(document).ready(testSetup.init);
</script>
于 2012-08-02T22:00:49.427 回答