Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Unit test of script fails unless Test::Warnings is used?!

by wanna_code_perl (Friar)
on Oct 13, 2019 at 00:38 UTC ( [id://11107390]=perlquestion: print w/replies, xml ) Need Help??

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

Fellow Monks!

I have a script in my latest distribution that I am adding unit tests for. I've gone with the simple caller approach:

bin/script:

#!/usr/bin/perl use 5.010; use strict; use warnings; main(@ARGV) unless caller; sub main { die 'main() was run' }

This script exits with no warnings and normal status when require'd via perl -Ibin -e 'require "script";', but die()s as expected when run directly from the commandline.

t/bin/load.t:

#!perl use 5.010; use strict; use lib qw(bin); use Test::More; #use Test::Warnings ':all'; # <-- BEGIN { eval { require 'script' }; BAIL_OUT("bin/script did not load: $@") if $@; } done_testing;

When run via make test, I get bin/load.t ........ skipped: (no reason given) and 255 exit status. When run via prove bin/load.t, I get the same:

t/bin/00-run.t (Wstat: 65280 Tests: 0 Failed: 0) Non-zero exit status: 255

I don't know why it's failing. Interestingly, it only failed when I started removing extraneous use lines from bin/load.t. Removing Test::Warnings was the one that stopped it from working! If the use Test::Warnings ':all'; line is uncommented, the test script succeeds.

The BEGIN block seemingly makes no difference. The results are the same if the code is run outside of BEGIN { ... }. Also, if I add a die to the top of bin/script it (correctly) fails with the BAIL_OUT() message from bin/load.t.

So the abnormal exit has me confused. Why is it failing, and how do I fix it? I guess the success under Test::Warnings should be a clue, but I would expect the tests to be more sensitive to warnings when Test::Warnings is included (due to the extra test it adds), not less sensitive.

Replies are listed 'Best First'.
Re: Unit test of script fails unless Test::Warnings is used&#8253;
by 1nickt (Canon) on Oct 13, 2019 at 01:10 UTC

    Hi, you should use Test::Exception:

    #!perl use 5.010; use strict; use lib qw(bin); use Test::More; use Test::Exception; lives_ok sub { require 'script' }; done_testing;

    Or better yet, always use Test::Most.

    (Your test gets a fail result under prove because you declare done_testing with no tests run so the harness sees no passes. Add ok 1; and you'll get success.)

    Hope this helps!


    The way forward always starts with a minimal test.

      I use Test::Exception all over this test suite. In fact, it was one of the use lines I deleted. :-)

      Using it for the require test hadn't occurred to me, though. I'll switch to that. Thanks. Edit: How would I replicate the OP's BAIL_OUT semantics when using lives_ok?

      Edit: Either you edited your reply, or I didn't see you answering the bit about why the test failed unless Test::Warnings was included. That makes perfect sense.

        "Edit: How would I replicate the OP's BAIL_OUT semantics when using lives_ok?"

        lives_ok sub { require 'script' } or BAIL_OUT("bin/script did not load: $@");
        :-)

        To make your test suite stop any time a test fails:

        use Test::Most 'die';
        (Although you cannot control the output as with BAIL_OUT, and it will apply to all tests in the file.)

        (Note that Test::Most loads Test::More and Test::Exception as well as others and warnings and strict.)

        Hope this helps!

        Update: showed one-test solution first


        The way forward always starts with a minimal test.

        Because Test::Warnings runs a test of its own, so the harness is satisfied. Output of your test script using Test::Warnings under --verbose:

        $ prove -lrv 11107390.t 11107390.t .. ok 1 - no (unexpected) warnings (via done_testing) 1..1 ok All tests successful.

        Hope this helps!


        The way forward always starts with a minimal test.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2024-04-19 19:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found