使用 qUnit 时如何在每次测试之前运行函数?

2023-11-25

nUnits 相当于什么[SetUp]qUnit 的属性?


注册 QUnit 回调

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

回拨详情

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

begin
(所有测试开始)

{}  /* empty hash */

done
(所有测试结束)

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

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

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

测试开始
(在每次测试开始时调用)

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

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

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

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

  • module:模块的名称

模块完成
(在每次测试结束时调用)

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

Examples

// 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>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 qUnit 时如何在每次测试之前运行函数? 的相关文章

随机推荐