Per*_*Tew 16

注册QUnit回调

var mySetupFunc(details){/* setup code */}
QUnit.testStart(mySetupFunc);
Run Code Online (Sandbox Code Playgroud)

回调详情

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

开始
(开始所有测试)

{}  /* empty hash */
Run Code Online (Sandbox Code Playgroud)

完成
(所有测试结束)

  • 失败:(int)总测试失败
  • 传递:(int)传递的总测试
  • 总计:(int)总测试运行
  • runtime:(int)以毫秒为单位运行测试的时间

log
(在ok()方法中调用等)

  • result:(boolean)如果是好则为true,如果失败则为false
  • message:(string)你传递给ok的消息()

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

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

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

  • name:(string)测试的名称(传递给test()或asyncTest()的第一个参数)
  • module:(string)模块的名称(如果你从未调用过module(),那么将是未定义的)
  • 失败:(int)失败的断言计数
  • 传递:(int)成功的断言计数
  • total:(int)测试中所有断言的计数

moduleStart
(在每个模块的开头调用)

  • module:模块的名称

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

  • module :(字符串)模块的名称
  • 失败:(int)失败的断言计数(模块中所有测试的总计)
  • 传递:(int)成功的断言计数(模块中所有测试的总计)
  • total:(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);
Run Code Online (Sandbox Code Playgroud)

我上面引用的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>
Run Code Online (Sandbox Code Playgroud)


Gra*_*meF 5

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

有关详细信息,请参阅文档.

  • 如果你不知道如何使用这个`QUnit.testStart = function(name){};` (2认同)