Unit Testing My Library Print Footer

[ prev SettingUpForUnitTestingMyLibrary | next UnitTestingMyLibraryPrintHeader | top UnitTestingMyLibrary ]

As simple subroutine to start with is MyLibrary::printFooter:

 sub printFooter {
        # if the footer file exists ($footer), then slup it up and print it
        #print "\n\n<!-- F O O T E R -->\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 &MyLibrary::printFooter ----------
 package printFooter;
 require "../MyLibrary.pm";
 sub set_up {
    $MyLibrary::gFooter = "***FOOTER TEXT***";
 }
 sub tear_down {
    $MyLibrary::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 $MyLibrary 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 MyLibrary.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 &MyLibrary::printFooter ----------
 package printFooter;
 use Test::Unit;
 require "../MyLibrary.pm";
 use IO::String;
 sub set_up {
    $io = IO::String->new($result);
    $old_handle = select($io);
    $MyLibrary::gFooter = "***FOOTER TEXT***";
 }
 sub tear_down {
    select($old_handle);
    $io = undef;
    $result = undef;
    $old_handle = undef;
    $MyLibrary::gFooter = undef;
 }
That prepares the ground for the first test.
 sub test_footer {
    my $expected = $MyLibrary::gFooter;
    &MyLibrary::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 = "";
    $MyLibrary::gFooter = undef;
    &MyLibrary::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 Mylibrary::printFooter and adds it to the main suite. Then it runs the main suite.
 # ---------- run all tests ----------
 package MyLibrary;
 use Test::Unit;
 create_suite();
 create_suite("printFooter");
 add_suite("printFooter");
 run_suite();


CategoryTesting


EditText of this page (last edited August 17, 2002) or FindPage with title or text search