Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Testing for Beginners

by stvn (Monsignor)
on Apr 08, 2004 at 21:43 UTC ( [id://343771]=note: print w/replies, xml ) Need Help??


in reply to Testing for Beginners

markmoon,

Everyone here has given some great advice, but for me the testing lightbulb went off when I first really saw and messed around with an existsing test script. It was always too easy for me to put off reading tutorials and such (yes, the wrong kind of laziness), but when i finally got in front of a test that was already set up and ready to go, it all clicked. So in the spirit of passing on what worked for me, I have mocked up a quick test script that might help you get your feet wet.

First things first, you need to download and install the latest versions of Test::Simple (which has Test::More in it, which is your workhorse), and Test::Harness. You more than likely already have both on your system, but it can't hurt to get them up to date, and plus, Test::Harness comes with this great utility script called "prove", which makes running tests a lot simpler.

Now, here goes the code, it is purposely VERY simple so as not to cloud the issue, which is testing, with funky code.

Put this code into a file called "Module.pm"

package MyModule; use strict; use warnings; sub add { my ($left, $right) = @_; return $left + $right; } 1; __END__

Put this code into a file called "test.t"

#!/usr/bin/perl use strict; use warnings; # we have 3 tests to run use Test::More tests => 3; # if you dont know how many tests # you have you can just replace: # tests => 3 # with: # "no_plan" # that way you dont have to count # your tests, although when you are # finished its best to put the plan back # in place. # test that we can require the module require_ok("MyModule.pm"); # it also has the added benefit of requiring # the actual module # test that the module has the function "add" available can_ok("MyModule", 'add'); # now test "add" works as expected cmp_ok(MyModule::add(1, 1), '==', 2, '... 1 added to 1 is 2');

Now make sure both files (MyModule.pm and test.t) are in the same directory, and then run this from the command line (assuming you have already cd/chdir to the same directory).

prove test.t
You should see the following output:
test....ok + All tests successful. Files=1, Tests=3, 0 wallclock secs ( 0.15 cusr + 0.05 csys = 0.20 C +PU)
And thats all. Obviously a more complex module would require a more complex tests, and take note, my tests are very simplistic. But this might give you help in getting started, as it helped me.

-stvn

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (1)
As of 2024-04-25 00:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found