TestBuilder - Back end for building test libraries
var Test = new TestBuilder();
function ok (test, description) {
Test.ok(test, description);
}
TestBuilder provides the a building block upon which to write test libraries like TestSimple and TestMore that can work together. All tests are expected to use a plan and to be run in an HTML element with its "id" attribute set to "test". See TestSimple and TestMore for details. Users of this class, however, are expected to be folks who want to write test functions that interoperate with TestSimple and TestMore.
var Test = new TestBuilder();
new TestBuilder()
, you'll get the same object. (This is called a singleton). var Test = TestBuilder.create();
new TestBuilder()
if you're testing a TestBuilder-based module. Test.reset();
These methods are for setting up tests and declaring how many there are. You usually only want to call one of these methods.
Test.plan('no_plan');
Test.plan('skip_all', reason );
Test.plan('tests', numTests );
var max = Test.expectedTests();
Test.expectedTests(max);
Test.noPlan();
var plan = Test.hasPlan();
plan
is either null
(no plan has been set) "no_plan" (indeterminate number of tests) or an integer (the number of expected tests). Test.skipAll();
Test.skipAll(reason);
reason
.These methods actually run the tests. The description
argument is always optional.
Test.ok(test, description);
Test.isEq(got, expect, description);
got
is equivalent to the stringified form of expect
. Test.isNum(got, expect, description);
got
is equivalent to the stringified form of expect
. Test.isntEq(got, dontExpect, description);
isEq()
. Tests to see whether the stringified form of got
is not equivalent to the stringified form of dontExpect
. Test.isntNum(got, dontExpect, description);
isNum()
. Tests to see whether the numeric form of got
is not equivalent to the stringified form of dontExpect
. Test.like(got, /regex/, description);
Test.like(got, 'regex', description);
got
matches the regular expression in regex
. If a string is passed for the regex
argument, it will be converted to a regular expression object for testing. Test.unlike(got, /regex/, description);
Test.unlike(got, 'regex', description);
unlike()
. Tests to see whether got
does not match the regular expression in regex
. If a string is passed for the regex
argument, it will be converted to a regular expression object for testing. Test.cmpOk(got, op, expect, description);
got
and expect
. Specify any binary comparison operator as a string via the op
argument. In addition to the usual JavaScript operators, cmpOk() also supports the Perl-style string comparison operators:eq
- String equalne
- String not equallt
- String less thangt
- String greater thanle
- String less than or equalge
- String greater than or equal Test.BAILOUT(reason);
Test.skip();
Test.skip(why);
why
. Test.todoSkip();
Test.todoSkip(why);
skip()
, only it will declare the test as failing and TODO. Test.skipRest();
Test.skipRest(reason);
skip()
, only it skips all the rest of the tests you plan to run and terminates the test. Test.useNumbers(onOrOff);
ok 1
ok 2
ok 3
ok
ok
ok
true
. Test.noHeader(noHeader);
true
, no "1..N" header will be printed. Test.noEnding(noEnding);
true
, none of that will be done.Controlling where the test output goes.
Test.diag(msg);
Test.diag(msg, msg2, msg3);
diag()
is often used in conjunction with a failing test (ok() || diag()
) it "passes through" the failure. return ok(...) || diag(...);
var currTest = Test.currentTest();
Test.currentTest(num);
my @tests = Test.summary();
true
for pass, false
for fail. This is a logical pass/fail, so todos are passes. my @tests = Test.details();
tests[testNum - 1] = {
ok: is the test considered a pass?
actual_ok: did it literally say 'ok'?
desc: description of the test (if any)
type: type of test (if any, see below).
reason: reason for the above (if any)
};
currentTest()
is changed. In these cases, TestBuilder doesn't know the result of the test, so it's type is "unknown". The details for these tests are filled in. They are considered ok, but the name and actual_ok is left null
. tests[22] = { // 23 - 1, since arrays start from 0.
ok: 1, // logically, the test passed since it's todo
actual_ok: 0, // in absolute terms, it failed
desc: 'hole count',
type: 'todo',
reason: 'insufficient donuts'
};
TODO: {
Test.todo(why, howMany);
...normal testing code goes here...
}
howMany
tests will be expected to fail and thus marked as "TODO" tests. var package = Test.caller();
my(pack, file, line) = Test.caller();
my(pack, file, line) = Test.caller(height);
CPAN can provide the best examples. TestSimple and TestMore both use TestBuilder.
document.write()
, but it'd be good to allow users to define alternate outputs (tests may not always run in a browser, eh?). Maybe we can use an output object? Currently, a browser and its DOM are expected to be present.Original Perl code by chromatic and maintained by Michael G Schwern <schwern@pobox.com>. Ported to JavaScript by David Wheeler <david@kineticode.com>.
Copyright 2002, 2004 by chromatic <chromatic@wgz.org> and Michael G Schwern <schwern@pobox.com>, 2005 by David Wheeler.
This program is free software; you can redistribute it and/or modify it under the terms of the Perl Artistic License or the GNU GPL.
See http://www.perl.com/perl/misc/Artistic.html and http://www.gnu.org/copyleft/gpl.html.
Hey! The above document had some coding errors, which are explained below: