Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 390
tpower Avatar asked Nov 05 '09 20:11

tpower


People also ask

How do you write a test case on QUnit?

Create a Test CaseMake a call to the QUnit. test function, with two arguments. Name − The name of the test to display the test results. Function − Function testing code, having one or more assertions.

What is QUnit module?

Modules with grouped test functions are used to define nested modules. QUnit run tests on the parent module before going deep on the nested ones, even if they're declared first. The beforeEach and afterEach callbacks on a nested module call will stack in LIFO (Last In, First Out) Mode to the parent hooks.

Which is QUnit?

QUnit is a JavaScript library to test your JavaScript code. You can write your tests using certain functions provided by QUnit and then have QUnit run them. QUnit can run your tests in the browser or in a plain Node. js process.


2 Answers

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>
like image 170
Perry Tew Avatar answered Oct 12 '22 22:10

Perry Tew


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>
like image 44
dano Avatar answered Oct 12 '22 23:10

dano