develooper Front page | perl.qa | Postings from January 2007

Re: Bad test functions in Test::Exception

Thread Previous | Thread Next
From:
A. Pagaltzis
Date:
January 30, 2007 10:09
Subject:
Re: Bad test functions in Test::Exception
Message ID:
20070130180938.GS2896@klangraum
* Nadim Khemir <nadim@khemir.net> [2007-01-30 18:20]:
>   # Check that a test runs without an exception
>   lives_and { is $foo->method, 42 } 'method is 42';
> Isn't this equivalent to is($foo->method, 42 , 'method is 42')?
> The test framework will catch the error if any. It's just
> weird to attempt to catch something when the expected result is
> to pass.

No. The framework won’t catch the exception and the test script
will die at that point. You would have to catch the exception
yourself, then check if you got one. To get the same effect as

    lives_and { ok die, "foo" };

you would have to write

    eval { ok die, "foo"; 0 } or do { fail; diag "died: $@" };

Note how layout-sensitive this code is. If you change it just a
bit, you will run different numbers of tests depending on whether
the exception gets thrown or not.

You could also always run two separate tests:

    local $@ = "";
    ok eval { die }, "foo";
    ok ! $@, "foo didn't die";

But that’s an extra test. `lives_and` removes all the headache
and makes sure you can write a semantically singular test as a
single test.

-- 
*AUTOLOAD=*_;sub _{s/(.*)::(.*)/print$2,(",$\/"," ")[defined wantarray]/e;$1}
&Just->another->Perl->hack;
#Aristotle

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About