开发者

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

What is the equivalent of nUnits [SetU开发者_如何学Cp] attribute for qUnit?


Registering a QUnit Callback

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. 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
  • passed: (int) total tests passed
  • total: (int) total tests run
  • runtime: (int) how long tests took to run in milliseconds

log
(called within the ok() methods, etc)

  • result: (boolean) true if good, false if failed
  • message: (string) whatever message you passed to ok()

testStart
(called at the start of each test)

  • name: the name of the test (first argument passed to test() or asyncTest())
  • module: the name of the module (if you haven't called the module() method, this will be undefined)

testDone
(called at the end of each test)

  • 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
(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)
  • passed: (int) count of assertions that succeeded (total for all tests in module)
  • total: (int) count of all assertions in the module

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:

<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. See below:

Please note that console.log() doesn't work on all browsers!

<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. name is the string name of the test batch.

See the documentation for more info.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜