Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

On my most-recent project, I did something I had not properly done before:   I built an ongoing test-suite, during the development process. As each new feature was added to the code, and sometimes before, I immediately wrote a test-case for it. And I didn't proceed until that test, and all preceding tests, “ran clean.”

This had immediate effects:

  1. Initial code-writing took about twice as long as before.
    • Overall development time was far less.
  2. During this process, “regression” of previously-written code happened at unexpected (and frequent) times.
  3. Writing tests in this way, and at this time, forces you to think more slowly and more deliberately about the code that you write, and thus to write much better code.
    • The “payback” was so obvious, and so immediate, that I became somewhat obsessed, so to speak, with writing and maintaining those test cases. I found myself feeling quite nervous about “putting my full weight upon” a new piece of code that wasn't tested. (Also, once you get into it, writing new tests is engaging and fun.)
  4. The bugs that were uncovered were usually nasty and subtle. I would not have discovered them except under the least-desirable circumstances.
    • The bugs that weren't uncovered were, for the most part, “merely annoying.”
  5. When the code was finished, it was reliable, and I didn't worry about doing demos.
    • In spite of all this, a demo is still the best test-case you can ever have.
    • The probability of a bug appearing during a demo is in direct proportion to your smugness that it won't, plus 100%.   :-D

Fortunately for all of us, Perl makes it very easy to write tests. A test program is simply a Perl program, written using a test-module such as Test::Most, Test::Memory::Cycle, Test::Exception, or Test::Class. You see test-suites run every time you install anything from CPAN, so it's a very well-developed system. When you start writing test-suites of your own, you really appreciate what it means when you see hundreds or even thousands of test-cases flying by during one of those installs. The fact that “CPAN is highly reliable” is anything but an accident.

What I do (and it's not the only way nor necessarily the best way to do it) is simply to create a t subdirectory, for “tests,” with number-prefixed subdirectories and number-prefixed test-programs in each. (This is done because the test files and directories will be traversed in-order.) Then, I run these with the command:   prove -r.

A test-case is very simple, really:   it's both a list of things that you do expect to happen, and things that you don't. It's a test of things that should cause an exception to occur, and things that shouldn't. It's a test of the “edge cases,” the sublime, and the ridiculous.

For web-based applications you can go further. Modules such as Test::WWW::Mechanize can perform an entire web-interaction sequence. Or you have things like Test::WWW::Declare, which is based around the notion of defining a “flow.”

My point here is not to enumerate all of the things that you can do with Perl testing. (There are, today, 805 CPAN modules whose name begins with “Test::” ...) Rather, it is:  “You should do this. It really works.” Sure, I'm not the first person to say it. I'm just one who took about twenty years to really listen.   ;-) ;-) :-D


In reply to Testing IS Development by sundialsvc4

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (7)
As of 2024-04-19 09:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found