Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Wrapping Test::More assertions in Test::More utility classes?

by silkybones (Initiate)
on Sep 25, 2012 at 15:56 UTC ( #995582=perlquestion: print w/replies, xml ) Need Help??
silkybones has asked for the wisdom of the Perl Monks concerning the following question:

Greetings Monks. Long time listener. First time caller.

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.

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.

Basically, the client of the Test::More extension/wrapper class would do things like this:

use MyTestMoreRunner; my $testMoreWrapper = MyTestMoreWrapper->new( # the runner will set Test::More's test => 20 numTests => 20, # the class I'm testing testClass => 'theClassImTesting', ); $testMoreWrapper->runTest( # test name (Test::More assertion's 3rd arg testName => "testSomething returns 20", # these get passed to theClassImTesting params => { foo => 'foo', bar => 'bar' }, # assertions on what theClassImTesting returns is => 20 ); $testMoreWrapper->done();

The point of this is that I'd like my test classes to not have to use Test::More. 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:

sub okExplain { my ($result, $desc, $dumpMeOnFailure, $moreDiag) = @_; $moreDiag ||= ""; ok($result == 1, $desc) || diag(explain($dumpMeOnFailure), $moreDi +ag); }

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?

Replies are listed 'Best First'.
Re: Wrapping Test::More assertions in Test::More utility classes?
by tobyink (Abbot) on Sep 25, 2012 at 16:43 UTC

    You're on the right track with Test::Builder. You are right that Test::Builder doesn't use Test::More. In fact, the inverse is true: Test::More is based on Test::Builder.

    Take a look at, say, Test::Warn and see how it uses Test::Builder to create a set of extra testing keywords that plays nicely with Test::More.

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
      Thanks, I'll look at that.

      Also, so it seems like creating the type of object I mention actually works to my surprise. I'll assume for now that creating more than one of these objects, in different threads for example, might confuse Test::More. We'll see. I'll post here with updates.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://995582]
Approved by Marshall
Front-paged by Old_Gray_Bear
and cookies bake in the oven...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (10)
As of 2018-05-23 21:27 GMT
Find Nodes?
    Voting Booth?