Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

TAP test question

by knbknb (Acolyte)
on Jun 04, 2008 at 13:50 UTC ( [id://690140]=perlquestion: print w/replies, xml ) Need Help??

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

I want to test if the @EXPORT_TAGS ( 'all' => qw(sub1 sub2 sub3 )) 'shortcut' works properly. Is this the right way to do it? I want some info printed out so I wrap the use_ok() function into another ok() function, because use_ok() does not accept a descriptive string as a third argument, AFAK. Or is this just too quick and dirty?
ok( use_ok('MyModule::Common', ':all'), "Can use ':all' to import all subs that MyModule::Common exports " ) or exit;

Replies are listed 'Best First'.
Re: TAP test question
by brian_d_foy (Abbot) on Jun 04, 2008 at 14:52 UTC

    When I want to test that a subroutine is now in my namespace, I check that the subroutine isn't yet defined, do the import, then check that the subroutine is defined:

    BEGIN { ok( ! defined( &some_func, "some_func not defined yet" ); use_ok( 'MyModule::Common', ':all' ); ok( defined( &some_func, "some_func was imported" ); }

    or if I care about testing the import method because I'm doing something tricky:

    BEGIN { my $class = 'MyModule::Common'; ok( ! defined( &some_func, "some_func not defined yet" ); require_ok( $class ); ok( $class->import, "import returns true" ); ok( defined( &some_func, "some_func was imported" ); }

    If you're relying on %EXPORT_TAGS and what to make sure it exports everything it is supposed to (meaning, most likely, that you told it to export the right things, you can look in %EXPORT_TAGS and see if each of those things are now defined in your current namespace. That can be a bit messy though.

    Generally, I like to give each function its own test file, where I do extensive testing on it. If it should be in the :all tag, I'll test that it is exported just like I did in my code example. I don't worry about testing the the :all tag in a single test. I let the individual subroutine tests figure that out.

    --
    brian d foy <brian@stonehenge.com>
    Subscribe to The Perl Review
      Thanks for answering. Please see my reply to the first answer on what I intended to do. my second post was submitted a bit late.
Re: TAP test question
by moritz (Cardinal) on Jun 04, 2008 at 14:01 UTC
    Don't nest use_ok in ok().
    use_ok('MyModule::Common', ':all');

    I don't know if that's enough for you to test, but basically I would assume that Exporter does its job.

      I am assuming that Exporter does its job. I just want to make sure that it is indeed the ':all' marker that is available to MyModule.pm programmer... as long as the module is in development. In case someone decided to rename it to something .. e.g 'all' or ':All'. This is what the output looks like when I don't nest
      ok 3 - use MyModule::Common;
      nested ok(use_ok()) -- results in two tests
      ok 4 - use MyModule::Common; ok 5 - Can use ':all' to import all subs that MyModule::Common exports
      nested - mistyped ':All' marker
      ok(use_ok('ICDP::Common', ':All'),
      "All" is not defined in %MyModule::Common::EXPORT_TAGS at /usr/local/s +hare/perl/5.8.8/Test/More.pm line 676 not ok 3 - use MyModule::Common; # Failed test 'use MyModule::Common;' # at .../t/MyModule/Common/Common.t line 23. # Tried to use 'MyModule::Common'. # Error: Can't continue after import errors at (eval 5) line 2 # BEGIN failed--compilation aborted at (eval 5) line 2. not ok 4 - Can use ':all' to import all subs that MyModule::Common exp +orts
      Another matter would be to test that the list of subs corresponding to the :all label matches some expected values. How do I do that? Well, I think I must re-read the Test::XXX documentation first.
        Another matter would be to test that the list of subs corresponding to the :all label matches some expected values. How do I do that?

        You can look at the symbol table (see perlmod for details) and see if all the expected symbols were exported.

        But I advice against it, because then you have to maintain the list of what :all means in two places, and repetition is your biggest enemy when writing and maintaining software.

Re: TAP test question
by chromatic (Archbishop) on Jun 04, 2008 at 17:22 UTC

    If you want a descriptive string, call diag().

      Thanks, chromatic. By the way, just this morning I had another quick look into your testing handbook. I've enjoyed it very much when I read it the first time, many months ago. And I still like it.
      To sum up, I'll use
      use_ok('MyModule::Common', ':all') or exit; diag( "Can use ':all' to import all subs that MyModule::Common exports + ");
      ,then.
        --- oops. wasn't logged in. The previous poster was me, knbknb.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2024-04-23 11:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found