Hello dear esteemed monks,
More than once I felt an urge to put a piece of a unit test script into production code to see what's actually happening there.
Now there is excellent Test::More that provides a very terse, recognizable, and convenient language to build unit test. Unfortunately, it is not quite useful for production code. There are also multiple runtime assertion solutions, but they mostly focus on optimizing themselves out.
My new module Assert::Refute is here to try and bridge the gap. The usage is as follows:
use My::Module;
use Assert::Refute qw(:all), {on_fail => 'carp'};
use Assert::Refute::T::Numeric;
my $foo = My::Module->bloated_untestable_method;
refute_these {
like $foo->{bar}, qr/f?o?r?m?a?t/;
can_ok $foo->{baz}, qw(do_this do_that frobnicate);
is_between $foo->{price}, 10, 1000, "Price is reasonable";
};
And this can be copied-and-pasted verbatim into a unit testing script. So why bother with runtime assertions? Possible reasons include:
-
Testing the method requires many preconditions/dependencies;
-
The method includes side effects and outside world interactions that are not easily replaced with mocks;
-
The method only misbehaves sporadically, doing what it should do most of the time, and the exact conditions required are not known;
-
The method needs to be refactored to be properly tested, and needs test coverage to be refactored.
Main features include:
-
refute and subcontract calls allow to build arbitrarily complex checks from simple ones;
-
refute_these {...} block function to perform runtime assertions;
-
Prototyped functions mirroring those in Test::More to allow for moving checks between runtime and test scripts for optimal speed/accuracy tradeoff;
-
Object-oriented interface to allow for keeping the namespace clean;
-
Simple building and testing of custom checks that will run happily under Test::More as well as Assert::Refute;
-
Reasonably fast, at around 250-300K refutations/second on a 2.7GHz processor.
This project continues some of my previous posts. Despite humble 0.07 version and documentation mentioning it's alpha, I think it is ready to be shown to the public now.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.