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

Greetings, Monks!

I'm writing up the tests for a module I'm writing, and having noticed that I was writing the same thing over and over in different test files, I factored them out and put them into a .pm file in the t/ directory.

Now I've got error messages with ./Build test that I don't have with prove -I lib t/testname.t.

I'm confidant I can make the error messages go away, so don't worry about that. What I'd really like is a solid grounding in how the environment is different, so any pointers to good docs would be appreciated.

My questions are:

  1. Is that a good way to do this, or is there a better way to factor out common testing-only code?
  2. Is there anything about running in the test harness that I should watch out for?

UPDATE: Ah hah! I know why there are warnings under ./Build test and not under prove. While I had 'use warnings' on, I hadn't added the '-w' flag to prove. Running ./Build test runs the tests with the -w flag. Adding the '-w' flag to prove, makes it show the warnings just like ./Build test does. Apparently, I should have put 'use warnings' in a BEGIN block.

Yet Another UPDATE.. Okay, nix on the BEGIN block. Maybe $^W will do what I want...

Replies are listed 'Best First'.
Re: Tests in a module/package?
by gwadej (Chaplain) on Mar 11, 2009 at 20:15 UTC

    My normal approach for handling this case is to make a lib directory under t directory for test library code. I can then use FindBin to add that directory to any test files that need it. I'm not sure where I got this idea. (I'm pretty sure it's not mine.)

    This allows me to keep testing-specific utility code out of t/ but close by.

    G. Wade
      Remember to write tests for the test library.

      Tests written using Test::Builder can easily be tested using Test::Builder::Tester. This is pretty good described in Perl Testing.

      --
      No matter how great and destructive your problems may seem now, remember, you've probably only seen the tip of them. [1]
      Specifically, the usual idiom is to use FindBin with a "use lib" call, like this inside your *.t code:
      use FindBin qw( $Bin ); use lib "$Bin/lib"; use My::Special::Purpose::Test::Code qw( :all );
      This is the usual way to specify a module location relative to the script location.
Re: Tests in a module/package?
by shmem (Chancellor) on Mar 11, 2009 at 22:11 UTC
    Apparently, I should have put 'use warnings' in a BEGIN block

    Well now. You know that warnings is lexically scoped? It would be confined to the BEGIN block, then.

      Woah.. is there any way to turn on warnings for compile inside of a module?

        Of course:

        package Module; use warnings;

        ;-)