Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Lazy test writing?

by pileofrogs (Priest)
on Mar 19, 2009 at 18:46 UTC ( #751832=perlquestion: print w/replies, xml ) Need Help??
pileofrogs has asked for the wisdom of the Perl Monks concerning the following question:

Ahoy, Monks.

What good modules (or other tools) are there for a lazy person writing tests for his modules? My tests seem to include a lot of redundant typing, and I want that to go away.

I'd also like to find a better, more reliable, way to find exhaustive tests than just sitting here saying "Um.. did I try that with a negative number?... How about undef?.. Uh...". I also have a fantasy that there is a tool out there that uses Devel::Cover and either writes your tests for you or writes test skeletons which one could fill in.

Any suggestions would be fantastic.


Replies are listed 'Best First'.
Re: Lazy test writing?
by BrowserUk (Pope) on Mar 19, 2009 at 20:00 UTC
Re: Lazy test writing?
by ELISHEVA (Prior) on Mar 19, 2009 at 20:23 UTC
    Laziness aside, redundant test composing increases the risk of expectation errors. Wanting to get rid of redundancy is an excellent goal.

    One part of the answer is to take a step back an analyze your test suite the same way you do any other pile of code: separate the data from the logic. If you have a group of tests that are the same except for some inputs and expected values, move the inputs and expected values into arrays and hashes. Then write a test suite function that takes those arrays and hashes as a parameter and runs all of the tests. I write almost all my tests this way and it is a life saver.

    Another trick is to group inputs according to expected value. Next write a test suite function that takes an array of inputs, and one expectation. Inside the function loop through the inputs calling your test suite function with the current input and the expected value.

    This is a great way to make sure that the default values really default as they should: one of the inputs contains no undefined values; the rest of the inputs replace parameters that just happen to equal the default value with undef (or omit them all together if at the end). Whether or not a defined or undefined value is in the parameter array, if your default rules are working properly they should have the same expectations. It is also a good way to make sure that exceptions and warnings are being thrown as they should be: in this case your input group is bunch of inputs that are expected to all trigger the same exceptions and warnings.

    Best, beth

Re: Lazy test writing?
by perrin (Chancellor) on Mar 19, 2009 at 19:27 UTC
    It can be a real pain to get started with, but Test::Class is good at removing repetition.
Re: Lazy test writing?
by Bloodnok (Vicar) on Mar 19, 2009 at 19:31 UTC
    Why do you need suggestions from us when you make the suggestion: ...uses Devel::Cover and either writes your tests for you... - go to it :-D

    A user level that continues to overstate my experience :-))

      My experience with Devel::Cover is pretty thin. Is there a way to get it to not only tell me what tests I need but also write them? That sounds a little too good to be true...

Re: Lazy test writing?
by doom (Deacon) on Mar 19, 2009 at 20:03 UTC
    Two thoughts: (1) use some sort of templating system. I use template.el (via perlnow.el) with emacs. You can probably find some way to do it that you like. (2) cut and paste. No, seriously. I think it's usually a mistake to require your test code to meet the same standards as your production code (think about it -- "test first programming" will get you stuck in an infinite loop if you need to test your test code).

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://751832]
Front-paged by Arunbear
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2018-12-11 01:22 GMT
Find Nodes?
    Voting Booth?
    How many stories does it take before you've heard them all?

    Results (52 votes). Check out past polls.

    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!