NAME

Test::Unit::TestCase - unit testing framework base class

SYNOPSIS

package FooBar;
use base qw(Test::Unit::TestCase);

sub new {
    my $self = shift()->SUPER::new(@_);
    # your state for fixture here
    return $self;
}

sub set_up {
    # provide fixture
}
sub tear_down {
    # clean up after test
}
sub test_foo {
    # test the foo feature
}
sub test_bar {
    # test the bar feature
}

DESCRIPTION

(Taken from the JUnit TestCase class documentation)

A test case defines the "fixture" (resources need for testing) to run multiple tests. To define a test case:

  1. implement a subclass of TestCase

  2. define instance variables that store the state of the fixture

  3. initialize the fixture state by overriding set_up()

  4. clean-up after a test by overriding tear_down().

Each test runs in its own fixture so there can be no side effects among test runs. Here is an example:

      package MathTest;
      use base qw(Test::Unit::TestCase);

      sub new {
	  my $self = shift()->SUPER::new(@_);
	  $self->{value_1} = 0;
	  $self->{value_2} = 0;
	  return $self;
      }

      sub set_up {
	  my $self = shift;
	  $self->{value_1} = 2;
	  $self->{value_2} = 3;
      }

For each test implement a method which interacts with the fixture. Verify the expected results with assertions specified by calling $self->assert() with a boolean value.

      sub test_add {
	  my $self = shift;
	  my $result = $self->{value_1} + $self->{value_2};
	  $self->assert($result == 5);
      }

Once the methods are defined you can run them. The normal way to do this uses reflection to implement run_test. It dynamically finds and invokes a method. For this the name of the test case has to correspond to the test method to be run. The tests to be run can be collected into a TestSuite. The framework provides different test runners, which can run a test suite and collect the results. A test runner either expects a method suite() as the entry point to get a test to run or it will extract the suite automatically.

If you do not like the rather verbose backtrace that appears when a test fails, you can use the quell_backtrace() method. You will get any message provided, but not the backtrace.

AUTHOR

Framework JUnit authored by Kent Beck and Erich Gamma.

Ported from Java to Perl by Christian Lemburg.

Copyright (c) 2000 Christian Lemburg, <lemburg@acm.org>.

All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

Thanks go to the other PerlUnit framework people: Brian Ewins, Cayte Lindner, J.E. Fritz, Zhon Johansen.

Thanks for patches go to: Matthew Astley.

SEE ALSO