The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Test::Unit::Lite - Unit testing without external dependencies

SYNOPSIS

Bundling the Test::Unit::Lite as a part of package distribution:

  perl -MTest::Unit::Lite -e bundle

Using as a replacement for Test::Unit:

  package FooBarTest;
  use Test::Unit::Lite;   # unnecessary if module isn't directly used
  use base '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 {
      my $self = shift;
      my $obj = ClassUnderTest->new(...);
      $self->assert_not_null($obj);
      $self->assert_equals('expected result', $obj->foo);
      $self->assert(qr/pattern/, $obj->foobar);
  }
  sub test_bar {
      # test the bar feature
  }

DESCRIPTION

This framework provides lighter version of Test::Unit framework. It implements some of the Test::Unit classes and methods needed to run test units. The Test::Unit::Lite tries to be compatible with public API of Test::Unit. It doesn't implement all classes and methods at 100% and only those necessary to run tests are available.

The Test::Unit::Lite can be distributed as a part of package distribution, so the package can be distributed without dependency on modules outside standard Perl distribution. The Test::Unit::Lite is provided as a single file.

Bundling the Test::Unit::Lite as a part of package distribution

The Test::Unit::Lite framework can be bundled to the package distribution. Then the Test::Unit::Lite module is copied to the inc directory of the source directory for the package distribution.

FUNCTIONS

bundle

Copies Test::Unit::Lite modules into inc directory. Creates missing subdirectories if needed. Silently overwrites previous module if was existing.

CLASSES

Test::Unit::TestCase

This is a base class for single unit test module. The user's unit test module can override the default methods that are simple stubs.

The MESSAGE argument is optional and is included to the default error message when the assertion is false.

new

The default constructor which just bless an empty anonymous hash reference.

set_up

This method is called at the start of test unit processing. It is empty method and can be overrided in derived class.

tear_down

This method is called at the end of test unit processing. It is empty method and can be overrided in derived class.

list_tests

Returns the list of test methods in this class.

fail([MESSAGE])

Immediate fail the test.

assert(ARG [, MESSAGE])

Checks if ARG expression returns true value.

assert_null(ARG [, MESSAGE])
assert_not_null(ARG [, MESSAGE])

Checks if ARG is defined or not defined.

assert_equals(ARG1, ARG2 [, MESSAGE])
assert_not_equals(ARG1, ARG2 [, MESSAGE])

Checks if ARG1 and ARG2 are equals or not equals. If ARG1 and ARG2 look like numbers then they are compared with '==' operator, otherwise the string 'eq' operator is used.

assert_num_equals(ARG1, ARG2 [, MESSAGE])
assert_num_not_equals(ARG1, ARG2 [, MESSAGE])

Force numeric comparition.

assert_str_equals(ARG1, ARG2 [, MESSAGE])
assert_str_not_equals(ARG1, ARG2 [, MESSAGE])

Force string comparition.

assert(qr/PATTERN/, ARG [, MESSAGE])
assert_matches(qr/PATTERN/, ARG [, MESSAGE])
assert_does_not_match(qr/PATTERN/, ARG [, MESSAGE])

Checks if ARG matches PATTER regexp.

assert_deep_equals(ARG1, ARG2 [, MESSAGE])
assert_deep_not_equals(ARG1, ARG2 [, MESSAGE])

Check if reference ARG1 is a deep copy of reference ARG2 or not. The references can be deep structure. If they are different, the message will display the place where they start differing.

Test::Unit::TestSuite

This is a base class for test suite, which groups several test units.

empty_new([NAME])

Creates a fresh suite with no tests.

new([CLASS | TEST])

Creates a test suite from unit test name or class. If a test suite is provided as the argument, it merely returns that suite. If a test case is provided, it extracts all test case methods (see Test::Unit::TestCase->list_test) from the test case into a new test suite.

name

Contains the name of the current test suite.

units

Contains the list of test units.

add_test([TEST_CLASSNAME | TEST_OBJECT])

Adds the test object to a suite.

count_test_cases

Returns the number of test cases in this suite.

run

Runs the test suite and output the results as TAP report.

Test::Unit::TestRunner

This is the test runner which outputs in the same format that Test::Harness expects.

new

The constructor for whole test framework.

suite

Contains the test suite object.

start(TEST_SUITE)

Starts the test suite.

Test::Unit::HarnessUnit

In fact it is an empty class which inherits all methods on Test::Unit::TestRunner class.

Test::Unit::Debug

The empty class which is provided for compatibility with original Test::Unit framework.

COMPATIBILITY

Test::Unit::Lite should be compatible with public API of Test::Unit. The Test::Unit::Lite also has some known incompatibilities:

  • The test methods are sorted alphabetically.

  • It implements new assertion method: assert_deep_not_equals.

  • Does not support ok, assert(CODEREF, @ARGS) and multi_assert.

EXAMPLES

t/tlib/SuccessTest.pm

This is the simple unit test module.

  package SuccessTest;

  use strict;
  use warnings;

  use base 'Test::Unit::TestCase';

  sub test_success {
    my $self = shift;
    $self->assert(1);
  }

  1;

t/tlib/AllTests.pm

This is the test suite which calls all test cases located in t/tlib directory.

  package AllTests;
  
  use base 'Test::Unit::TestSuite';
  
  use File::Find ();
  use File::Basename ();
  use File::Spec ();
  
  sub new {
      return bless {}, shift;
  }
  
  sub suite {
      my $class = shift;
      my $suite = Test::Unit::TestSuite->empty_new("Framework Tests");
  
      my $dir = (File::Basename::dirname(__FILE__));
      my $depth = scalar File::Spec->splitdir($dir);
  
      File::Find::find({
          wanted => sub {
              my $path = File::Spec->canonpath($File::Find::name);
              return unless $path =~ s/\.pm$//;
              my @path = File::Spec->splitdir($path);
              splice @path, 0, $depth;
              return unless scalar @path > 0;
              my $class = join '::', @path;
              return unless $class;
              return if $class =~ /^Test::Unit::/;
              return if @ARGV and $class !~ $ARGV[0];
              return unless eval "use $class; $class->isa('Test::Unit::TestCase');";
              $suite->add_test($class);
          },
          no_chdir => 1,
      }, $dir || '.');
  
      return $suite;
  }
  
  1;

t/all_tests.t

This is the test script for Test::Harness called with "make test".

  #!/usr/bin/perl -w

  use strict;
  use lib 'inc', 't/tlib', 'tlib';  # inc is needed for bundled T::U::L

  use Test::Unit::Lite;  # load the Test::Unit replacement
  use Test::Unit::HarnessUnit;

  my $testrunner = Test::Unit::HarnessUnit->new();
  $testrunner->start("AllTests");

t/test.sh

This is the optional shell script for calling test suite directly.

  #!/bin/sh
  set -e
  cd `dirname $0`
  cd ..
  PERL=${PERL:-perl}
  find t/tlib -name '*.pm' -print | while read pm; do
      $PERL -Iinc -Ilib -It/tlib -MTest::Unit::Lite -c "$pm"
  done
  $PERL -w -Iinc -Ilib -It/tlib t/all_tests.t "$@"

SEE ALSO

Test::Unit, Test::Unit::TestCase, Test::Unit::TestSuite, Test::Unit::Assert, Test::Unit::TestRunner, Test::Unit::HarnessUnit.

TESTS

The Test::Unit::Lite was tested as a Test::Unit replacement for following distributions: Test::C2FIT, XAO::Base, Exception::Base.

BUGS

If you find the bug or need new feature, please report it.

AUTHOR

Piotr Roszatycki <dexter@debian.org>

LICENSE

Copyright 2007 by Piotr Roszatycki <dexter@debian.org>.

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

See http://www.perl.com/perl/misc/Artistic.html