Skip to content

Commit

Permalink
Fixed RT#64268: dying in setup does not skip rest of test
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianh committed Apr 17, 2011
1 parent 7714bc2 commit 224f02b
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 5 deletions.
7 changes: 4 additions & 3 deletions Changes
Expand Up @@ -7,11 +7,12 @@ Changes for Perl extension Test-Class
- Fixed RT#56636: feature request: option to turn off auto-skipping
uncompleted tests (thanks to Ken Fox for suggestion) with addition
of fail_if_returned_early() (thanks to Dave Evans for patch)
- startup and shutdown methods are no longer run if a class will not
- Fixed RT#64268: dying in setup does not skip rest of test
- Startup and shutdown methods are no longer run if a class will not
run any test methods
- Updated acknowledgements
- Added missing filter tests to MANIFEST& distribution

- Added missing filter tests to MANIFEST & distribution
0.36 - or the "Adrian should have released this earlier" release [2010-08-19]
(Thanks to Mark Morgan for doing all the useful work on this one!)
- New add_filter() method allows global filtering of test methods
Expand Down
20 changes: 19 additions & 1 deletion lib/Test/Class.pm
Expand Up @@ -245,10 +245,21 @@ sub _exception_failure {
if defined $Current_method && $method ne $Current_method;
_show_header($self, @$tests);
$Builder->ok(0, "$message died ($exception)");
_threw_exception( $self, $method => 1 );
};

my %threw_exception;
sub _threw_exception {
my ( $self, $method, $optional_value) = @_;
my $class = ref( $self );
$threw_exception{ $class }{ $method } = $optional_value
if defined $optional_value;
return $threw_exception{ $class }{ $method };
}

sub _run_method {
my ($self, $method, $tests) = @_;
_threw_exception( $self, $method => 0 );
my $num_start = $Builder->current_test;
my $skip_reason;
my $original_ok = \&Test::Builder::ok;
Expand Down Expand Up @@ -367,9 +378,15 @@ sub runtests {
foreach my $test ( @test_methods ) {
local $Current_method = $test;
$Builder->diag("\n$class->$test") if $ENV{TEST_VERBOSE};
foreach my $method (@setup, $test, @teardown) {
my @methods_to_run = (@setup, $test, @teardown);
while ( my $method = shift @methods_to_run ) {
_show_header($t, @tests) unless _has_no_tests($t, $method);
$all_passed = 0 unless _run_method($t, $method, \@tests);
if ( _threw_exception( $t, $method ) ) {
my $num_to_skip = _total_num_tests($t, @methods_to_run);
$Builder->skip( "$method died" ) for ( 1 .. $num_to_skip );
last;
};
};
};
foreach my $method (_get_methods($t, SHUTDOWN)) {
Expand Down Expand Up @@ -1658,6 +1675,7 @@ This is yet another implementation of the ideas from Kent Beck's Testing Framewo
Thanks to
Adam Kennedy,
agianni,
Alexander D'Archangel,
Andrew Grangaard,
Apocalypse,
Ask Bjorn Hansen,
Expand Down
29 changes: 29 additions & 0 deletions t/die-in-setup.t
@@ -0,0 +1,29 @@
#! /usr/bin/perl

use strict;
use warnings;

{
package Foo;
use base qw( Test::Class );
use Test::More;

sub setup_method :Test(setup) {
die "oops - we died\n";
}

sub test : Test {
pass "this should never run";
}
}

use Test::Builder::Tester tests => 1;
$ENV{TEST_VERBOSE}=0;
test_out( "not ok 1 - setup_method (for test method 'test') died (oops - we died)" );
test_err( "# Failed test 'setup_method (for test method 'test') died (oops - we died)'" );
test_err( "# at t/die-in-setup.t line 27." );
test_err( "# (in Foo->setup_method)" );
test_out("ok 2 # skip setup_method died");
Test::Class->runtests;
test_test("die in setup caused test method to fail");

2 changes: 1 addition & 1 deletion t/die_before_plan.t
Expand Up @@ -25,6 +25,6 @@ $ENV{TEST_VERBOSE}=0;
test_out("not ok 1 - setup (for test method 'test') died (died before plan set)");
test_fail(+3);
test_err( "# (in Object::Test->setup)" );
test_out("ok 2 - test just here to get setup method run");
test_out("ok 2 # skip setup died");
Object::Test->runtests;
test_test("die before plan");

0 comments on commit 224f02b

Please sign in to comment.