Contents |
QUnit is a powerful, easy-to-use, JavaScript test suite. It's used by the jQuery project to test its code and plugins but is capable of testing any generic JavaScript code (and even capable of testing JavaScript code on the server-side).
QUnit is especially useful for regression testing: Whenever a bug is reported, write a test that asserts the existence of that particular bug. Then fix it and commit both. Every time you work on the code again, run the tests. If the bug comes up again - a regression - you'll spot it immediately and know how to fix it, because you know what code you just changed.
Having good unit test coverage makes safe refactoring easy and cheap. You can run the tests after each small refactoring step and always know what change broke something.
QUnit is similar to other unit testing frameworks like JUnit, but makes use of the features JavaScript provides and helps with testing code in the browser, eg. with its stop/start facilities for testing asynchronous code.
The code is located at: http://github.com/jquery/qunit
Planning for QUnit, a to-be-built qunitjs.com site and other testing tools happens on the jQuery Testing Team planning wiki.
QUnit is maintained by Jörn Zaefferer and the jQuery Testing Team.
Please post to the QUnit and testing forum for anything related to QUnit or testing in general.
For announcements, follow @qunitjs
To use QUnit, you have to include its qunit.js and qunit.css files and provide a basic HTML structure for displaying the test results:
test("a basic test example", function() {
ok( true, "this test is fine" );
var value = "hello";
equal( value, "hello", "We expect value to be hello" );
});
module("Module A");
test("first test within module", function() {
ok( true, "all pass" );
});
test("second test within module", function() {
ok( true, "all pass" );
});
module("Module B");
test("some other test", function() {
expect(2);
equal( true, false, "failing test" );
equal( true, true, "passing test" );
});
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<link rel="stylesheet" href="http://code.jquery.com/qunit/git/qunit.css" type="text/css" media="screen" />
<script type="text/javascript" src="http://code.jquery.com/qunit/git/qunit.js"></script>
<script>
$(document).ready(function(){
test("a basic test example", function() {
ok( true, "this test is fine" );
var value = "hello";
equal( value, "hello", "We expect value to be hello" );
});
module("Module A");
test("first test within module", function() {
ok( true, "all pass" );
});
test("second test within module", function() {
ok( true, "all pass" );
});
module("Module B");
test("some other test", function() {
expect(2);
equal( true, false, "failing test" );
equal( true, true, "passing test" );
});
});
</script>
</head>
<body>
<h1 id="qunit-header">QUnit example</h1>
<h2 id="qunit-banner"></h2>
<div id="qunit-testrunner-toolbar"></div>
<h2 id="qunit-userAgent"></h2>
<ol id="qunit-tests"></ol>
<div id="qunit-fixture">test markup, will be hidden</div>
</body>
</html>
The #qunit-header element should contain the name of the testsuite, and won't be modified by QUnit. The #qunit-banner element will set to show up as red if a test failed, green if all tests passed. The #qunit-userAgent elements is set to display the navigator.userAgent property. The #qunit-tests element will be used as a container for the test results.
The #qunit-fixture element can be used to provide and manipulate test markup, and it's content will be automatically reset after each test (see QUnit.reset). The element is styled with position:absolute; top:-10000px; left:-10000; - with these, it won't be obstructing the result, without affecting code the relies on the affected elements to be visible (instead of display:none).
All these are optional. See below for alternatives on processing the test results.
You can customize individual testruns via URL paramters. To start, click the Rerun-link next to any test result to have QUnit only run that single test. This adds ?filter=[name-of-test] to the URL.
You can also add "?filter=[module]" URL to run only tests within that module, e.g. http://swarm.jquery.org/git/jquery/master/test/?filter=effects runs only tests for effects.
Two more filters can be activated manually by editing the URL, or by clicking the checkboxes in the header:
To integrate QUnit into browser automation tools, those doing the work of launching various browsers and gathering the results, QUnit provides a simple microformat for its test result.
<p id="qunit-testresult" class="result"> Tests completed in 221 milliseconds.<br/> <span class="passed">232</span> tests of <span class="total">232</span> passed, <span class="failed">0</span> failed. </p>
Additionally, QUnit provides a series of callbacks that can be overwritten to provide updates when various actions occur. All of them (except begin) receive a single argument with the properties listed here:
QUnit.log({ result, actual, expected, message }) is called whenever an assertion is completed. result is a boolean (true for passing, false for failing) and message is a string description provided by the assertion.
QUnit.testStart({ name }) is called whenever a new test batch of assertions starts running. name is the string name of the test batch.
QUnit.testDone({ name, failed, passed, total }) is called whenever a batch of assertions finishes running. name is the string name of the test batch. failed is the number of test failures that occurred. total is the total number of test assertions that occurred. Passed is the number of test assertions that passed.
QUnit.moduleStart({ name }) is called whenever a new module of tests starts running. name is the string name of the module.
QUnit.moduleDone({ name, failed, passed, total }) is called whenever a module finishes running. name is the string name of the module. failed is the number of module failures that occurred. total is the total number of module assertions that occurred. Passed is the number of module assertions that passed.
QUnit.begin() is called once before running any tests. (a better would've been QUnit.start, but thats already in use elsewhere and can't be changed.)
QUnit.done({ failed, passed, total, runtime }) is called whenever all the tests have finished running. failed is the number of failures that occurred. total is the total number of assertions that occurred, passed the passing assertions. runtime is the time in milliseconds to run the tests from start to finish.
Additionally QUnit.reset is called after every test group. If the default, resetting the #qunit-fixture element, isn't enough, you can override or proxy it to add additional resets.
jQuery itself has the biggest set of tests using QUnit:
The validation plugin has decent test coverage, too:
Examples from the jQuery UI project:
If you want to read more on unit testing JavaScript (not specific to QUnit), check out the book Test-Driven JavaScript Development.
More to come.