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

Re: why Test::More?

by Your Mother (Canon)
on Sep 10, 2013 at 13:46 UTC ( #1053264=note: print w/ replies, xml ) Need Help??


in reply to why Test::More?

Testing is really super important in a shop that is serious and trying to not waste anyone's time or accidentally ship stuff sideways to customers. Even in the shortest code or whimsical examples like yours, bugs creep in. Proper tests help ensure they don't go unnoticed.

Perl Testing: A Developer's Notebook (ISBN 0596100922) is really quite good. Pick it up. Make your own big projects better and wow the next interview committee. Other required reading: prove, Test::Simple, Test::More, App::Prove. Plenty of other Test:: space to explore as you like.

Here are your tests rewritten (style differences are up to you, I like to omit parens and explicit returns where possible).

use strictures; use Test::More; # plan test count here or use done_testing($count). is hello_world(), "Hello world!", "Hello world is hello world"; # Naive test, will not catch your bug, and you have one. cmp_ok get_number(), ">", 0, "get_number() returns greater than zero"; subtest "Iterate on get_number()" => sub { my $iterations = 10_000; # Higher confidence. my $ok; for ( 1 .. $iterations ) { $ok++ if get_number() > 0; } my $msg = "get_number() returns greater than zero"; $ok == $iterations ? pass($msg) : fail($msg . " - expected $iterations > 0, got $ok"); done_testing(1); }; done_testing(3); # <- Either "plan" or delcare test count here! exit 0; sub hello_world { "Hello world!"; } sub get_number { int(rand(1000)); }
~>prove my-test.t -v ok 1 - Hello world is hello world ok 2 - get_number() returns greater than zero not ok 1 - get_number() returns greater than zero - expected 10000 + > 0, got 9993 1..1 not ok 3 - Iterate on get_number() 1..3 Dubious, test returned 1 (wstat 256, 0x100) Failed 1/3 subtests Test Summary Report ------------------- /home/moo/my-test.t (Wstat: 256 Tests: 3 Failed: 1) Failed test: 3 Files=1, Tests=3, 0 wallclock secs ( 0.03 usr 0.01 sys + 0.03 cusr + 0.00 csys = 0.07 CPU) Result: FAIL

Found your bug, right? int(rand(whatever)) can return zero. Fix with int(rand(1000)+1) and run again.

ok 1 - Hello world is hello world ok 2 - get_number() returns greater than zero ok 1 - get_number() returns greater than zero 1..1 ok 3 - Iterate on get_number() 1..3 ok All tests successful. Files=1, Tests=3, 0 wallclock secs ( 0.03 usr 0.01 sys + 0.03 cusr + 0.00 csys = 0.07 CPU) Result: PASS


Comment on Re: why Test::More?
Select or Download Code
Re^2: why Test::More?
by Anonymous Monk on Sep 10, 2013 at 15:08 UTC
    this was a super response....thanks

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (9)
As of 2014-04-18 08:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (464 votes), past polls