in reply to Better arranging Test Suite

I usually do this sort of things with a data structure of my tests and a simple loop with the repetitive test code. E.g.

#!/usr/bin/perl use strict; use warnings; use Test::More; # no plan here -- plan later use Test::Cmd; my @cases = ( # label, args, exit, expected, regex [ "plain", "", 0, "Linux", "Linux" ], [ "with node", "-n", 0, "Desktop1", "Desktop" ], ); my $tests_per_case = 3; plan tests => $tests_per_case * @cases; for my $case ( @cases ) { my ($label, $args, $exit, $expected, $regex) = @$case; my $cmd = Test::Cmd->new( prog => "/bin/uname $args" ); is $cmd->run, $exit, '$label\: uname ran okay'; is $cmd->stdout, $expected, "$label\: Test uname $args"; like $cmd->stdout, qr/$regex/, "$label\: Found uname parameter '$r +egex'"; }

-xdg

Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Replies are listed 'Best First'.
Re^2: Better arranging Test Suite
by piew (Initiate) on Aug 12, 2007 at 22:41 UTC
    Thank you both. Both have better and cleaner approach.

      Corion's approach works well when the data is pretty static. I often wind up needing perl data structures as input to my case, so I specify my cases in code rather than in DATA. But both are the same basic approach.

      -xdg

      Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.