The basic unit of testing is the ok. For each thing you want to test your program will print out an "ok" or "not ok" to indicate pass or fail. You do this with the ok() function (see below).
The only other constraint is that you must pre-declare how many tests you plan to run. This is in case something goes horribly wrong during the test and your test program aborts, or skips a test or whatever. You do this like so:
You must have a plan.
ok( foo == bar, description ); ok( foo == bar );
ok() is given an expression (in this case
foo == bar). If it's true, the test passed. If it's false, it didn't. That's about it.
ok() prints out either "ok" or "not ok" along with a test number (it keeps track of that for you).
// This produces "ok 1 - Hell not yet frozen over" (or not ok) ok( getTemperature(hell) > 0, 'Hell not yet frozen over' );
If you provide a
description, that will be printed along with the "ok/not ok" to make it easier to find your test when if fails (just search for the name). It also makes it easier for the next guy to understand what your test is for. It's highly recommended you use test names.
Test.Simple will start by printing number of tests run in the form "1..M" (so "1..5" means you're going to run 5 tests). This strange format lets Test.Harness know how many tests you plan on running in case something goes horribly wrong.
If all your tests passed, Test.Simple will exit with zero (which is normal). If anything failed it will exit with how many failed. If you run less (or more) tests than you planned, the missing (or extras) will be considered failures. If no tests were ever run, Test.Simple will throw a warning and exit with 255. If the test died, even after having successfully completed all its tests, it will still be considered a failure and will exit with 255.
This module is by no means trying to be a complete testing system. It's just to get you started. Once you're off the ground, we recommended that you look at Test.More.
It will produce output like this:
1..5 ok 1 - Constructor works ok 2 - title() get ok 3 - director() get not ok 4 - rating() get # Failed test (t/film.html at line 14) ok 5 - numExplodingSheep() get # Looks like you failed 1 tests of 5
Indicating that the
Film.rating() method is broken.
More testing functions! Once you outgrow Test.Simple, look at Test.More. Test.Simple is 100% forward compatible with Test.More (i.e. you can just use Test.More instead of Test.Simple in your programs and things will still work).
JSUnit: elaborate xUnit-style testing framework.
Copyright 2001, 2002, 2004 by Michael G Schwern <firstname.lastname@example.org>, 2005, 2008 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.