Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

I posted this over at use.perl. I figured I'd stuff it here some someone else may learn from my pain.

I'm such and idiot. I can't believe I never noticed this before. It's quite a dangerous oversight too since it meant tests didn't register a pass or a fail; no output at all which threw off the test counts. Don't let this happen to you.

---some.t--- use Test::More tests => 2; use Error ':try'; use_ok('MyModule'); try { MyModule->foo('badparam'); } catch with MyModule::Exception { pass; } otherwise { fail; }

Of course, if the module throws the exception, we pass. If it throws another exception (or die), we fail. But, if the module unexpectedly succeeds, we neither fail nor pass. That's not so bad right? THe count mismatch would cause the test to fail right? Sure, but picture this:

---some.t--- use Test::More tests => 2; use Error ':try'; use_ok('MyModule'); try { MyModule->foo('badparam'); } catch with MyModule::Exception { pass; } otherwise { fail; } ok('added this test later in this huge test file');

In this scenerio, you've added a test but forgot to update the test plan count. Now you've declared 2 tests and passed 2 tests. No failures and you never know a completely failing test...didn't. This is ever more true if you're using no_plan. Probably another reason not to ever do that. :-)

Ths answer of course is to put a fail in the try block. Not something that seams obvious upon first thought.

---some.t--- use Test::More tests => 2; use Error ':try'; use_ok('MyModule'); try { MyModule->foo('badparam'); fail; } catch with MyModule::Exception { pass; } otherwise { fail; }

Boy do I have a lot of test files to double check. That was a painful lesson.


In reply to How not to test exceptions by jk2addict

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found