Tutorial
The most concise way of running a test suite with jsUnity:
var results = jsUnity.run(function () {
function testA() {
}
});
// results.suiteName == undefined
// results.total == 1
// results.passed == 1
// results.failed == 0
// results.duration == ?
Anonymous functions are just one way of writing test suites. Here, our (unnamed) test suite has only a single test called testA. Test function names must start with “test” for the functions to be regarded as test functions by jsUnity.
The results object holds the outcome of the test suite run. Properties total, passed, failed give the number of test cases that were run, passed and failed, respectively. The duration property gives the time in milliseconds it took to run the test suite.
You can also use named functions as test suites. The name of the function becomes the name of the test suite and can be retrieved from the suiteName property of the results object:
function namedTestSuite() {
function testA() {
jsUnity.assertions.assertTrue(true);
}
function testB() {
jsUnity.assertions.assertEquals(Math.PI, 22 / 7);
}
}
var results = jsUnity.run(namedTestSuite);
// results.suiteName == "namedTestSuite"
// results.total == 2
// results.passed == 1
// results.failed == 1
// results.duration == ?
You can use the default set of assertion functions through the assertions property. jsUnity currently comes with the following assertion functions that are inspired by JsUnit and JsUnitTest:
assertExceptionassertTrueassertFalseassertIdenticalassertNotIdenticalassertEqualassertNotEqualassertMatchassertNotMatchassertTypeOfassertNotTypeOfassertInstanceOfassertNotInstanceOfassertNullassertNotNullassertUndefinedassertNotUndefinedassertNaNassertNotNaNfail
If constantly typing “jsUnity.assertions” seems too verbose, you can use the attachAssertions function to copy all assertions functions to the global (or current) scope:
var myTestSuite = function () {
function testA() {
assertTrue(1 + 1 == 2);
}
}
jsUnity.attachAssertions();
jsUnity.run(myTestSuite);
// results.suiteName == undefined
//
// Note that using a named variable
// is not the same as using a named function!
Since the current scope may not be writable in all environments, the assertion functions can be attached to a given scope:
var scope = {};
var myTestSuite = function () {
function testA() {
scope.assertTrue(1 + 1 == 2);
}
}
jsUnity.attachAssertions(scope);
jsUnity.run(myTestSuite);
By virtue of being an xUnit framework, jsUnity also supports the setUp and tearDown functions that get run prior to and after each test function:
var myTestSuite = function () {
function setUp() {
// setup test fixture
}
function tearDown() {
// cleanup test fixture
}
function testA() {
}
function testB() {
}
}
jsUnity.run(myTestSuite);
The stub log function is used by jsUnity to report progress. You can replace the stub with a custom function to add logging to your test suite runs:
jsUnity.log = function (s) {
document.write("<div>" + s + "</div>");
};
Test suite parsing errors are reported through the error function which is by default wired to direct error messages to the log function after prepending “[ERROR] ” to the error messages. You can override the error function to get custom error reporting:
jsUnity.error = function (s) {
document.write("<div class=\"error\">" + s
+ "</div>");
};
Another form of test suites is objects with test functions as properties:
var myTestSuite = {
setUp: function () {
},
tearDown: function () {
},
testA: function () {
},
testB: function () {
}
};
jsUnity.run(myTestSuite);
You can also use an array of functions:
function testA() {
}
function testB() {
}
var myTestSuite = [ testA, testB ];
jsUnity.run(myTestSuite);
An array of function names works as well:
function testA() {
}
function testB() {
}
var myTestSuite = [ "testA", "testB" ];
jsUnity.run(myTestSuite);
The final form of test suites is JavaScript source code of a function-type test suite:
var testSuiteSrc = scriptElem.innerHTML; jsUnity.run(testSuiteSrc);
This can come in handy if you’re loading test suites through Ajax or an iframe.