Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Testing my carping

by nefigah (Monk)
on Mar 15, 2008 at 07:43 UTC ( [id://674341]=perlquestion: print w/replies, xml ) Need Help??

nefigah has asked for the wisdom of the Perl Monks concerning the following question:

Hello Brethren (and Sisteren!),

I'm going through my first-ever unit testing experience with my first-ever Perl OO module. So far so good, but a couple questions have arisen:

From what I can tell, carp and croak seem to be the way to do "exceptions," correct? (BTW, why the silly names?)

Assuming the above is true, what is the best way to go about testing if something is correctly croaking on invalid input? Should I put the eval blocks right there in the tests, or are there built-in functions that "catch" them? (I couldn't find anything about this in the perldoc testing tutorials or the Q&A here.)

Thanks!


I'm a peripheral visionary... I can see into the future, but just way off to the side.

Replies are listed 'Best First'.
Re: Testing my carping
by tirwhan (Abbot) on Mar 15, 2008 at 09:50 UTC

    Here are some examples on how to test for error messages using CPAN modules.

    Check that code does or does not die (with a specific error message):

    use Test::Exception; dies_ok { testsub("die") } "Dies on inappropriate input"; throws_ok { testsub("die screaming") } qr/arrrgh/, "Dies with appropri +ate error message"; lives_ok { testsub("survive") } "Survives with appropriate input";

    Check that a warning is or is not given:

    use Test::Warn; warning_is { testsub("complain") } qr/nag/, "Gives a specific warning" +; warning_is { testsub("quiet") undef, "No warnings given";

    Check that none of the tests you run emit a warning:

    use Test::NoWarnings;

    For this last one you'll have to add 1 to the number of tests you run (in your Test::More plan), which will then pass whether any of the tests in this file emitted a warning.

    See the perldoc for each of these modules for further examples and documentation.


    All dogma is stupid.
Re: Testing my carping
by kyle (Abbot) on Mar 15, 2008 at 13:28 UTC

    If you don't want to install Test::Exception (or don't want to rely on it), you can do the same kind of thing manually without too much trouble.

    use English '-no_match_vars'; eval { possible_exception_thrower() }; ok( ! $EVAL_ERROR, 'possible_exception_thrower() threw nothing' ); eval { this_should_die() }; ok( $EVAL_ERROR, 'this_should_die() did die' ); like( $EVAL_ERROR, qr/exceptional error text/, 'this_should_die() died for the expected reason );

    You can use the __WARN__ "signal handler" to check if your code warns the way you want.

    { my $warning; local $SIG{__WARN__} = sub { $warning = shift }; is( $warning, undef, 'no warnings recorded' ); code_that_warns(); is( $warning, 'expected warning', 'code_that_warns() does indeed warn' ); $warning = undef; non_warning_code(); is( $warning, undef, 'non_warning_code() is clean' ); }

    If you're going to be pitching (and testing for) more than one warning, then the warning handler should push into an array that you can test against.

Re: Testing my carping
by hipowls (Curate) on Mar 15, 2008 at 08:46 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://674341]
Approved by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2024-04-23 17:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found