在使用qUnit时,如何在每个测试运行前运行一个函数?

9

qUnit中等价于nUnit的[SetUp]属性是什么?

3个回答

16

注册一个 QUnit 回调函数

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

回调函数详细信息

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

begin
(所有测试的开始)

{}  /* empty hash */

done
(所有测试结束)

  • failed: (int) 总共失败的测试数
  • passed: (int) 总共通过的测试数
  • total: (int) 总共运行的测试数
  • runtime: (int) 测试运行时间(以毫秒为单位)

log
(在ok()方法内调用)

  • result: (boolean) 如果测试通过则为true,失败则为false
  • message: (string) 传递给ok()的任何消息

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

  • name: 测试名称(传递给test()或asyncTest()方法的第一个参数)
  • module: 模块名称(如果没有调用module()方法,则为undefined)

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

  • name: (string) 测试名称(传递给test()或asyncTest()方法的第一个参数)
  • module: (string) 模块名称(如果从未调用过module(),则为undefined)
  • failed: (int) 失败的断言数
  • passed: (int) 成功的断言数
  • total: (int) 测试中所有断言的数量

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

  • module: 模块名称

moduleDone
(在每个模块结束时调用)

  • module: (string) 模块名称
  • failed: (int) 失败的断言数(模块中所有测试的总数)
  • passed: (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);

我引用上面的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>

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>

非常好。我会使用它。 - Perry Tew

4

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

更多信息请参见文档


1
是的,我看到了,但它并不明显应该如何使用或用于什么。现在我知道了,谢谢! - tpower
2
如果您不知道如何使用 QUnit.testStart = function (name) {};,请注意: - Juri Glass
“测试批次”这个术语有些含糊不清。它是否与测试函数定义中的名称相同:test(name, [expected], test) - StuperUser
Juri所描述的方法现已过时。新方法是QUnit.testStart(function)。将函数作为参数传递,而不是将testStart分配给它。当前版本(1.10.0pre-A)允许将多个函数注册到回调(testStart,testDone等)中。旧方法只允许一个。 - Perry Tew

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接