Writing a Testing Harness

     

TAP is a simple protocol (see "Interpreting Test Results" in Chapter 1), but you shouldn't have to write your own parser when Test::Harness already knows how to interpret the results. However, Test::Harness only prints out what it discovers.


Note: Test::Harness uses Test::Harness:: Straps internally

Test::Harness::Straps is a thin wrapper around a TAP parser. It collects the results in a data structure but does not analyze or print them. Writing a program to report those results in an alternate format is easy. If you want to do something when tests fail, or if you want to do something more complicated than simply reporting test results, why not write your own testing harness?

How do I do that?

Save the following program somewhere in your path as new_harness.pl and make it executable:

 #!perl     use strict;     use warnings;     use Test::Harness::Straps;     my $strap = Test::Harness::Straps->new(  );     for my $file (@ARGV)     {         next unless -f $file;         my %results = $strap->analyze_file( $file );         printf <<END_REPORT, $file, @results{qw( max seen ok skip todo bonus )};     Results for %s         Expected tests:    %d         Tests run:         %d         Tested passed:     %d         Tests skipped:     %d         TODO tests:        %d         TODO tests passed: %d     END_REPORT     } 

Run it on a directory full of tests (the Test::Harness suite, for example):

 $  new_harness t/strap*t  Results for t/strap-analyze.t         Expected tests:    108         Tests run:         108         Tested passed:     108         Tests skipped:     0         TODO tests:        0         TODO tests passed: 0     Results for t/strap.t         Expected tests:    176         Tests run:         176         Tested passed:     176         Tests skipped:     0         TODO tests:        0         TODO tests passed: 0 


Note: Your shell should expand the file pattern t/strap*. t to include only the straps tests shown in the output .

What just happened ?

The first few lines start the program as normal, loading a few modules and pragmas and creating a new Test::Harness::Straps object. The program then loops around all filenames given on the command line, skipping them if they don't exist.

All of the magic happens in the call to analyze_file( ) . This method takes the name of a test file to run, runs it, collects and parses the output, and returns a hash with details about the test file. The rest of the program prints some of these details.

As documented in Test::Harness::Straps , most of the keys of this hash are straightforward. Table 3-1 lists the most important ones.

Table 3-1. Keys of a test file's results

Key

Description

max

The number of tests planned to run

seen

The number of tests actually run

ok

The number of tests that passed

skip

The number of tests skipped

todo

The number of TODO tests encountered

bonus

The number of TODO tests that passed



Note: The current version of Test:: Harness::Straps, as distributed with Test:: Harness, is an alpha release. Andy Lester, the maintainer, plans to change the interface. Take this lab's information as a guideline and consider the module's documentation as authoritative .

Another important key is details . It contains an array reference of hashes containing details for each individual test. Table 3-2 explains the keys of this hash.

Table 3-2. Keys of a test's details

Key

Description

ok

Did the test pass, true or false?

actual_ok

Did it pass without being a skipped or TODO test, true or false?

name

The test description, if any.

type

The type of the test, skip, todo, or normal (an empty string).

reason

The reason for the skip or TODO, if either.




Perl Testing. A Developer's Notebook
Perl Testing: A Developers Notebook
ISBN: 0596100922
EAN: 2147483647
Year: 2003
Pages: 107

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net