[ prev SettingUpForUnitTestingMyLibrary | next UnitTestingMyLibraryPrintHeader | top UnitTestingMyLibrary ] As simple subroutine to start with is M''''''yLibrary::printFooter: sub printFooter { # if the footer file exists ($footer), then slup it up and print it #print "\n\n\n"; #if (-e $footer) { # # my $b = `cat $footer`; # print $b; # #} print $gFooter; } This subroutine has grown moribund with all but one line commented out due to changes in the design to put the standard page footer into the database. This makes it a nice simple starting place. The only data it uses is one global string variable, $gFooter, and it's only action is to print the contents of that variable. The set_up and tear_down methods are simple: # ---------- tests for &M''''''yLibrary::printFooter ---------- package printFooter; require "../M''''''yLibrary.pm"; sub set_up { $M''''''yLibrary::gFooter = "***FOOTER TEXT***"; } sub tear_down { $M''''''yLibrary::gFooter = undef; } All the variables defined in set_up need to be set back to ''undef'' in tear_down to avoid interference with other tests. The $M''''''yLibrary NameSpace qualification is necessary to ensure that the variable will be available to the subroutine being tested. There is a difficulty caused by the print statement; the subroutine doesn't return a value it simply prints it to STDOUT. Many of the subroutines in M''''''yLibrary.pm print various parts of an HTML page to STDOUT, so we need a way to capture the printed values in order to test that the subroutine worked as expected. The solution is to capture printed output into a variable. This leads to another Perl module that is required for this to be done as easily as possible, IO::String. The result is a new set_up and tear_down routine. # ---------- tests for &M''''''yLibrary::printFooter ---------- package printFooter; use Test::Unit; require "../M''''''yLibrary.pm"; use IO::String; sub set_up { $io = IO::String->new($result); $old_handle = select($io); $M''''''yLibrary::gFooter = "***FOOTER TEXT***"; } sub tear_down { select($old_handle); $io = undef; $result = undef; $old_handle = undef; $M''''''yLibrary::gFooter = undef; } That prepares the ground for the first test. sub test_footer { my $expected = $M''''''yLibrary::gFooter; &M''''''yLibrary::printFooter; assert($result eq $expected, "printFooter failed when gFooter given"); } The only other possible starting condition to test that leaps to mind is the case where printFooter is called but $gFooter hasn't been defined yet. Here Perl will print nothing without complaining about the undefined variable so the test should be. sub test_no_footer { my $expected = ""; $M''''''yLibrary::gFooter = undef; &M''''''yLibrary::printFooter; assert($result eq $expected, "printFooter failed when no gFooter given"); } That completes the TestCase and all that remains is to add the code that will run these tests. This creates a main TestSuite, creates a suite specifically for M''''''ylibrary::printFooter and adds it to the main suite. Then it runs the main suite. # ---------- run all tests ---------- package M''''''yLibrary; use Test::Unit; create_suite(); create_suite("printFooter"); add_suite("printFooter"); run_suite(); ---- CategoryTesting