Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^2: Inverting test conditions in Test::More ? (workaround)

by LanX (Cardinal)
on Jul 24, 2020 at 11:06 UTC ( #11119747=note: print w/replies, xml ) Need Help??


in reply to Re: Inverting test conditions in Test::More ?
in thread Inverting test conditions in Test::More ?

Thanks, finally someone accepting my question! :)

> There doesn't seem to be a simple, generic way to negate an arbitrary test in Test::More AFAICT.

I just realized that all tests return a boolean result.

So here a workaround:

use strict; use warnings; use Test::More; sub fail_ok (&;@) { my ($code,$description) = @_; my $result; TODO: { local $TODO= "should be silent"; my $result = $code->(); } $result ? fail($description) : pass($description); } fail_ok { is(1,2,"") } "Bingo! Test failed"; done_testing;

The TODO block ensures that there is no failed test in the final result.

But I still need to understand how to make the output of the failed inner test silent ... °

... I probably need to redirect the STDERR output of Test::Builder or fiddle with TAP altogether

-*- mode: compilation; default-directory: "d:/exp/" -*- Compilation started at Fri Jul 24 13:02:20 C:/Perl_524/bin\perl.exe -w d:/exp/pm_negate_test.pl not ok 1 - # TODO should be silent # Failed (TODO) test '' # at d:/exp/pm_negate_test.pl line 21. # got: '1' # expected: '2' ok 2 - Bingo! Test failed 1..2 Compilation finished at Fri Jul 24 13:02:21

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery

°) Hmm... probably with subtests...

Replies are listed 'Best First'.
Re^3: Inverting test conditions in Test::More ? (workaround)
by Arunbear (Prior) on Jul 24, 2020 at 13:43 UTC

    Are you testing regular code, or testing other tests?

    If the former, why not just something like
    my $cond = 1 == 2; ok( $cond ); # fails ok( ! $cond ); # passes
    ... or whatever boolean condition.

    If the latter, Test::Builder::Tester seems designed to test other tests.

    To be fair, I could be misunderstanding - this is not a use case I've come across before.
      > If the former, why not just something like

      I'm looking for a generic approach, is_deeply is an example where its not that easy, but reiterating an example with is_deeply would lead the thread again into a different direction.

      So either I give a trivial example which is too easy or a complex where people are concentrating on the concrete example instead discussing the abstraction.

      > If the latter, Test::Builder::Tester seems designed to test other tests.

      Good call! looks promising, thanks. :)

      > Are you testing regular code, or testing other tests?

      Both. I already experimented with Test::Builder to generate another Test object redirecting the output.

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

Re^3: Inverting test conditions in Test::More ? (workaround)
by LanX (Cardinal) on Jul 24, 2020 at 14:43 UTC
    > But I still need to understand how to make the output of the failed inner test silent ... °

    FWIW, this will silence the output, tho using a new Test::Builder object or using Test::Builder::Tester might be the better approach.

    use strict; use warnings; use Test::More; my $Test = Test::Builder->new; sub fail_ok (&;@) { my ($code,$description) = @_; # --- silence output my $ignore; $Test->output(\$ignore); $Test->failure_output(\$ignore); $Test->todo_output(\$ignore); my $result; TODO: { local $TODO= "should be silent"; #my $result = subtest "Negation of $description", $code; my $result = $code->(); } # --- undo silence $Test->reset_outputs; # --- output result $result ? fail($description) : pass($description); } fail_ok { is(1,2,"") } "Bingo! Test failed"; fail_ok { is(1,3,"") } "Bingo! Test failed again"; done_testing;

    C:/Perl_524/bin\perl.exe -w d:/exp/pm_negate_test.pl ok 2 - Bingo! Test failed ok 4 - Bingo! Test failed again 1..4

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11119747]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2020-09-26 16:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If at first I don’t succeed, I …










    Results (141 votes). Check out past polls.

    Notices?