File Coverage

blib/lib/Test/Nightly/Report.pm
Criterion Covered Total %
statement 54 62 87.1
branch 16 28 57.1
condition 3 6 50.0
subroutine 12 12 100.0
pod 2 3 66.6
total 87 111 78.3


line stmt bran cond sub pod time code
1             package Test::Nightly::Report;
2              
3 8     8   30350 use strict;
  8         18  
  8         379  
4              
5 8     8   45 use Carp;
  8         17  
  8         645  
6 8     8   9814 use Template;
  8         255807  
  8         267  
7 8     8   12779 use DateTime;
  8         1721312  
  8         366  
8              
9 8     8   599 use Test::Nightly::Email;
  8         18  
  8         103  
10 8     8   5450 use Test::Nightly::Report::Template;
  8         21  
  8         252  
11              
12 8     8   50 use base qw(Test::Nightly::Base Class::Accessor::Fast);
  8         15  
  8         6781  
13              
14             my @methods = qw(
15             email_report
16             report_template
17             report_output
18             test
19             tests
20             test_report
21             version_report
22             version_result
23             );
24              
25             __PACKAGE__->mk_accessors(@methods);
26              
27             our $VERSION = '0.03';
28              
29             =head1 NAME
30              
31             Test::Nightly::Report - Generates a test report.
32              
33             =head1 DESCRIPTION
34              
35             Generates a report based on the tests that have been run, that can then be emailed to you, or output to a file. You probably should not be dealing with this directly.
36              
37             =head1 SYNOPSIS
38              
39             use Test::Nightly::Report;
40            
41             my $nightly = Test::Nightly::Report->new({
42             email_report => {
43             to => 'kirstinbettiol@gmail.com',
44             }
45             });
46              
47             $report->run();
48              
49             The following methods are available:
50              
51             =cut
52              
53             =head2 new()
54              
55             my $report = Test::Nightly::Report->new({
56             email_report => \%email_config, # Emails the report. See Test::Nightly::Email for config.
57             report_template => '/dir/somewhere/template.txt', # Defaults to internal template.
58             report_output => '/dir/somewhere/output.txt', # File to output the report to.
59             test_report => 'all', # 'failed' || 'passed'. Defaults to all.
60             });
61              
62             Produces a report on the tests that have been run.
63              
64             Depending on what you pass in, defines what report is generated.
65             If you would like the report emailed to you, pass in C.
66             If you would like the report to be logged somewhere, then pass in C.
67              
68             Default template can be seen in L
69              
70             =cut
71              
72             sub new {
73              
74 4     4 1 32 my ($class, $conf) = @_;
75              
76 4         27 my $self = bless {}, $class;
77              
78 4         181 $self->_init($conf, \@methods);
79              
80 4         83 return $self;
81              
82             }
83              
84             =head2 run()
85              
86             $report->run({
87             ... takes the same arguments as new ...
88             });
89              
90             Generates the report.
91              
92             =cut
93              
94             sub run {
95              
96 4     4 1 20 my ($self, $conf) = @_;
97              
98 4 100       41 $self->test_report('all') unless $self->test_report();
99 4         109 $self->_debug('Running Report');
100              
101             # Return if there are no tests
102 4 100       139 return if ( !$self->tests() );
103             # Return if there are no passed tests and we are only reporting on passed tests
104 3 100 66     161 return if ( !$self->_passed_tests() && $self->test_report() eq 'passed' );
105             # Return if there are no failed tests and we are only reporting on failed tests
106 2 50 33     25 return if ( !$self->_failed_tests() && $self->test_report() eq 'failed' );
107              
108             # Work out what test data we want.
109 2         9 my %vals;
110              
111 2 50       15 if ($self->test_report() eq 'failed') {
    50          
112 0         0 $vals{'tests'} = $self->_failed_tests();
113             } elsif ($self->test_report() eq 'passed') {
114 0         0 $vals{'tests'} = $self->_passed_tests();
115             } else {
116 2         45 $vals{'tests'} = $self->tests();
117             }
118              
119             # Read in the passed in template, else use the default template
120 2         21 my $template;
121 2 50       84 if (defined $self->report_template()) {
122              
123 0 0       0 open DATA, $self->report_template() or $self->_add_error('Test::Nightly::Report::run() - Error with "report_template": ' . $self->report_output() . ': ' . $!);
124 0         0 while() {
125 0         0 $template .= $_ . "\r\n";
126             }
127              
128             } else {
129 2         20 $template = Test::Nightly::Report::Template::DEFAULT;
130             }
131              
132 2 50       8 if ($template) {
133            
134 2         111 my $tt = Template->new({ABSOLUTE => 1});
135              
136             # Process the Report
137 2         116266 my $report = '';
138 2         22 $tt->process(\$template, \%vals, \$report);
139 2 50       149601 carp $tt->error() if ($tt->error());
140              
141             # Send an email if an email address is passed in
142 2 50       43 if (defined $self->email_report()) {
143              
144 0         0 my $email = Test::Nightly::Email->new($self->email_report());
145              
146 0         0 $email->email({
147             subject => 'Results of your Tests',
148             message => $report,
149             });
150              
151             }
152 2 50       28 if (defined $self->report_output()) {
153              
154 2 50       22 open(FH,">" . $self->report_output()) || $self->_add_error('Test::Nightly::Report::run() - Error with "report_output": ' . $self->report_output() . ': ' . $!);
155 2         280 print FH $report;
156 2         144 close(FH);
157              
158             }
159              
160             }
161              
162             }
163              
164             # Gets the tests from the the test output
165              
166             sub tests {
167              
168 6     6 0 26 my $self = shift;
169              
170 6 50       65 if (defined $self->test()) {
171 6         68 return $self->test()->tests();
172             } else {
173 0         0 return;
174             }
175              
176             }
177              
178             # Gets the passed tests from the test output
179              
180             sub _passed_tests {
181              
182 3     3   12 my $self = shift;
183              
184 3         31 return $self->test()->passed_tests();
185              
186             }
187              
188             # Gets the failed tests from the test output
189              
190             sub _failed_tests {
191              
192 2     2   4 my $self = shift;
193              
194 2         11 return $self->test()->failed_tests();
195              
196             }
197              
198             =head1 List of methods:
199              
200             =over 4
201              
202             =item email_report
203              
204             If set will email the report. Takes a hash ref of \%email_config, refer to Test::Nightly::Email for the options.
205              
206             =item report_template
207              
208             Pass this in if you wish to have your own customised report template. Otherwise, uses the default template is in Test::Nightly::Report::Template
209              
210             =item report_output
211              
212             Set this to a filepath/filename and the report will be outputted here.
213              
214             =item test
215              
216             Output of the test.
217              
218             =item test_report
219              
220             This is where you specify what you wish to report on after the outcome of the test. Specifying 'passed' will only report on tests that passed, specifying 'failed' will only report on tests that failed and specifying 'all' will report on both.
221              
222             =head1 AUTHOR
223              
224             Kirstin Bettiol
225              
226             =head1 COPYRIGHT
227              
228             (c) 2005 Kirstin Bettiol
229             This library is free software, you can use it under the same terms as perl itself.
230              
231             =head1 SEE ALSO
232              
233             L,
234             L,
235             L,
236             L,
237             L,
238             L.
239              
240             =cut
241              
242             1;