Test-Driven Development is a popular methodology that has typically found its roots in large software systems. The idea behind the process is that tests execute on code in order to make sure that the output is correct given the specific input.
One of the biggest wins in test-driven development is that there is a suite of tests that can be executed when code is changed or new code is introduced to verify that nothing is broken.
QUnit offers the same functionality for JavaScript-based projects.
Getting started with QUnit is easy. After downloading the project, setup a basic HTML document that includes QUnit’s JavaScript and stylesheet:
[cc lang=”html”]
QUnit Test Suite
[/cc]
Once done, you can begin writing your modules, tests, and assertions in custom JavaScript functions:
[cc lang=”javascript”]
test(“module without setup/teardown (default)”, function() {
expect(1);
ok(true);
});
test(“expect in test”, 3, function() {
ok(true);
ok(true);
ok(true);
});
test(“expect in test”, 1, function() {
ok(true);
});
module(“setup test”, {
setup: function() {
ok(true);
}
});
test(“module with setup”, function() {
expect(2);
ok(true);
});
var state;
module(“setup/teardown test”, {
setup: function() {
state = true;
ok(true);
},
teardown: function() {
ok(true);
}
});
test(“module with setup/teardown”, function() {
expect(3);
ok(true);
});
[/cc]
Permitting all is setup correctly, load the HTML document up in your browser and the tests should execute:
Personally, I’m a big fan of this particular testing framework. It’s lightweight, fast, and easy to get started.
Speak your mind...