Example 5-14. examples/intro/t10_calc.t
#!/usr/bin/perl
use strict;
use warnings;
my %tests = (
'1 + 1' => 2,
'2 + 2' => 4,
'2 + 2 + 2' => 6,
'1+1' => 2,
'0+ -1' => -1,
'0-1' => -1,
'-1+1' => 0,
);
use Test::Simple tests => 7;
foreach my $t ( keys %tests ) {
ok( `./mycalc $t` == $tests{$t}, $t );
}
Example 5-15. examples/intro/t10_calc.out
1..7 ok 1 - 0-1 ok 2 - 1 + 1 ok 3 - -1+1 ok 4 - 0+ -1 ok 5 - 1+1 not ok 6 - 2 + 2 + 2 # Failed test '2 + 2 + 2' # in t10_calc.t at line 18. ok 7 - 2 + 2 # Looks like you failed 1 test of 7.
There is a small problem though. When you add a new test to the hash, you also have to remember to update the tests => 7 line.
There are a number of solution to this problem
| Prev | Home (Copyright Gabor Szabo) Perl Training Israel | Next |
| Add names to the tests | Up | Load Test::Simple at run time |