Reputation: 25425
I'm building a perl application as a "modulino" to make testing easier. The directory structure looks something like this:
MainScript.pm
t/001_load.t
t/002_setup.t
t/003_etc...
The first test in each .t test file also loads the module with:
BEGIN { use_ok('MainScript'); }
For testing, I run the following in the top level directory:
prove -l
Everything up to this point works great. Where I run into a concern is when I accidentally run the .t test scripts directly.
My fingers have an almost autonomic habit of hitting the hotkey to run a script immediately after saving it in my text editor. When I do this with one of the test files, it dutifully runs and reports failure across the board. This is expected since it can't see the module if run from its own directory. My concern is that some tests write to the file system. Examples being config files and data files used for verification. My preference would be for the test script to croak before it does anything if it can't load the module it's supposed to be testing. That way, I don't have to worry about anything going haywire from the rest of the test.
The solution I'm currently working with is to replace the BEGIN { use_ok('MainScript'); }
line with a standard use call:
use MainScript;
Running test with prove -l
still works as expected. Even though the use_ok test was removed it's obviously okay since the rest of the file runs. If, for some reason, the test can't find the module, it chokes and "prove" still shows the test file as failing. If I run the script directly from the editor, it fails at compile time with a message that it can't find the requested module in @INC. This seems like a desirable behavior to me.
Is there a better way to do this, and/or is there something in this approach that will bite me down the road?
Upvotes: 1
Views: 127
Reputation: 62099
That's the way I've always done it.
I have one test (usually named 00-load.t
) that does use_ok
and nothing else (except print the version number). All the tests that actually exercise the module do a normal use
, because if that fails, there's no point in proceeding with the tests.
Upvotes: 4