HTML and CSS Reference
In-Depth Information
1.3 Test Functions, Cases, and Suites
The test we have built so far has several assertions, but because the assert function
throws an error when a test fails, we won't know whether or not tests following a
failing test fail or succeed. For more fine-grained feedback, we can organize our test
into test functions . Each test function should exercise only one unit, but it may do
so using one or more assertions. For complete control, we can also require each test
to only test one specific behavior of a single unit. This means there will be many
tests for each function, but they'll be short and easy to understand, and the test as
a whole will provide to-the-point feedback.
A set of related test functions/methods is referred to as a test case. In the case
of the strftime function, we can imagine a test case for the whole method, with
each test testing a specific behavior of the function through one or more assertions.
Test cases are usually organized in test suites in more complex systems. Listing 1.10
shows a very simple testCase function. It accepts a string name and an object
with test methods. Every property whose name starts with the word “test” is run as
a test method.
Listing 1.10 A simple testCase function
function testCase(name, tests) {
assert.count = 0;
var successful = 0;
var testCount = 0;
for (var test in tests) {
if (!/^test/.test(test)) {
continue;
}
testCount++;
try {
tests[test]();
output(test, "#0c0");
successful++;
} catch (e) {
output(test + " failed: " + e.message, "#c00");
}
}
var color = successful == testCount ? "#0c0" : "#c00";
 
 
Search WWH ::




Custom Search