QUnit Callbacks
by anandhinava
HTML
<script src="http://code.jquery.com/qunit/qunit-1.12.0.js"></script>
<link rel="stylesheet" href="http://code.jquery.com/qunit/qunit-1.12.0.css">
<h1 id="qunit-header">Test Suite</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">
<ul class="nav">' + '
<li class="nav-item first">1</li>' + '
<li class="nav-item ">2</li>' + '
<li class="nav-item ">3</li>' + '
<li class="nav-item last">4</li>' +</ul>
</div>
JavaScript
// Runs once at the very beginning.
QUnit.begin(function () {
console.log("Running Test Suite");
});
// Runs once at the very end.
QUnit.done(function (data) {
console.info("Suite: %d failures / %d passed / %d tests", data.failed, data.passed, data.total);
});
// Runs once after each assertion.
QUnit.log(function (data) {
console[data.result ? "log" : "error"](data.message);
});
// Runs before each test.
QUnit.testStart(function (data) {
console.group("Test: " + data.name);
});
// Runs after each test.
QUnit.testDone(function (data) {
console.info("Test: %d failures / %d passed / %d tests", data.failed, data.passed, data.total);
console.groupEnd();
});
// Runs before each module.
QUnit.moduleStart(function (data) {
console.group("Module: " + data.name);
});
// Runs after each module.
QUnit.moduleDone(function (data) {
console.info("Module: %d failures / %d passed / %d tests", data.failed, data.passed, data.total);
console.groupEnd();
});
module("core");
test("a test in the core module", function () {
ok(true, "this test had better pass");
});
test("another test in the core module", function () {
ok(true, "this test had also better pass");
});
module("options");
test("a test in the options module", function () {
ok(true, "this test really, really better pass");
});
test("another test in the options module", function () {
ok(false, "sadly, this test is going to fail");
});
// Defining "setup" and "teardown" callbacks.
module("other", {
setup: function () {
ok(true, "once extra assert per test");
this.prop = "foo";
},
teardown: function () {
ok(true, "and one extra assert after each test");
}
});
test("test with setup and teardown", function () {
expect(3);
deepEqual(this.prop, "foo", "this.prop === 'foo' in all tests");
});