使用qUnit时,如何在每次测试之前运行函数? [英] How do I run a function before each test when using qUnit?

查看:94
本文介绍了使用qUnit时,如何在每次测试之前运行函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

What is the equivalent of nUnits [SetUp] attribute for qUnit?

推荐答案

注册QUnit回调



Registering a QUnit Callback

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






回拨详情



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


Callback Details

As of QUnit version 1.10.0pre-A, each registered callback will receive a hash as the first (and only) parameter. I've named mine 'details' in the example above. The contents of the hash vary by callback. Here's a list of information in each hash.

开始

(开始所有测试)

{}  /* empty hash */

已完成

(所有测试结束)


  • 失败:(int)总测试失败

  • 传递:(int)总测试通过

  • 总计:(int)总测试运行

  • 运行时:(int)测试运行的时间(以毫秒为单位)

  • failed: (int) total tests failed
  • passed: (int) total tests passed
  • total: (int) total tests run
  • runtime: (int) how long tests took to run in milliseconds

日志

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


  • result:(boolean)如果是好则为true,如果失败则为false

  • 消息:(字符串)您传递给ok的消息()

testStart

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


  • name:测试的名称(传递给test()或asyncTest()的第一个参数)

  • module:模块的名称(如果你没有调用module()方法,这将是未定义的)

testDone

(在每个测试的结束)


  • name :(字符串)测试的名称(传递给test()的第一个参数或asyncTest())

  • 模块:(字符串)模块的名称(如果你从未调用过模块()将是未定义的)

  • 失败: (int)失败的断言计数

  • 传递:(int)成功的断言计数

  • total:(int)所有断言的计数测试

  • name: (string) the name of the test (first argument passed to test() or asyncTest())
  • module: (string) the name of the module (will be undefined if you never called module())
  • failed: (int) count of assertions that failed
  • passed: (int) count of assertions that succeeded
  • total: (int) count of all assertions in the test

moduleStart

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


  • 模块:模块名称

moduleDone

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


  • 模块:(字符串)模块的名称

  • 失败:( int)失败的断言计数(模块中所有测试的总计)

  • 传递:(int)成功的断言计数(模块中所有测试的总计)

  • total:(int)模块中所有断言的计数

  • module: (string) the name of the module
  • failed: (int) count of assertions that failed (total for all tests in module)
  • passed: (int) count of assertions that succeeded (total for all tests in module)
  • total: (int) count of all assertions in the module
// 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表如下所示:

My html table that is reference above looks like this:

<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>

这篇关于使用qUnit时,如何在每次测试之前运行函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆