Test::More & Supporting Files

B

Bill

To help with my unit tests (using Test::More), I created a helper
function 'eq_arrayref_and_file', that basically takes an array
reference and compares it to the contents of a static flie. This makes
it easy to test the output of a function by doing something like:

my @results = Foo::somefunction();
ok( eq_arrayref_and_file( \@results, 'test1.out' );

Two questions relating to this:

1) My helper functions are defined in FooHelper.pm. This module is
*only* needed for testing (not running/using the actual Foo.pm module).
Should I place it in the lib directory? Is it possible to place it in
the t/ directory and have "make test" still be able to find it?

2) Is there an ideal spot to put supporting static data files? I.e.,
so test1.out can be found in the above example?

I can hack things up on my machine to make both work, but I'm hoping to
eventually but the result on CPAN and any best practices
recommendations would be appreciated.

Thanks!
-Bill
 
T

Tassilo v. Parseval

Also sprach Bill:
To help with my unit tests (using Test::More), I created a helper
function 'eq_arrayref_and_file', that basically takes an array
reference and compares it to the contents of a static flie. This makes
it easy to test the output of a function by doing something like:

my @results = Foo::somefunction();
ok( eq_arrayref_and_file( \@results, 'test1.out' );

Two questions relating to this:

1) My helper functions are defined in FooHelper.pm. This module is
*only* needed for testing (not running/using the actual Foo.pm module).
Should I place it in the lib directory? Is it possible to place it in
the t/ directory and have "make test" still be able to find it?

Don't put it into the lib/ directory. I think there's a realistic chance
that it will end up being installed into @INC.

But you may put it anywhere else. If you put it into t/, then you have
to add

use lib 't';
use Foo;

to each of your test-scripts. The test-scripts are run from the
base-directory or your distribution. So if you include paths in which
perl is supposed to look for modules with the 'lib' pragma, then specify
those paths relative to the base-directory.
2) Is there an ideal spot to put supporting static data files? I.e.,
so test1.out can be found in the above example?

The same is true for data files that your test-scripts are supposed to
access. A portable way to open them is by using File::Spec:

use Test::More;
use File::Spec;
...
open FILE, "<", File::Spec->catfile("t", "static", "test1.out")
or die $!;

if you want to open t/static/test1.out.

Tassilo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,244
Latest member
cryptotaxsoftware12

Latest Threads

Top