简体   繁体   English

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

[英]How do I run a function before each test when using qUnit?

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

Registering a QUnit Callback注册 QUnit 回调

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

Callback Details回调详情

As of QUnit version 1.10.0pre-A, each registered callback will receive a hash as the first (and only) parameter.从 QUnit 1.10.0pre-A 版本开始,每个注册的回调都会收到一个哈希值作为第一个(也是唯一的)参数。 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.这是每个散列中的信息列表。

begin开始
(start of all tests) (所有测试开始)

{}  /* empty hash */

done完毕
(end of all tests) (所有测试结束)

  • failed: (int) total tests failed失败:(int)总测试失败
  • passed: (int) total tests passed通过:(int)通过的测试总数
  • total: (int) total tests run总计:(int)运行的总测试
  • runtime: (int) how long tests took to run in milliseconds运行时:(int)以毫秒为单位运行测试所需的时间

log日志
(called within the ok() methods, etc) (在 ok() 方法等中调用)

  • result: (boolean) true if good, false if failed结果:(布尔值)如果好则为真,如果失败则为假
  • message: (string) whatever message you passed to ok()消息:(字符串)您传递给 ok() 的任何消息

testStart测试开始
(called at the start of each test) (在每次测试开始时调用)

  • name: the name of the test (first argument passed to test() or asyncTest()) name:测试的名称(传递给 test() 或 asyncTest() 的第一个参数)
  • module: the name of the module (if you haven't called the module() method, this will be undefined)模块:模块的名称(如果你还没有调用 module() 方法,这将是未定义的)

testDone测试完成
(called at the end of each test) (在每次测试结束时调用)

  • name: (string) the name of the test (first argument passed to test() or asyncTest())名称:(字符串)测试的名称(传递给 test() 或 asyncTest() 的第一个参数)
  • module: (string) the name of the module (will be undefined if you never called module())模块:(字符串)模块的名称(如果您从未调用过module(),则未定义)
  • failed: (int) count of assertions that failed失败:(int)失败的断言计数
  • passed: (int) count of assertions that succeeded通过:(int)成功的断言计数
  • total: (int) count of all assertions in the test总计:(int)测试中所有断言的计数

moduleStart模块开始
(called at the start of each module) (在每个模块开始时调用)

  • module: the name of the module模块:模块的名称

moduleDone模块完成
(called at the end of each test) (在每次测试结束时调用)

  • module: (string) the name of the module模块:(字符串)模块的名称
  • failed: (int) count of assertions that failed (total for all tests in module)失败:(int)失败的断言计数(模块中所有测试的总数)
  • passed: (int) count of assertions that succeeded (total for all tests in module)通过:(int)成功的断言计数(模块中所有测试的总数)
  • total: (int) count of all assertions in the module总计:(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);

My html table that is reference above looks like this:我上面引用的 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>

Perry Tew's answer helped me greatly in solving this issue for myself, and if anyone is interested I wrote an encapsulated events object that will setup all the events for you to just hook into. Perry Tew 的回答极大地帮助了我为自己解决这个问题,如果有人感兴趣,我编写了一个封装的事件对象,它将设置所有事件供您使用。 See below:见下文:

Please note that console.log() doesn't work on all browsers!请注意, 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>

QUnit.testStart(name) is called whenever a new test batch of assertions starts running.每当新的断言测试批次开始运行时,就会调用QUnit.testStart(name) name is the string name of the test batch. name是测试批次的字符串名称。

See the documentation for more info.有关更多信息,请参阅文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM