Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Test on calling undefined subs fails and exits test

by Don Coyote (Friar)
on Aug 08, 2019 at 11:46 UTC ( #11104160=perlquestion: print w/replies, xml ) Need Help??

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

I am getting Child exited without calling finalize() Errors in my testing. But I haven't read about that anywhere so far.

I am testing imports on string require. The first subtest works, the subs are defined in the calling main, and warn about redefinition. And they are not defined in the calling package.

As the namespace is not imported (there is no package definition in the required file), I expect a test to call a fully qualified routines from the required package to fail.

ok( ! &working_l::setlist, "calling undefined routine fails" ); --- Undefined subroutine &working_l::setlist called at working.t line 33. # Child (calling undefined subs should not work) exited without ca +lling finalize() not ok 3 - calling undefined subs should not work # Failed test 'calling undefined subs should not work' # at C:/Dwimperl/perl/lib/Test/Builder.pm line 252. # Tests were run but no plan was declared and done_testing() was not s +een.

The test appears to exit where I want it to return false here, and continue. The error seems to be fatal. Do I need to start wrapping evals, or which tests should I be making here?

Replies are listed 'Best First'.
Re: Test on calling undefined subs fails and exits test
by Corion (Pope) on Aug 08, 2019 at 12:23 UTC

    Yes. Your tests are a normal Perl program, except that this normal Perl program outputs OK or not OK.

    If you want to prevent an error from terminating your program, you need to wrap the offending line in eval { ... }.

      Yes, that has done the trick. thx

Re: Test on calling undefined subs fails and exits test
by hippo (Canon) on Aug 08, 2019 at 12:55 UTC

    If you expect a test to fail fatally and want to test that then you can use one of the many Test::* modules which help with this such as: Test::Exception, Test::Trap, Test::Fatal, etc.

      I'm taken with Test::Trap, I can be a little clearer with what is happening in the test names.

      The sub leaves by dieing and not exiting. And it is starting to look like the finalize() routine is a test module death. But I already knew that of course

      subtest 'calling undefined routine dies' => sub { my @r = trap{ working_l::setlist() }; is( $trap->leaveby, 'die', "calling &working_l::setlist dies" ); like( $trap->die, qr/(?=.*finalize)/i, "&working_l::setlist dies with + finalize err" ); }; ---output--- ok 1 - require 'working_l.pl'; ok 2 # skip workinprogress ok 1 - calling &working_l::setlist dies not ok 2 - &working_l::setlist dies with finalize err # Failed test '&working_l::setlist dies with finalize err' # at working.t line 36. # 'Undefined subroutine &working_l::setlist call +ed at working.t line 33. # ' # doesn't match '(?^i:(?=.*finalize))' 1..2 # Looks like you failed 1 test of 2.

      Right, they look like the business. ty hippo

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://11104160]
Approved by davies
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (6)
As of 2019-08-23 14:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?