File Coverage

blib/lib/Test/Able/Runner.pm
Criterion Covered Total %
statement 26 26 100.0
branch 4 6 66.6
condition n/a
subroutine 7 7 100.0
pod 3 3 100.0
total 40 42 95.2


line stmt bran cond sub pod time code
1             package Test::Able::Runner;
2             {
3             $Test::Able::Runner::VERSION = '1.002';
4             }
5 2     2   1932 use Moose;
  2         800160  
  2         45  
6 2     2   14518 use Test::Able ();
  2         125221  
  2         53  
7 2     2   60 use Moose::Exporter;
  2         10  
  2         10  
8 2     2   139 use Moose::Util::MetaRole;
  2         3  
  2         582  
9              
10             =head1 NAME
11              
12             Test::Able::Runner - use Test::Able without a bunch of boilerplate
13              
14             =head1 VERSION
15              
16             version 1.002
17              
18             =head1 SYNOPSIS
19              
20             use Test::Able::Runner;
21              
22             use_test_packages
23             -base_package => 'My::Project::Test';
24              
25             run;
26              
27             =head1 DESCRIPTION
28              
29             I like L<Test::Able>. I really don't like having to copy my boilerplate test runner and modify it when I use it in a new project. This provides a basic test runner for your testable tests that takes care of the basics for you. You can extend it a bit to customize things if you like as well. Let me know if you want this to do something else.
30              
31             This mostly assumes that you want to run several tests as a group within a single Perl interpreter. If this is not what you want, then you probably don't want this module.
32              
33             =cut
34              
35             Moose::Exporter->setup_import_methods(
36             with_meta => [ 'run', 'use_test_packages' ],
37             also => 'Test::Able',
38             );
39              
40             =head1 METHODS
41              
42             =head2 use_test_packages
43              
44             The first thing your test runner needs to do is call this method to tell it what packages need to be included in your test.
45              
46             =head2 COMMON CASES
47              
48             Before describing the options, here are some examples of how to use this subroutine.
49              
50             =head3 EXAMPLE 1
51              
52             use_test_packages
53             -base_package => 'My::Project::Test',
54             -test_path => 't/lib';
55              
56             This is pretty much the simplest case. This will load and run all the packages starting with the name "My::Project::Test" found in the project's F<t/lib> directory. I show the C<< -test_path >> option here, but in this case it's redundant. Your test path is assumed to be F<t/lib> in the usual case.
57              
58             =head3 EXAMPLE 2
59              
60             use_test_packages
61             -test_packages => [ qw(
62             My::Project::Test::One
63             My::Project::Test::Two
64             My::Project::Test::Three
65             ) ];
66              
67             Rather than searching for any test packages you might have in your test folder, you might prefer to explicitly list them.
68              
69             =head3 OPTIONS
70              
71             =over
72              
73             =item C<< -base_package >>
74              
75             This is the package namespace to search for classes within. Any class found under this namespace (within any directory included in C<< -test_path >>) will be run in your tests. If you want to include classes under this base package namespace that are not tests (test roles or base classes or whatever), you may place a global package variable within the package named C<< $NOT_A_TEST >> and set it to a true value:
76              
77             package My::Project::Test::Base;
78             use Test::Able;
79              
80             our $NOT_A_TEST = 1;
81              
82             You may use this option or the C<< -test_packages >> option. This may be a single scalar package name
83              
84             =item C<< -test_packages >>
85              
86             This is the list of test packages to load and run. It is always given as an array of package names.
87              
88             =item C<< -test_path >>
89              
90             This is the search path for test classes. This lists additional paths that should be added to C<< @INC >> to search for tests before loading the tests. These paths are added to the front of C<< @INC >>.
91              
92             It can be given as a single scalar or as an array of paths:
93              
94             use_test_packages
95             -base_package => 'My::Project::Test',
96             -test_path => [ 't/online', 't/offline' ];
97              
98             =back
99              
100             =cut
101              
102             sub use_test_packages {
103 2     2 1 56 my $meta = shift;
104 2         7 my %options = @_;
105              
106 2 50       97 $meta->base_package( $options{-base_package} )
107             if $options{-base_package};
108 2 50       8 $meta->test_packages( $options{-test_packages} )
109             if $options{-test_packages};
110 2 100       43 $meta->test_path( $options{-test_path} )
111             if $options{-test_path};
112             }
113              
114             =head2 init_meta
115              
116             Sets up your test runner package.
117              
118             =cut
119              
120             sub init_meta {
121 2     2 1 174 my $class = shift;
122 2         6 my %options = @_;
123              
124 2         7 my $meta = Test::Able->init_meta(@_);
125              
126 2         357884 Moose::Util::MetaRole::apply_metaroles(
127             for => $options{for_class},
128             class_metaroles => {
129             class => [ 'Test::Able::Runner::Role::Meta::Class' ],
130             },
131             );
132              
133 2         6451 return $options{for_class}->meta;
134             }
135              
136             =head2 run
137              
138             This invokes the test runner for all the tests you've requested.
139              
140             =cut
141              
142             sub run {
143 2     2 1 1997 my $meta = shift;
144              
145 2         42 my $runner = $meta->name->new;
146 2         10736 $runner->meta->setup_test_objects;
147 2         33 $runner->meta->run_tests;
148             }
149              
150             =head1 COOKBOOK
151              
152             Here are some other things you might like to try.
153              
154             =head2 Test Runner Tests
155              
156             The test runner itself may have tests if you want. The test runner classes uses the usual L<Test::Able> bits, so this works. Similarly, you can do setup, teardown, and all the rest in your runner.
157              
158             use Test::Able::Runner;
159              
160             use_test_packages
161             -base_package => 'Foo::Test';
162              
163             test plan => 1, test_something => sub {
164             ok(1);
165             };
166              
167             run;
168              
169             =head1 AUTHOR
170              
171             Andrew Sterling Hanenkamp C<< <hanenkamp@cpan.org> >>
172              
173             =head1 COPYRIGHT AND LICENSE
174              
175             Copyright 2010 Qubling Software LLC.
176              
177             This library is free software. You can redistribute it and/or modify
178             it under the same terms as Perl itself.
179              
180             =cut
181              
182             1;