Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Test::Class and use lib

by friedo (Prior)
on Aug 22, 2005 at 01:50 UTC ( [id://485599]=perlquestion: print w/replies, xml ) Need Help??

friedo has asked for the wisdom of the Perl Monks concerning the following question:

I am using Test::Class to do my unit tests for a large distribution that I'm beginning. I really like Test::Class as I can neatly package stuff up in inherritable methods instead of writing long, spaghetti-esque procedural test scripts which offend my sense of style. (Just ask perrin <grin>).

The docs for Test::Class suggest putting your test modules under t/lib. I understand that this will prevent them from being indexed by PAUSE, which is good. My concern arises over including this directory in my test script, t/main.t, which looks like this:

#!/usr/bin/perl # t/main.t -- launch all tests from here use strict; use warnings; use lib './t/lib'; use Test::Class; use MyApp::Test; use MyApp::Foobar::Test; Test::Class->runtests;

This works fine on my system, but I'm worried that the use lib might break on other OSes, especially with the hardcoded relative path. Is there an accepted standard way of doing this which is better?

Replies are listed 'Best First'.
Re: Test::Class and use lib
by xdg (Monsignor) on Aug 22, 2005 at 10:44 UTC

    Another handy trick I picked up from one of Ingy's presentations at YAPC this year is just naming your test modules along the lines of 't::' and the conversion to a path happens automatically on use. E.g., given t/Test/ModuleA.pm:

    use t::Test::ModuleA;

    It also clearly denotes that a module is a support module for testing, which I personally find helps readability in my test scripts.

    -xdg

    Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

      Another handy trick I picked up from one of Ingy's presentations at YAPC this year is just naming your test modules along the lines of 't::' and the conversion to a path happens automatically on use.

      Nice. ++

Re: Test::Class and use lib
by adrianh (Chancellor) on Aug 22, 2005 at 08:16 UTC
    This works fine on my system, but I'm worried that the use lib might break on other OSes, especially with the hardcoded relative path. Is there an accepted standard way of doing this which is better?

    It should work fine. lib automatically converts unix style paths to the appropriate format for whatever platform it's running under.

Re: Test::Class and use lib
by fokat (Deacon) on Aug 22, 2005 at 05:11 UTC
    The docs for Test::Class suggest putting your test modules under t/lib. I understand that this will prevent them from being indexed by PAUSE, which is good. My concern arises over including this directory in my test script, t/main.t (...)
    This works fine on my system, but I'm worried that the use lib might break on other OSes, especially with the hardcoded relative path. Is there an accepted standard way of doing this which is better?

    Dear friedo

    Your attention to portability, as well as desire to properly bundle test code with your distribution, are indeed very good attitudes.

    That kind of use lib statements would work only when the testing is done from the build directory, which IMHO is what happens almost all of the time. After all, this is intended to run before the code is installed, so there is no standard place to point to anyway.

    That said, I would use the fragment of very untested code below, to construct a hopefully more portable alternative by using File::Spec...

    #!/usr/bin/perl # t/main.t -- launch all tests from here use strict; use warnings; use File::Spec; use lib File::Spec::catdir(qw/. t lib/); # The rest of your code goes here...

    Note that personally, I consider this a bit of overkill :)

    Update: Thanks and ++ to adrianh for pointing out that my overkill was already thought by the authors of lib :)

    Best regards

    -lem, but some call me fokat

      That said, I would use the fragment of very untested code below, to construct a hopefully more portable alternative by using File::Spec...

      You don't need to do this since use lib does it for you :-) From the POD:

      In order to keep lib.pm small and simple, it only works with Unix filepaths. This doesn't mean it only works on Unix, but non-Unix users must first translate their file paths to Unix conventions.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://485599]
Approved by sk
Front-paged by Old_Gray_Bear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (4)
As of 2024-04-20 13:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found