Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: RFC: Basic Testing Tutorial

by stevieb (Canon)
on Jul 07, 2019 at 16:11 UTC ( #11102506=note: print w/replies, xml ) Need Help??


in reply to RFC: Basic Testing Tutorial

Great post, hippo!

Like haukex, I've just got a couple of suggestions. I'll stay away from more advanced testing functionality as this is a "Basic" tutorial.

Ensuring things die() when you expect them to, in combination of the like() and is() functions. If the call within the eval() succeeds, the 1 will be returned. If not, it dies, and the eval() returns undef:

for (-1, 4096){ is eval { $e->read($_); 1; }, undef, "read() with $_ as addr param fails"; like $@, qr/address parameter out of range/, "...and error is sane +"; }

Always use sane test messages, so that it's trivially easy to see the output and quickly identify in your test script where the test actually is. In the above, it specifies exactly what I'm testing (read()), it states that I'm specifically testing the addr param, and even signifies which iteration of the loop did/didn't break ($_).

How to use skip. This is a basic piece of functionality that a blooming unit test writer needs to know. There are several ways and reasons to use this, but I'll stick to the most basic premises:

Skip all tests in a file for any reason:

plan skip_all => "i2c bus currently disabled for other purposes";

Skip all tests in a file based on external flag:

if (! $ENV{SERIAL_ATTACHED}){ plan skip_all => "The damned serial port shorted out!"; }

Skip all tests in a file if certain software isn't installed (stolen from a POD test):

my $min_tpc = 1.08; eval "use Test::Pod::Coverage $min_tpc"; plan skip_all => "Test::Pod::Coverage $min_tpc required for testing PO +D coverage" if $@;

Speaking of testing, yesterday I reached the 10,000 test mark on one of my larger, more elaborate projects :)

Files=61, Tests=10032, 586 wallclock secs ( 8.68 usr  0.50 sys + 149.28 cusr  8.14 csys = 166.60 CPU)

Replies are listed 'Best First'.
Re^2: RFC: Basic Testing Tutorial
by hippo (Chancellor) on Jul 11, 2019 at 09:53 UTC

    Thanks, stevieb. I've added in an example of skip now as that is definitely worth bringing to the beginner's attention.

    Regarding the error trapping, if anything I'd be more inclined to steer the new tester towards Test::Exception or Test::Fatal rather than rolling their own. What's your reasoning for using the bare eval instead?

      "What's your reasoning for using the bare eval instead?"

      Habit, less prerequisites in my distributions, and less abstraction. I'm not saying that a distribution isn't a good idea, I just avoid going that route where I can.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2020-02-22 10:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What numbers are you going to focus on primarily in 2020?










    Results (99 votes). Check out past polls.

    Notices?