Running Tests

     

Before you can gain any benefit from writing tests, you must be able to run them. Fortunately, there are several ways to do this, depending on what you need to know.

How do I do that?

To see real tests in action, download the latest version of Test::Harness (see http://search.cpan.org/dist/Test-Harness) from the CPAN and extract it to its own directory. Change to this directory and build the module as usual (see "Installing Test Modules," earlier in this chapter). To run all of the tests at once, type make test :

 $  make test  PERL_DL_NONLAZY=1 /usr/bin/perl5.8.6 "-MExtUtils::Command::MM" "-e" \         "test_harness(0, 'blib/lib', 'blib/arch')" t/*.t     t/00compile.........ok 1/5# Testing Test::Harness 2.46     t/00compile.........ok     t/assert............ok     t/base..............ok     t/callback..........ok     t/harness...........ok     t/inc_taint.........ok     t/nonumbers.........ok     t/ok................ok     t/pod...............ok     t/prove-globbing....ok     t/prove-switches....ok     t/strap-analyze.....ok     t/strap.............ok     t/test-harness......ok             56/208 skipped: various reasons     All tests successful, 56 subtests skipped.     Files=14, Tests=551,  6 wallclock secs ( 4.52 cusr +  0.97 csys =  5.49 CPU) 

What just happened ?

make test is the third step of nearly every Perl module installation. This command runs all of the test files it can find through Test::Harness , which summarizes and reports the results. It also takes care of setting the paths appropriately for as-yet- uninstalled modules.

What about...

Q:

How do I run tests for distributions that don't use Makefile.PL ?

A:

make test comes from ExtUtils::MakeMaker , an old and venerable module. Module::Build is easier to use in some cases. If there's a Build.PL file, instead use the commands perl Build.PL , perl Build , and perl Build test . Everything will behave as described here.

Q:

How do I run tests individually?

A:

Sometimes you don't want to run everything through make test , as it runs all of the tests for a distribution in a specific order. If you want to run a few tests individually, use prove instead. It runs the test files you pass as command-line arguments, and then summarizes and prints the results.


Note: If you don't have prove installed, you're using an old version of Test:: Harness. Use bin/ prove instead. Then upgrade .
 $  prove t/strap*.t  t/strap-analyze....ok     t/strap............ok     All tests successful.     Files=2, Tests=284,  1 wallclock secs ( 0.66 cusr +  0.14 csys =  0.80         CPU) 

If you want the raw details, not just a summary, use prove 's verbose ( -v ) flag:

 $  prove -v t/assert.t  t/assert....1..7     ok 1 - use Test::Harness::Assert;     ok 2 - assert(  ) exported     ok 3 - assert( FALSE ) causes death     ok 4 -   with the right message     ok 5 - assert( TRUE ) does nothing     ok 6 - assert( FALSE, NAME )     ok 7 -   has the name     ok     All tests successful.     Files=1, Tests=7,  0 wallclock secs ( 0.06 cusr +  0.01 csys =  0.07         CPU) 

This flag prevents prove from eating the results. Instead, it prints them directly along with a short summary. This is very handy for development and debugging (see "Interpreting Test Results," later in this chapter).

Q:

How do I run tests individually without prove?

A:

You can run most test files manually; they're normally just Perl files.

 $  perl t/00compile.t  1..5     ok 1 - use Test::Harness;     # Testing Test::Harness 2.42     ok 2 - use Test::Harness::Straps;     ok 3 - use Test::Harness::Iterator;     ok 4 - use Test::Harness::Assert;     ok 5 - use Test::Harness; 

Oops! This ran the test against Test::Harness 2.42, the installed version, instead of Version 2.46, the new version. All of the other solutions set Perl's @INC path correctly. When running tests manually, use the blib module to pick up the modules as built by make or perl Build :


Note: Confused about @INC and why it matters? See perldoc perlvar for enlightenment .
 $  perl -Mblib t/00compile.t  1..5     ok 1 - use Test::Harness;     # Testing Test::Harness 2.46     ok 2 - use Test::Harness::Straps;     ok 3 - use Test::Harness::Iterator;     ok 4 - use Test::Harness::Assert;     ok 5 - use Test::Harness; 

The -M switch causes Perl to load the given module just as if the program file contained a use blib; line.

The TEST_FILES argument to make_test can simplify this:


Note: TEST_FILE Scan also take a file pattern, such as TEST_FILES=t/ strap*.t .
 $  make test TEST_FILES=t/00compile.t  t/00compile....ok 1/5# Testing Test::Harness 2.46     t/00compile....ok     All tests successful.     Files=1, Tests=5,  0 wallclock secs ( 0.13 cusr +  0.02 csys =  0.15         CPU) 

For verbose output, add TEST_VERBOSE=1 .



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