Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Does anybody write tests first?

by moritz (Cardinal)
on Feb 22, 2008 at 11:14 UTC ( [id://669524]=note: print w/replies, xml ) Need Help??


in reply to Does anybody write tests first?

I found that testing helps a lot, and still I don't really test many of my perl scripts.

What helped is to move the functionality to modules, because IMHO modules are much neater to test than scripts.

So I started to write modules that do 95% of the work, and the scripts that use these modules just do some command line processing, but no real logic.

And yes, often I write the tests first. That's especially useful if you have a small, complex piece of code. Like a recursive regex that parses nested constructs.

Then you can build a basic regex, run the tests, refine it, run the tests, ... until you're done. And you don't fall into the trap of modifying something and getting what you want, but having undetected side effects. (At least you're not trapped that easily ;)

Replies are listed 'Best First'.
Re^2: Does anybody write tests first?
by andreas1234567 (Vicar) on Feb 22, 2008 at 11:32 UTC
    What helped is to move the functionality to modules, because IMHO modules are much neater to test than scripts.
    True, but it doesn't have to be like that.

    Keep the script, but encapsulate all code in subroutines except the last line below. The last line ensures that one can require the script without actually running its' contents. I argue that this script is no harder to test than a module.

    # script.pl sub foo { ...; $retval; } sub bar { ...; $retval; } sub main { foo() or die(); bar() or die(); } # Call main only if no stack. Non-empty stack means we're testing. # See chapter 9 of Perl Testing - A Developer's Notebook main() unless caller;
    --
    Andreas

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2024-03-29 07:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found