Author image Oleksii Kysil
and 1 contributors

NAME

Test::Class::Simple - Simplify your unit tests writing based on Test::Class

VERSION

version 0.06

SYNOPSIS

  package My::Example;

  sub new {
    my $class = shift;

    $class = ref($class) || $class;
    my $self = { _counter => 0 };
    bless $self, $class;
    return $self;
  }

  sub increase_counter {
    my $self = shift;

    $self->{_counter}++;
    return $self->{_counter};
  }


  package My::Example::Test;
  use parent qw(Test::Class::Simple);

  # setup methods are run before every test method.
  sub _setup {
    my $self = shift;

    # get mocked object of the class that is Test::MockObject::Extends
    my $instance = $self->get_instance();
    $instance->{_counter} = 100;
    return;
  }

  # Set which class should be mocked
  sub get_module_name {
    return 'My::Example';
  }

  # Indicate that instance should be created
  sub create_instance {
    return 1;
  }

  # a test method that runs 2 test cases
  sub test_counter : Test(2) {
    my $self = shift;

    my $test_cases = [
      {
        method => 'increase_counter',
        params => [],
        exp    => 101,
        name   => 'Increase counter once',
      },
      {
        method => 'increase_counter',
        params => [],
        exp    => 102,
        name   => 'Increase counter twice',
      },
    ];
    $self->run_test_cases($test_cases);
    return;
  }

later in a nearby .t file

  #!/usr/bin/perl
  use My::Example::Test;

  # run all the test methods in My::Example::Test
  My::Example::Test->new()->runtests();
  exit 0;

DESCRIPTION

This is an extension of Test::Class module to implement unit tests in more simple and declarative way.

Methods

pre_setup()

Can be overridden. Method that is executed before every test method and is useful for some initializations required for the tests. Is executed before creating mocked object;

post_setup()

Can be overridden. Method that is executed before every test method and is useful for some initializations required for the tests. Is executed after creating mocked object;

get_instance()

Returns mocked object of the class specified in get_module_name(). If create_instance() is set to false, returns undef value.

create_instance()

Can be overridden and must return boolean value. Indicates whether mocked instance should be created.

get_module_name()

Must be overridden and should return name of the module for which tests should be run.

run_on_module($set_value)

Sets boolean value that indicates that tests should run against the module rather then the instance of the class.

run_test_cases($cases)

Accepts arrayref of the test cases described with options inside hash references and executes them one by one.

Options

method

Name of the method that should be executed.

params

Array reference of the parameters that should be passed to the method

exp

Can be either data structure or a code reference. For data structure cmp_deeply will be executed. If code reference is set then result will be passed as a single parameter and will be expected to return true value if test case was considered as successful.

name

Name of the test case. Usually shown in the output of test run.

pre_test_hook

Code reference that will be executed before current test case. E.g. for mocking data for next test case.

post_test_hook

Code reference that will be executed after current test case. E.g. for unmocking data.

AUTHOR

Oleksii Kysil

COPYRIGHT & LICENSE

Copyright 2020 Oleksii Kysil, all rights reserved.

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