<?xml version="1.0" encoding="windows-1252"?>
<node id="995582" title="Wrapping Test::More assertions in Test::More utility classes?" created="2012-09-25 11:56:25" updated="2012-09-25 11:56:25">
<type id="115">
perlquestion</type>
<author id="995549">
silkybones</author>
<data>
<field name="doctext">
&lt;p&gt;Greetings Monks.  Long time listener.  First time caller.&lt;/p&gt;

&lt;p&gt;Basically, I'm using Test::More to deal with Unit-testing a huge and unruly codebase, but I want to be able abstract out common Test::More assertion wrappers so I can re-use them.&lt;/p&gt;

&lt;p&gt;I've looked at TAP::Harness, Test::Builder, and other modules.  The only one that seems to get close to what I need is Test::Builder (which is not based on Test::More) because you can actually call new(), or create() on the class, and then set the number of tests, and tell the class when to run tests.&lt;/p&gt;

&lt;p&gt;Basically, the client of the Test::More extension/wrapper class would do things like this:&lt;/p&gt;

&lt;code&gt;
use MyTestMoreRunner;
 
my $testMoreWrapper = MyTestMoreWrapper-&gt;new( 
    # the runner will set Test::More's test =&gt; 20
    numTests =&gt; 20,
    # the class I'm testing
    testClass =&gt; 'theClassImTesting',
);

$testMoreWrapper-&gt;runTest(
    # test name (Test::More assertion's 3rd arg
    testName =&gt; "testSomething returns 20",
    # these get passed to theClassImTesting
    params   =&gt; { foo =&gt; 'foo', bar =&gt; 'bar' },
    # assertions on what theClassImTesting returns
    is       =&gt; 20
);

$testMoreWrapper-&gt;done();
&lt;/code&gt;

&lt;p&gt;The point of this is that I'd like my test classes to not have to &lt;code&gt;use Test::More&lt;/code&gt;.  I want my test classes to to able to share functions that wrap Test::More assertions, so I don't have to do things like this in each of my test classes:&lt;/p&gt;

&lt;code&gt;
sub okExplain {
    my ($result, $desc, $dumpMeOnFailure, $moreDiag) = @_;
    $moreDiag ||= ""; 
    ok($result == 1, $desc) || diag(explain($dumpMeOnFailure), $moreDiag);
}
&lt;/code&gt;

&lt;p&gt;Is there some way I can create such an object that wraps Test::More assertion methods and can dynamically set the number of thests, all without losing the Test::More runtime flow?&lt;/p&gt;</field>
</data>
</node>
