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

While reading Code Review - What Medium?. I came up with what may be a fairly basic question. How do you write tests against your code? It sounds to me like sepreate code that reads the code in question. What would that code look like?


UPDATE
I just found testing code. Which has a quite a bit of useful information, or links to useful info.

Thanks
Ted
--
"That which we persist in doing becomes easier, not that the task itself has become easier, but that our ability to perform it has improved."
  --Ralph Waldo Emerson

Replies are listed 'Best First'.
Re: How to write code tests
by Joost (Canon) on Jun 01, 2005 at 15:43 UTC
      the Test::Unit documentation

      Even though I'm biased I'd recommend avoiding Test::Unit and using Test::Class instead. Unlike the former Test::Class is maintained (by me :-) and plays with all the other Test::Builder based Perl test modules.

Re: How to write code tests
by cbrandtbuffalo (Deacon) on Jun 01, 2005 at 16:13 UTC
    Another place to look is in Perl modules you install. When you run the install process, you type "make test" (at lease you should) and a bunch of numbers scroll by. Those are test scripts that are running against the module code to make sure it will run on your system. The tests are distributed with the module itself, so you're free to look at them. Just look in the t directory.
Re: How to write code tests
by tlm (Prior) on Jun 01, 2005 at 18:25 UTC
Re: How to write code tests
by zby (Vicar) on Jun 01, 2005 at 16:44 UTC
    I have just seen it on CPAN: Smart::Comments - a new module by Damian Conway. It is not strictly about testing - rather about debugging and setting assertions but it looks pretty interesting.
Re: How to write code tests
by DrHyde (Prior) on Jun 02, 2005 at 09:12 UTC
    People have answered with the tools they use for testing, but I read your question as being more about how you design your tests.

    It's quite simple. All code starts with a specification (even if just in your head) and some documentation (eg the POD for your module). You write tests to ensure that everything in the spec and the documentation is true. Additionally, once you have written your code you should be aware of where any weak points in it are - where, for example, your off-by-one errors might be lurking - and so you would write extra tests to exercise those bits of the code more thoroughly. It is very important to test and document both for success and for failure. If your docs say that "the age parameter tells how old the person is" then you probably want to test (and document!) what happens when a user enters -1, 1000, and "grapefruit", as well as for reasonable values.

    And when you change the code, you of course have updated the docs too, and you add tests to ensure that the updated docs are still true.

Re: How to write code tests
by adrianh (Chancellor) on Jun 02, 2005 at 08:01 UTC
Re: How to write code tests
by perl_devel (Friar) on Jun 02, 2005 at 04:25 UTC
    Hi,
    You have modules thru which you can test your scripts (Do positive and negative tests ) grouped under directory t with extensions t
    (eg) /t/test.t
    use Test::More
    @result = Testpackage->find(name=>"newterm") ;
    is($result[0]->name(),"newterm","Find with name parameter works") ;
    If the name value returns newterm test passed else failed
    Regards
    P.B.Sathish Kumar
Re: How to write code tests
by chanakya (Friar) on Jun 02, 2005 at 08:35 UTC
    Here's a sample module X::CSV. You can check the test script given below to check how the test is implemented.
    package X::CSV; use strict; use warnings; use Text::CSV; =head1 NAME X::CSV - parse Comma Seperated Value files =head1 SYNOPSIS #create CSV parse my $csvParser = X::CSV->new( data_file => $file_path); eval{ my $data = $csvParser->parse(); }; if($@){ die($@); } sub parse(){ my $self = shift my (@parsed, @csv_source); #open the csv file open(CSV_SOURCE , "<" . $self->data_file) or die "..."; .... .... }
    Here's a sample test file to test the module X::CSV using Test::More
    #!/usr/bin/perl use strict; use warnings; use X::CSV; #Number of tests use Test::More tests =>3 ; #test 1 & 2 BEGIN { use_ok( 'X::CSV' ); } require_ok( 'X::CSV' ) ; #parse the test.csv my $parser = X::CSV->new( data_file => $ENV{TEST_ROOT}.'/t/test.csv' ); my $result = $parser->parse(); my $length = @{$result}; #test 3 #We have an csv file with 3 rows is($length,'3','We have an sheet with 3 rows');
    !!@!!
    May the force be with you