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

In reply to Re^8: Developing a module, how do you do it ? by tobyink
in thread Developing a module, how do you do it ? by mascip

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.