in reply to Writing code using Test::More -- how to fail within subs?

Test::Class would be one solution. Something like this should do the job (untested code):

{ package BlahBlah::Test; use base qw(Test::Class); sub test_object_emit { my ($self, $test_object, $emit_method) = @_; my $ot = output_test->new( $self->current_method ); my $outfile = $ot->openout(); $test_object->$emit_method($outfile); $ot->ok_files_match(); }; sub out1 : Test(tests => 4) { my $self = shift; my $x = new BlahBlah $x->foo(5000); $x->bar(200); $self->test_object_emit($x, "emit"); }; sub out2 : Test(tests => 4) { my $self = shift; my $x = new Fribble $x->ni(12); $self->test_object_emit($x, "super_emit"); }; # ... etc ... }; BlahBlah::Test->runtests;

We name our test methods after each output_test. Since $self->current_method returns the name of the running test method we can use this to create the appropriate output_test object in test_object_emit.

It you die in a Test::Class test method the remaining tests in that method are automatically failed. So if anything in test_object_emit dies all the tests in the calling test method will fail, and you'll go on to the next test case (aka method).

I'm assuming that ok_files_match runs four tests (hence the tests => 4).

Replies are listed 'Best First'.
Re: Re: Writing code using Test::More -- how to fail within subs?
by John M. Dlugosz (Monsignor) on Feb 09, 2003 at 04:47 UTC
    Thanks, I'll read the docs on Test::Class. But what's an xUnit? The description explains “Easily create test classes in an xUnit style.” and that's not very enlightening since I've never heard of xUnit style.

      xUnit has become the "generic" name for OO test frameworks similar to Test::Class and Test::Unit.

      The "Unit" is from "unit testing" (although they're useful in other testing areas too) and the "x" refers to the language involved (JUnit == Java, PUnit == Python, etc.) This styles follows SUnit, Kent Beck's Smalltalk testing framework.

      If you're interested take a look at Kent Beck's original paper on SUnit and this list of other xUnit frameworks.