http://qs321.pair.com?node_id=1222488

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

Test::Exception, Test::Mock, Test::More, and Test::Most have sufficed for my needs thus far.

Can I count on their test routines, ok, dies_ok, is_deeply, etc., to return boolean success values? It seems undocumented and unnecessary to TAP.

My question can be expanded to include other Test::* modules not on my radar.

Thanks,
rir

Replies are listed 'Best First'.
Re: Test::More::ok and her ilk
by Athanasius (Archbishop) on Sep 17, 2018 at 03:53 UTC

    Hello rir,

    Looking at the source code for just the ok method in Test::More, version 1.302140:

    1. sub ok (lines 317-22) returns the result of calling ok via Test::More->builder.

    2. Test::More->builder “Returns the Test::Builder object underlying Test::More” (line 1774). This is implemented in Test::Builder::Module: it simply returns Test::Builder->new (lines 171-3).

    3. The new method of Test::Builder returns a singleton Test::Builder object.

    4. So the underlying ok method invoked is Test::Builder->ok (lines 637-98) which explicitly converts its $test argument into a boolean value (line 644):
      sub ok { my( $self, $test, $name ) = @_; ... $test = $test ? 1 : 0; ... return $test; }

    Clear? ;-)

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: Test::More::ok and her ilk
by Your Mother (Archbishop) on Sep 17, 2018 at 02:57 UTC

    If I understand then yes you can. It’s a kinda convoluted, expensive, and hacky way to do what I think you’re asking. Plus you’ll probably have to, at some point, munge the IO layers so the TAP output isn’t visible. FWIW, I rely on it frequently in testing.

    ok some_test(), "Some test passed" or diag "BUNCH O'DEBUG INFO or $@ or whatever...";

    There is a Test::* space module that purports to run tests without output but I can’t remember the name because when I tried to use it a few months ago it did not work as advertised so I wouldn’t be recommending it even if I could remember it. :P

Re: Test::More::ok and her ilk
by LanX (Saint) on Sep 17, 2018 at 02:35 UTC
    Not sure what I'm missing but isn't it obvious?

    Maybe rephrasing your question helps.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Re: Test::More::ok and her ilk
by tobyink (Canon) on Sep 18, 2018 at 08:10 UTC
Re: Test::More::ok and her ilk
by Anonymous Monk on Sep 17, 2018 at 15:21 UTC

    I have counted on this behavior for years. Your question has made me realize that it is, in fact, not documented anywhere obvious. I think I picked up the habit because of the examples given in the Test::More documentation for BAIL_OUT and diag.

    Now, Test::More is implemented in terms of Test::Builder, and the latter's documentation does say that all tests return true if the test succeeds and false if it fails. So the de facto answer appears to be "yes, you can." But the paper trail seems to be lacking, so I would say the de jure answer is "no, you can not." Maybe a documentation bug report should be filed?

      We wrote those test functions all to return true or false respectively, so that the ok() ... or diag() pattern would work, for example.. It's documented in Test::More (I just checked), but it's not super obvious. I'm sure documentation patches would be welcome.

        My question was poorly made. There is an abundance of test programs and I am interested in the broad group of TAP emitting Perl testing programs. While I think most of the significant/popular modules follow Test::More's behaviour, I have no sound basis to support the thought. That thought is what I'd like to confirm or debunk before starting a project.

        (Update: "No sound basis"--like I don't even know which see much use.)

        I enjoyed your book with Langworth. I didn't care for the antiquing but you two delivered the high quality it was meant to suggest.

        rir