|
NAMETest::TableDriven - write tests, not scripts that run themSYNOPSISuse A::Module qw/or two!/; use Test::TableDriven ( foo => { input => 'expected output', another => 'test', }, bar => [[some => 'more tests'], [that => 'run in order'], [refs => [qw/also work/]], [[qw/this is also possible/] => { and => 'it works' }], ], ); runtests; sub foo { my $in = shift; my $out = ...; return $out; } sub bar { same as foo } DESCRIPTIONWriting table-driven tests is usually a good idea. Adding a test case doesn't require adding code, so it's easy to avoid fucking up the other tests. However, actually going from a table of tests to a test that runs is non-trivial."Test::TableDriven" makes writing the test drivers trivial. You simply define your test cases and write a function that turns the input data into output data to compare against. "Test::TableDriven" will compute how many tests need to be run, and then run the tests. Concentrate on your data and what you're testing, not "plan tests =" scalar keys %test_cases> and a big foreach loop. WHAT DO I DOStart by using the modules that you need for your tests:use strict; use warnings; use String::Length; # the module you're testing Then write some code to test the module: sub strlen { my $in = shift; my $out = String::Length->strlen($in); return $out; } This "strlen" function will accept a test case (as $in) and turns it into something to compare against your test cases: Oh yeah, you need some test cases: use Test::TableDriven ( strlen => { foo => 3, bar => 3, ..., }, ); And you'll want those test to run somehow: runtests; Now execute the test file. The output will look like: 1..2 ok 1 - strlen: bar => 3 ok 2 - strlen: foo => 3 Add another test case: strlen => { foo => 3, bar => 3, quux => 4, ..., }, And your test still works: 1..3 ok 1 - strlen: bar => 3 ok 2 - strlen: quux => 4 ok 3 - strlen: foo => 3 Yay. DETAILSI'm not in a prose-generation mood right now, so here's a list of things to keep in mind:
EXPORTruntestsRun the tests. Only call this once.BUGSReport them to RT, or patch them against the git repository at:git clone git://git.jrock.us/Test-TableDriven (or <http://git.jrock.us/>). AUTHORJonathan Rockway "<jrockway AT cpan.org>".COPYRIGHTThis module is copyright (c) 2007 Jonathan Rockway. You may use, modify, and redistribute it under the same terms as Perl itself.
Visit the GSP FreeBSD Man Page Interface. |