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 | |
by adrianh (Chancellor) on Feb 09, 2003 at 10:06 UTC |