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

Dear monks,

I decided it is time to start testing my code. :-) Reading about Test::* educated me on how to write test scripts for my modules. But I'd like to write the tests right into the scripts to be tested because

  1. I don't want to pollute my filesystem with an extra file for each module (I tend to write numerous small ones) and
  2. I also want to test scripts that aren't modules but main() programs.

Is there a way to do this?

Thank you.
~ Sixtease

Replies are listed 'Best First'.
Re: Embedded test
by geekphilosopher (Friar) on Mar 10, 2007 at 00:14 UTC
Re: Embedded test
by gam3 (Curate) on Mar 10, 2007 at 02:50 UTC
    The perl test framework is very flexable and you can do almost anything you want in it.
    # here is a valid test file print "1..2\n"; print "ok 1\n"; print "ok 2\n";
    So you can have as many (or as few) test files as you want, and you can test anything in those files.
    Test::More is a very good test modules with lots of different funtions to test output. There might be a Test module that is designed to test scripts.
    I can see 2 ways to do this. You can either use Open3 to run the script and test the output, or tie PRINT and do the script. The first is likely easier, but the second would give you some more control.
    Good luck and have fun.
    Update Test::Script might have some good ideas on how to test scripts.
    -- gam3
    A picture is worth a thousand words, but takes 200K.

      Thank you for the answers.

      I've had a look at Test::Inline and I have some things unclear. I started with this module (For_test.pm):

      package For_test; use strict; sub two { return 2; } sub three { return 4; } =begin testing ok(two() == 2); ok(three() == 3); =end testing =cut 1

      I created inline2test.conf:

      input=. output=t verbose=1

      The I run perl t/for_test.t which tells me it can't find &main::two. It only helps if I manually add use For_test; to the test script and qualify the testing commands to look like

      ok(For_test::two() == 2); ok(For_test::three() == 3);

      This must surely be wrong - I don't think I should touch the generated test scripts. Also, if I got to where I actually wanted -- to testing *.pl files, not modules, I'd have a hard time supplying the needed use statement.

      What caught my eye was the Open3 module. It looks like it could do what I want but I thought I'd try to find an existing solution before writing my own testing suite. :-)

Re: Embedded test
by rodion (Chaplain) on Mar 10, 2007 at 11:14 UTC
    As others indicate, there is little in the testing modules that relys on tests being in ".t" files. It's a feature for you to use, or not use. It's a very handy convention that will be understood widely, but as always, TIMTOWTDI.

    A really wonderful book on testing, IMO, is Perl Testing: A Developer's Notebook. It's a compact and concise presentation of "here's how to do it" code examples, with supporting explanatory text to take you through the examples quickly. It covers a very wide range of testing scenarios, but it's organized so you don't have to read much more than just the section you're interrested in.

    You can take a look at a sample chapter of the book on-line at the O'Reilly website. The book's Amazon rating is high, but there are a few who really didn't like it, so it's probably a good idea to take a look at the sample.