Test::Unit::Lite(3pm)

πŸ“¦
version 0.1202 Β· 3 versions
, in opensuse-leap 16.0

🌸 Test::Unit::Lite(3pm) β€” NAME 🌸

okay so bestie, Test::Unit::Lite is literally Unit testing without external dependencies πŸ’…βœ¨


✨ SYNOPSIS ✨

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

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

Running all test units:

perl -MTest::Unit::Lite -e all_tests

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 gives you a lighter version of the Test::Unit framework. It implements some of the Test::Unit classes and methods needed to run test units. Test::Unit::Lite tries to be compatible with the public API of Test::Unit. It doesn't implement all classes and methods at 100%, and only those necessary to run tests are available.

Test::Unit::Lite can be distributed as a part of a package distribution, so the package can be distributed without dependency on modules outside the standard Perl distribution. 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 into 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 the inc directory. Creates missing subdirectories if needed. Silently overwrites a previous module if one was existing.
  • all_tests β€” Creates a new test runner for the Test::Unit::Lite::AllTests suite, which searches for test units in the t/tlib directory.

πŸ‘‘ CLASSES πŸ‘‘

🧚 Test::Unit::TestCase

This is a base class for a 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 in the default error message when the assertion is false.

  • new β€” The default constructor which just blesses an empty anonymous hash reference.
  • set_up β€” This method is called at the start of each test unit processing. It is an empty method and can be overridden in a derived class.
  • tear_down β€” This method is called at the end of each test unit processing. It is an empty method and can be overridden in a derived class.
  • list_tests β€” Returns the list of test methods in this class and base classes.
  • fail([MESSAGE]) β€” Immediate fail the test.
  • assert(ARG [, MESSAGE]) β€” Checks if the ARG expression returns a 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 the == operator; otherwise, the string eq operator is used.
  • assert_num_equals(ARG1, ARG2 [, MESSAGE]) / assert_num_not_equals(ARG1, ARG2 [, MESSAGE]) β€” Force numeric comparison.
  • assert_str_equals(ARG1, ARG2 [, MESSAGE]) / assert_str_not_equals(ARG1, ARG2 [, MESSAGE]) β€” Force string comparison.
  • assert(qr/PATTERN/, ARG [, MESSAGE]), assert_matches(qr/PATTERN/, ARG [, MESSAGE]), assert_does_not_match(qr/PATTERN/, ARG [, MESSAGE]) β€” Checks if ARG matches the 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 a 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 a 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 a 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 outputs the results as a TAP report.

πŸ¦‹ Test::Unit::TestRunner

This is the test runner which outputs a text report about a finished test suite.

  • new([$fh_out [, $fh_err]]) β€” The constructor for the whole test framework. Its optional parameters are filehandles for standard output and error messages.
  • fh_out β€” Contains the filehandle for standard output.
  • fh_err β€” Contains the filehandle for error messages.
  • suite β€” Contains the test suite object.
  • print_header β€” Called before running the test suite.
  • print_error β€” Called after an error was occurred on the set_up or tear_down method.
  • print_failure β€” Called after a test unit is failed.
  • print_pass β€” Called after a test unit is passed.
  • print_footer β€” Called after running the test suite.
  • start(TEST_SUITE) β€” Starts the test suite.

πŸ“ Test::Unit::Result

This object contains the results of a test suite.

  • new β€” Creates a new object.
  • messages β€” Contains the array of result messages. The single message is a hash which contains:
    • test β€” the test unit name,
    • type β€” the type of message (PASS, ERROR, FAILURE),
    • message β€” the text of message.
  • errors β€” Contains the number of collected errors.
  • failures β€” Contains the number of collected failures.
  • passes β€” Contains the number of collected passes.
  • add_error(TEST, MESSAGE) β€” Adds an error to the report.
  • add_failure(TEST, MESSAGE) β€” Adds a failure to the report.
  • add_pass(TEST, MESSAGE) β€” Adds a pass to the report.

πŸ’œ Test::Unit::HarnessUnit

This is the test runner which outputs in the same format that Test::Harness expects (Test Anything Protocol). It is derived from Test::Unit::TestRunner.

πŸŽ€ Test::Unit::Debug

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

⭐ Test::Unit::Lite::AllTests

The test suite which searches for test units in the t/tlib directory.


πŸ”₯ COMPATIBILITY πŸ”₯

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

  • The test methods are sorted alphabetically.
  • It implements a new assertion method: assert_deep_not_equals.
  • Does not support ok, assert(CODEREF, @ARGS) and multi_assert.

Test::Unit::Lite is compatible with Test::Assert assertion functions.


🌟 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/all_tests.t

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

#!/usr/bin/perl

use strict;
use warnings;

use File::Spec;
use Cwd;

BEGIN {
    unshift @INC, map { /(.*)/; $1 } split(/:/, $ENV{PERL5LIB}) if defined $ENV{PERL5LIB} and ${^TAINT};

    my $cwd = ${^TAINT} ? do { local $_=getcwd; /(.*)/; $1 } : '.';
    unshift @INC, File::Spec->catdir($cwd, 'inc');
    unshift @INC, File::Spec->catdir($cwd, 'lib');
}

use Test::Unit::Lite;

local $SIG{__WARN__} = sub { require Carp; Carp::confess("Warning: $_[0]") };

Test::Unit::HarnessUnit->new->start('Test::Unit::Lite::AllTests');

πŸ¦‹ t/test.pl

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

#!/usr/bin/perl

use strict;
use warnings;

use File::Basename;
use File::Spec;
use Cwd;

BEGIN {
    chdir dirname(__FILE__) or die "$!";
    chdir '..' or die "$!";

    unshift @INC, map { /(.*)/; $1 } split(/:/, $ENV{PERL5LIB}) if defined $ENV{PERL5LIB} and ${^TAINT};

    my $cwd = ${^TAINT} ? do { local $_=getcwd; /(.*)/; $1 } : '.';
    unshift @INC, File::Spec->catdir($cwd, 'inc');
    unshift @INC, File::Spec->catdir($cwd, 'lib');
}

use Test::Unit::Lite;

local $SIG{__WARN__} = sub { require Carp; Carp::confess("Warning: $_[0]") };

all_tests;

This is the perl equivalent of the shell command line:

perl -Iinc -Ilib -MTest::Unit::Lite -w -e all_tests

🌈 SEE ALSO 🌈

Test::Unit, Test::Assert.


πŸ§ͺ TESTS πŸ§ͺ

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


πŸ’€ BUGS πŸ’€

If you find the bug or want to implement new features, please report it at https://github.com/dex4er/perl-Test-Unit-Lite/issues

The code repository is available at http://github.com/dex4er/perl-Test-Unit-Lite


πŸ‘‘ AUTHOR πŸ‘‘

Piotr Roszatycki dexter@cpan.org


πŸŽ€ LICENSE πŸŽ€

Copyright (c) 2007-2009, 2012 by Piotr Roszatycki <dexter@cpan.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


and that’s the whole vibe, bestie β€” perl v5.42.0 β€’ 2013-06-04 β€’ Test::Unit::Lite(3pm) πŸ’–πŸ«Ά

πŸ“¦ where this page comes from