HTML and CSS Reference
In-Depth Information
1.3.1 Setup and Teardown
xUnit frameworks usually provide
setUp
and
tearDown
methods. These are
called before and after each test method respectively, and allow for centralized
setup of test data, also known as test fixtures. Let's add the date object as a test
fixture using the
setUp
method. Listing 1.12 shows the augmented
testCase
function that checks if the test case has
setUp
and
tearDown
, and if so, runs
them at the appropriate times.
Listing 1.12
Implementing
setUp
and
tearDown
in
testCase
function testCase(name, tests) {
assert.count = 0;
var successful = 0;
var testCount = 0;
var hasSetup = typeof tests.setUp == "function";
var hasTeardown = typeof tests.tearDown == "function";
for (var test in tests) {
if (!/^test/.test(test)) {
continue;
}
testCount++;
try {
if (hasSetup) {
tests.setUp();
}
tests[test]();
output(test, "#0c0");
if (hasTeardown) {
tests.tearDown();
}
// If the tearDown method throws an error, it is
// considered a test failure, so we don't count
// success until all methods have run successfully
successful++;
} catch (e) {
output(test + " failed: " + e.message, "#c00");
}
}