The Extreme Programming people say that the tests are the specification. In other words, if your code passes the tests, it is correct by definition. (Even if your tests are in fact incorrect. :) ). I usually don't write tests before I write code; I'm just not used to thinking that way, but when I do, I think in terms of a specification. What's this class supposed to do? What will its interface be? At the very least, let's assume it's a traditional OO Perl class with a blessed hashref implimentation and a constructor. That gives us some good information to start a simple test file:
use strict;
use warnings;
use Test::More;
use_ok('My::New::Module');
my $obj = My::New::Module->new;
ok( $obj );
isa_ok( $obj, 'My::New::Module' );
We've already tested some very important things: We can load the module, the constructor returns a true value, and that value is an object of the type we've been expecting.
If you're the type of person who can plan your modules completely ahead of time, go ahead and write simple tests for each method that this object will have, making sure they return the correct values and alter the object's internal state properly.
Personally, I tend to develop modules and tests in parallel, writing a feature, making sure it works to my satisfaction, and then writing a test to make sure it continues to work that way. Sometimes I get lazy and don't write the tests until the very end.
The most important thing is not to get too bogged down in dogma. Experiment a lot and do what works best for you and increases your productivity. Don't worry yourself if you're not strictly adhering to "red, green, refactor." Development methodologies should be taken as advice, not instructions. You must always find your own way.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.