ドキュメント

QUnit

From jQuery JavaScript Library

Jump to: navigation, search

Contents

About

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.

It's currently located at: http://github.com/jquery/qunit

Contact

QUnit is maintained by Jörn Zaefferer and John Resig.

Please post to the Developing jQuery Core forum (see Discussion) for anything related to QUnit (put QUnit into the subject of your thread).

Using QUnit

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:

Basic usage examples

test("a basic test example", function() {
  ok( true, "this test is fine" );
  var value = "hello";
  equals( "hello", value, "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);
  equals( true, false, "failing test" );
  equals( 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://github.com/jquery/qunit/raw/master/qunit/qunit.css" type="text/css" media="screen" />
<script type="text/javascript" src="http://github.com/jquery/qunit/raw/master/qunit/qunit.js"></script>

  <script>
  $(document).ready(function(){
    
test("a basic test example", function() {
  ok( true, "this test is fine" );
  var value = "hello";
  equals( "hello", value, "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);
  equals( true, false, "failing test" );
  equals( true, true, "passing test" );
});

  });
  </script>
  
</head>
<body>
  <h1 id="qunit-header">QUnit example</h1>
 <h2 id="qunit-banner"></h2>
 <h2 id="qunit-userAgent"></h2>
 <ol id="qunit-tests"></ol>
</body>
</html>

API documentation

NameType
Setup:









NameType
test( name, expected, test )
Add a test to run.
asyncTest( name, expected, test )
Add an asynchronous test to run. The test must include a call to start().
expect( amount )
Specify how many assertions are expected to run within a test.
module( name, lifecycle )
Separate tests into modules.
init( )
Initialize the test runner (if the runner has already run it'll be re-initialized, effectively resetting it). This method does not need to be called in the normal use of QUnit.
Assertions:






NameType
ok( state, message )
A boolean assertion, equivalent to JUnit's assertTrue. Passes if the first argument is truthy.
equals( actual, expected, message )
A comparison assertion, equivalent to JUnit's assertEquals.
same( actual, expected, message )
A deep recursive comparison assertion, working on primitive types, arrays and objects.
Asynchronous Testing:



NameType
start( )
Start running tests again after the testrunner was stopped. See stop().
stop( timeout )
Stop the testrunner to wait to async tests to run. Call start() to continue.

URL Parameters

You can customize individual testruns via URL paramters. To start, double click on a test to have QUnit only run that single test.

Or add the name of a module to the URL to run only tests within that module, eg jquery.com/test?fx runs only tests for effects. You can combine various filters, eg. ?fx&ajax to run effects and ajax tests.

Another feature is global-variables pollution detection. Add ?noglobals to the URL, and QUnit will detect if a test introduced a new global variable (aka new properties on the window object), making that test fail.

Integration into Browser Automation Tools

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. The callbacks are:

  • QUnit.log(result, 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, failures, total) is called whenever a batch of assertions finishes running. name is the string name of the test batch. failures is the number of test failures that occurred. total is the total number of test assertions that occurred.
  • QUnit.moduleStart(name) is called whenever a new module of tests starts running. name is the string name of the module.
  • QUnit.moduleDone(name, failures, total) is called whenever a module finishes running. name is the string name of the module. failures is the number of module failures that occurred. total is the total number of module assertions that occurred.
  • QUnit.done(failures, total) is called whenever all the tests have finished running. failures is the number of failures that occurred. total is the total number of assertions that occurred.

Additionally QUnit.reset is called after every test group. You can use override this method to reset the DOM structure to its original state (if you so choose).

Reference Test Suites

jQuery itself has the biggest set of tests using QUnit:

The validation plugin has decent test coverage, too:

Examples from the jQuery UI project:

Further resources

You're welcome to add more resources to this list.