in reply to Re^7: Developing a module, how do you do it ?
in thread Developing a module, how do you do it ?
So now you've got to invent names for all your tests. Just so you can search for that name to find the test?
That is asinine make-work.
If I use die. It automatically "names" the test, with the file and line number.
As I have already said, Test::Simple/Test::More, etc give you the file and line number out of the box, without needing to name the test.
But consider:
die "reason"; # versus just die;
If you ever provide an argument for die, you've just provided a name for a test. Is that "asinine make-work"?
Besides which, if the line in question is in a loop, a file name and line number might not be enough - a name can be very useful to figure out what's gone wrong.
{ package Maths; sub factorial { my $n = int(pop); return $n if $n<2; $n * factorial +($n - 1) } } use Test::More; my @expected = qw/ 0 1 2 6 24 100 720 /; plan tests => scalar @expected; is(Maths::factorial($_), $expected[$_], "Factorial of $_") for 0 .. $# +expected;
A failure on line 8 doesn't give you a clue what test has failed. A failure on line 8 named "Factorial of 5" does.
$ perl factorial.t 1..7 ok 1 - Factorial of 0 ok 2 - Factorial of 1 ok 3 - Factorial of 2 ok 4 - Factorial of 3 ok 5 - Factorial of 4 not ok 6 - Factorial of 5 # Failed test 'Factorial of 5' # at factorial.t line 8. # got: '120' # expected: '100' ok 7 - Factorial of 6 # Looks like you failed 1 test of 7.
In this case, looking at the output, it's clear where the failure is, and checking the factorial of 5 on a calculator, it's the expected result which is in fact incorrect - my ultra-useful Maths package appears to be bug-free. Though an improvement might be to die if called with a negative number.
{ package Maths; sub factorial { my $n = int(shift); die "does not compute" if $n<0; return $n if $n<2; $n * factorial($n - 1); } } use Test::More; use Test::Exception; my @results = qw/ 0 1 2 6 24 120 720 /; for (0 .. $#results) { lives_and { is Maths::factorial($_), $results[$_] } "Factorial of +$_"; } dies_ok { Maths::factorial(-2) } "Factorial of negative number"; done_testing;
ok 1 - Factorial of 0 ok 2 - Factorial of 1 ok 3 - Factorial of 2 ok 4 - Factorial of 3 ok 5 - Factorial of 4 ok 6 - Factorial of 5 ok 7 - Factorial of 6 ok 8 - Factorial of negative number 1..8
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^9: Developing a module, how do you do it ?
by BrowserUk (Patriarch) on Mar 04, 2012 at 09:51 UTC | |
by tobyink (Canon) on Mar 05, 2012 at 11:24 UTC | |
by BrowserUk (Patriarch) on Mar 05, 2012 at 11:46 UTC |