Monthly Archives: November 2010

Logging QUnit results

I’ve mentioned QUnit in other posts [POST: CUSTOM QUNIT TEST TYPES] and how much stock I put into unit testing in general, so I thought I’d share some more tips for people who don’t have much experience with QUnit. This will probably be a recurring theme on this blog.

One of the advantages of QUnit is that it has built-in hooks for testing automation tools. I use these hooks to do basic console logging so I can see what test is running next to my own log messages. I also log the amount of time each test takes.

The code to add these hooks is straightforward:

var startTime = null;
var currentTest = null;
QUnit.testStart = function(details) {
   currentTest = details.name;
   startTime = new Date().getTime();
   console.warn("QUnit starting " + currentTest);
};

QUnit.testDone = function(details) {
   console.assert(currentTest);
   cm.console.warn("QUnit finished " + currentTest + " (" + (new Date().getTime() - startTime).toString() + " ms)");
   currentTest = null;
};

QUnit.begin = function(details) {
   cm.console.log("QUnit starting");
};

QUnit.done = function(details) {
   cm.console.log("QUnit finished");
};

If you use modules then there are hooks at that level also:

QUnit.moduleStart = function(details) {
};
QUnit.moduleDone = function(details) {
};

The same approach can be used to do other things like send notifications (email/tweet) or generate a timing summary. You can read more about the QUnit integration hooks at:
http://docs.jquery.com/Qunit#Integration_into_Browser_Automation_Tools.

Advertisements
%d bloggers like this: