Perl Unit Testing by bzs12927

VIEWS: 0 PAGES: 7

									 Perl Unit Testing

    October 13, 2004



Perl Unit Testing WikiWiki
     Why perform unit testing?
• Testing shows the presence of errors, not
  their absence
• Builds confidence in application
• Speeds development

• How do we determine if an application is
  100% bug-free? Can we determine if a
  program is 100% bug-free?
 How to implement Unit Testing
• Unit testing not a given – requires some sort of a
  framework
• Last week: h2xs –AXc NewModule
   – Generates Makefile.pl
   – Perl Makefile.pl generates makefile
      • Generate t/ directory with #.t files
      • make tests!
• You may use any other testing framework you
  like, however, it should conform to the ‘make
  tests’ philosophy
      • Unless you supply explicit documentation with your
        submission, or a makefile with a tests rule which does it for me
                 Having a Plan
• A plan is a list of the unit tests you intend to write
• Believe it or not, some people actually develop the
  code according to the unit test plan, rather than the
  other way around
• I will give you a plan by specifying the unit tests I
  want you to create
   – It will not be a great plan, but enough to get you to
     write a couple of unit tests
• Having a plan ensures that your plan will be
  successful! Having no plan is, well, nothing.
          Deploying your plan
1. Find out what you have to do.
2. Write a Unit Test for the desired new capability.
   Pick the smallest increment of new capability
   you can think of.
3. Run the Unit Test. If it succeeds, you're done.
   Go to step 1, or if you are completely finished,
   go home.
4. Fix the immediate problem: maybe it's the fact
   that you didn't write the new method yet. Maybe
   the method doesn't quite work. Fix whatever it
   is. Go to step 3.
     Dirt Simple Example: 1.pl
use Test::More tests => 2;
BEGIN { use_ok('Network') };       # test 1

my $r = new Router( … );

isa_ok($r, ‘Router’);              # test 2

Did I create an instance of the class Router?
           More Information
• Let’s stop here, and pick next week with
  more details on unit testing functions

• More information can be found by typing:
  – perldoc Test::Simple
  – perldoc Test::More

								
To top