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

3

9

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

Fulllength answered 5/11, 2009 at 20:36 Comment(0)
P
16

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>
Pneuma answered 23/7, 2012 at 21:41 Comment(0)
G
4

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.

Graeco answered 5/11, 2009 at 20:51 Comment(4)
Yes, I saw this but it isn't obvious as to how it should be used or what it should be used for. Now I know, thanks!Fulllength
In case you don't know how to use this QUnit.testStart = function (name) {};Mighty
The term "test batch" is somewhat ambiguous. Is it the same as name in a test function definition: test( name, [expected], test )?Connotation
The method desribed by Juri is now deprecated. The new methods is QUnit.testStart(function). Pass the function as an argument rather than assigning testStart to it. The current (1.10.0pre-A) allows for mutiple functions to be registered to a callback (testStart,testDone, etc). The old way only allowed for one.Pneuma
W
4

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>
Windcheater answered 2/8, 2012 at 22:0 Comment(1)
very nice. I will be using this.Pneuma

© 2022 - 2024 — McMap. All rights reserved.