Permalink
Browse files

Don't run *any* test control methods unless we have test methods.

  • Loading branch information...
1 parent 9818f01 commit e98d2e0984cabe1290aeaa98b0a65b53a75d2c7e @Ovid Ovid committed Aug 12, 2010
Showing with 83 additions and 9 deletions.
  1. +3 −0 Changes
  2. +23 −4 lib/Test/Class.pm
  3. +50 −0 t/base-class.t
  4. +4 −4 t/filter_fixtures_only.t
  5. +3 −1 t/show_plan_in_shutdown.t
View
@@ -1,6 +1,9 @@
Changes for Perl extension Test-Class
0.36
+ - Improve documentation on how the test control methods work.
+ - Backwards-incompatible change: Don't run startup/shutdown if we don't
+ have test methods (the setup/teardown were already being skipped).
- Apply t/todo.t patch to fix RT#55324 (test bug, not Test::Class bug).
This makes the test suite pass with Test::Simple 0.95_01 and greater.
View
@@ -345,6 +345,8 @@ sub runtests {
$Builder->skip( $reason ) unless $reason eq "1";
} else {
$t = $t->new unless ref($t);
+ my @test_methods = _get_methods($t, TEST);
+ next TEST_OBJECT unless @test_methods;
foreach my $method (_get_methods($t, STARTUP)) {
_show_header($t, @tests) unless _has_no_tests($t, $method);
my $method_passed = _run_method($t, $method, \@tests);
@@ -354,7 +356,7 @@ sub runtests {
my $class = ref($t);
my @setup = _get_methods($t, SETUP);
my @teardown = _get_methods($t, TEARDOWN);
- foreach my $test (_get_methods($t, TEST)) {
+ foreach my $test (@test_methods) {
local $Current_method = $test;
$Builder->diag("\n$class->$test") if $ENV{TEST_VERBOSE};
foreach my $method (@setup, $test, @teardown) {
@@ -600,7 +602,7 @@ to import the test functions into your test class.
=head1 METHOD TYPES
-There are three different types of method you can define using Test::Class.
+There are two different types of method you can define using Test::Class.
=head2 1) Test methods
@@ -638,7 +640,24 @@ or use the :Tests attribute, which acts just like C<:Test> but defaults to C<no_
};
-=head2 2) Setup and teardown methods
+=head2 2) Test control methods
+
+These methods are used to control how the overall test class runs. They run
+in different phases, sort of like this:
+
+ load test class
+ skip if no test methods
+ "startup" methods run
+
+ foreach test method in test methods
+ "setup" methods run
+ "test" method runs
+ "teardown" methods run
+ end foreach
+
+ "teardown" methods run
+
+=head3 Setup and teardown methods
Setup and teardown methods are run before and after every test. For example:
@@ -672,7 +691,7 @@ You can also declare setup and teardown methods as running tests. For example yo
};
-=head2 3) Startup and shutdown methods
+=head3 Startup and shutdown methods
Startup and shutdown methods are like setup and teardown methods for the whole test class. All the startup methods are run once when you start running a test class. All the shutdown methods are run once just before a test class stops running.
View
@@ -0,0 +1,50 @@
+#! /usr/bin/perl
+
+use strict;
+use warnings;
+
+{
+
+ package Foo;
+ use base qw(Test::Class);
+ use Test::More;
+
+ sub startup : Tests(startup) {
+ my $self = shift;
+ fail("No startup classes without test methods");
+ }
+ sub shutdown : Tests(shutdown) {
+ my $self = shift;
+ fail("No shutdown classes without test methods");
+ }
+}
+
+{
+
+ package Bar;
+ use base qw(Foo);
+ use Test::More;
+
+ # deliberately overridding parent versions
+ sub startup : Tests(startup) {}
+ sub shutdown : Tests(shutdown) {}
+
+ sub passN {
+ my ( $self, $n ) = @_;
+ my $m = $self->current_method;
+ pass("$m just passing $_") foreach ( 1 .. $n );
+ }
+
+ sub two_tests : Tests {
+ $_[0]->passN(2);
+ }
+
+ sub some_test : Tests {
+ $_[0]->passN(2);
+ }
+}
+
+package main;
+use Test::More tests => 4;
+
+Test::Class->runtests;
View
@@ -9,7 +9,7 @@ use Test::More;
use base qw(Test::Class);
sub test_filtered_startup : Test( startup => 1 ) {
- pass( "startup test is run, even though all tests match filter" );
+ fail( "startup test not run, as no normal tests are unfiltered" );
}
sub test_filtered_setup : Test( setup => 1 ) {
@@ -21,7 +21,7 @@ sub test_filtered_teardown : Test( teardown => 1 ) {
}
sub test_filtered_shutdown : Test( shutdown => 1 ) {
- pass( "shutdown test is run, even though all tests match filter" );
+ fail( "shutdown test not run, as no normal tests are unfiltered" );
}
sub test_filtered : Test( 1 ) {
@@ -30,6 +30,6 @@ sub test_filtered : Test( 1 ) {
package main;
+use Test::More tests => 1;
Test::Class->add_filter( sub { 0 } );
-
-Test::Class->runtests;
+ok +Test::Class->runtests, 'Everything behaved as expected';
@@ -2,13 +2,15 @@
use strict;
use warnings FATAL => 'all';
-use Test::More tests => 1;
+use Test::More tests => 2;
my $shutdown_has_run;
{ package My::Test;
use base qw( Test::Class );
use Test::More;
+
+ sub foo : Test { pass("One test method must exist for shutdown to run") }
sub shutdown :Test( shutdown ) {
$shutdown_has_run = 1;

0 comments on commit e98d2e0

Please sign in to comment.