use_ok or die, die, die

Ovid on 2007-12-11T14:51:23

I can't tell you how many times I get tired of mysterious test behavior because someone forgot to put an 'or die' at the end of a use_ok() test. With this vim function, typing ',use' will convert all 'use_ok $package;' to have an 'or die' at the end.

map ,use :call UseOrDie()
function! UseOrDie()
    %s/^\(\s*use_ok\s*(\?['"].*['"])\?\)\s*;/\1 or die;/
endfunction

For those not familiar with the problem, let's say you have 300 tests in one .t file. The 'use_ok' fails, but scrolls off the screen and if you forget to fix the first failure first, you get totally mystified by failures which occur later. I've had this happen to me several times and it's frustrating. use_ok() and require_ok() should both die. If you use require_ok(), you can switch the above to this:

map ,use :call UseOrDie()
function! UseOrDie()
    %s/^\(\s*\(use\|require\)_ok\s*(\?['"].*['"])\?\)\s*;/\2 or die;/
endfunction


Why use use_ok, require_ok at all?

IlyaM on 2007-12-11T16:18:35

I just don't use them at all as ordinary use and require cause nearly exactly same test failure if there is something wrong with module you are trying to load.

Re:Why use use_ok, require_ok at all?

Aristotle on 2007-12-11T16:27:07

Agreed.

Another approach...

Alias on 2007-12-11T23:25:01

I use a 01_compile.t in all my modules that contains all the load-integrity testing. So regular use_ok stuff, plugin loading, checking $VERSION values all match, does Exporter export what it is supposed to, etc...

That way I get a full report on load failures, and the rest of the tests can totally ignore the issue and load normally.

Re:Another approach...

Ovid on 2007-12-12T10:41:44

That's what I've done with Test::Harness, but this isn't my code base.

Re:Another approach...

jplindstrom on 2007-12-12T23:29:15

It's our code base, including yours, no?

And I believe we already have a test file that loads all modules, but I'm not 100% certain (well, you shouldn't trust anything I say today anyway 8^)