Test::Class::Simple - Simplify your unit tests writing based on Test::Class
version 0.06
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;
This is an extension of Test::Class module to implement unit tests in more simple and declarative way.
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;
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;
Returns mocked object of the class specified in get_module_name(). If create_instance() is set to false, returns undef value.
undef
Can be overridden and must return boolean value. Indicates whether mocked instance should be created.
Must be overridden and should return name of the module for which tests should be run.
Sets boolean value that indicates that tests should run against the module rather then the instance of the class.
Accepts arrayref of the test cases described with options inside hash references and executes them one by one.
Name of the method that should be executed.
Array reference of the parameters that should be passed to the method
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 of the test case. Usually shown in the output of test run.
Code reference that will be executed before current test case. E.g. for mocking data for next test case.
Code reference that will be executed after current test case. E.g. for unmocking data.
Oleksii Kysil
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.
To install Test::Class::Simple, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Test::Class::Simple
CPAN shell
perl -MCPAN -e shell install Test::Class::Simple
For more information on module installation, please visit the detailed CPAN module installation guide.