blib/lib/HTTP/WebTest/Plugin/XMLReport.pm

filter: Statementstmt 98.7% 79 / 80 Branchbran 75.0% 15 / 20 Conditioncond 57.1% 4 / 7 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 100% 13 / 13 Podpod 75.0% 3 / 4 totaltotal 89.8% 115 / 128 CC 16 SCAR 15.6
CC
16
Cov
92%
report_test
8.1
_init_xml_writer
7.2
_cleanup
3.0
CRAP 4.8
↑ Summary
1 package HTTP::WebTest::Plugin::XMLReport;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use base qw(HTTP::WebTest::ReportPlugin);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use XML::Writer;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use POSIX qw(strftime);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 $VERSION = '1.01';
9  
10 =head1 NAME
11  
12 HTTP::WebTest::Plugin::XMLReport - Report plugin for HTTP::WebTest, generates output in XML format
13  
14 =head2 VERSION
15  
16 version 1.00 - $Revision: 1.4 $
17  
18 Compatible with L<HTTP::WebTest|HTTP::WebTest> version 2.x API
19  
20 =head1 SYNOPSIS
21  
22 See L<HTTP::WebTest|HTTP::WebTest>, the section about plugins.
23  
24 =head1 DESCRIPTION
25  
26 Generate a WebTest report in XML format.
27 The document element is the 'testresults' element.
28 For each test definition, a 'group' element is included,
29 containig individual test results as children of 'test'
30 elements.
31  
32 Example:
33  
34 <?xml version="1.0" encoding="UTF-8"?>
35 <testresults date="Mon Sep 16 17:00:03 2002">
36 <group name="Homepage" url="http://www.mysite.com">
37 <test name="Status code check">
38 <result status="PASS">200 OK</result>
39 </test>
40 <test name="Forbidden text">
41 <result status="PASS">Premature end of script headers</result>
42 </test>
43 <test name="Required text">
44 <result status="PASS">Please login:</result>
45 <result status="PASS">&lt;/html&gt;</result>
46 </test>
47 <test name="Content size check">
48 <result status="PASS">Number of returned bytes ( 30381 ) is &gt; or = 10000 ?</result>
49 <result status="PASS">Number of returned bytes ( 30381 ) is &lt; or = 99000 ?</result>
50 </test>
51 <test name="Response time check">
52 <result status="PASS">Response time ( 9.14 ) is &gt; or = 0.01 ?</result>
53 <result status="FAIL">Response time ( 9.14 ) is &lt; or = 8.00 ?</result>
54 </test>
55 </group>
56 [...]
57 </testresults>
58  
59  
60 =head1 GLOBAL PARAMETERS
61  
62 =head2 xml_report_dtd
63  
64 This global parameter specifies whether the testresults document
65 includes the DTD.
66  
67 For production work this will normally not be advised, but
68 it can be helpful when validating the report during development,
69 with a command like:
70  
71 xmllint --valid saved_xml_report.xml
72  
73 (xmllint is a utility program that comes bundled with the Gnome
74 XML libraries, aka LibXML).
75  
76 =head3 Allowed values
77  
78 C<yes>, C<no>
79  
80 =head3 Default value
81  
82 C<no>
83  
84 =head2 default_report
85  
86 You may want to suppress the default report.
87 From C<wtscript>:
88  
89 default_report no
90  
91 Or as optional global parameter from Perl:
92  
93 $wt = new HTTP::WebTest($defs, { default_report => 'no' });
94  
95 =head3 Allowed values
96  
97 C<yes>, C<no>
98  
99 =head3 Default value
100  
101 C<yes>
102  
103  
104 =head1 TEST PARAMETERS
105  
106 Note: these are set from the test definitions, see L<HTTP::WebTest|HTTP::WebTest>
107 for details.
108  
109 =head2 test_name
110  
111 The C<test_name> attribute from C<wtscript> will be used for the
112 C<name> attribute of the C<group> element in the XML output document.
113  
114 This attribute is the easiest way to identify each test group, so
115 it is adviced that you define one unique name per group in the
116 test definitions.
117  
118 =head1 REPORT DTD
119  
120 The full Document Type Definition for the output XML:
121  
122 <?xml version="1.0" ?>
123 <!ELEMENT testresults (group+)>
124 <!ATTLIST testresults
125 date CDATA #REQUIRED >
126
127 <!ELEMENT group (test+)>
128 <!ATTLIST group
129 name CDATA #REQUIRED
130 url CDATA #REQUIRED >
131
132 <!ELEMENT test (result+)>
133 <!ATTLIST test
134 name CDATA #REQUIRED >
135
136 <!ELEMENT result (#PCDATA)>
137 <!ATTLIST result
138 status (PASS | FAIL) #REQUIRED >
139  
140 =cut
141  
142 12 ▶sub param_types {
Subroutine param_types: called CC=1 SCAR=0.0
Pod documented
143 12 return shift->SUPER::param_types . "\n" .
144 q(default_report yesno
145 xml_report_dtd yesno
146 test_name scalar
147 );
148 }
149  
150 4 ▶sub _init_xml_writer {
Subroutine _init_xml_writer: called CC=7 SCAR=19.7
151 4 my $self = shift;
152  
153 4 $self->global_validate_params(qw(fh_out xml_report_dtd output_ref));
154 4 my $fh_out = $self->global_test_param('fh_out');
155 4 ▶ if ($self->global_test_param('output_ref')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->global_test_param("output_ref")) 4 0 4
156 4 eval { local $SIG{'__DIE__'}; require IO::Scalar; };
157 4 ▶ warn $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 4 4
158 4 $fh_out = new IO::Scalar($self->global_test_param('output_ref'));
159 }
160 4 my $text = join '', @_;
161  
162 4 ${$self->test_output} .= $text;
163 4 my $dtd = $self->global_test_param('xml_report_dtd');
164 4 ▶ if ($dtd && ($dtd eq 'yes')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dtd and $dtd eq 'yes') { } 2 2 4
Condition66%2/3
$dtd and $dtd eq "yes"
!ll&&!rl&&r
202
MC/DC50%1/2
$dtd and $dtd eq "yes"
$dtd $dtd eq "yes"
Truth table66%2/3
A and B
A= $dtdB= $dtd eq "yes"
ABresult
0X0
100
111
165 2 $dtd = sprintf("<!DOCTYPE testresults [\n%s]>\n", &_DTD());
166 } else {
167 2 $dtd = undef;
168 }
169  
170 4 ▶ if (defined $fh_out) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $fh_out) { } 4 0 4
171 4 $self->{x_out} = new XML::Writer(OUTPUT => $fh_out, DATA_INDENT => 2);
172 } else {
173 0 $self->{x_out} = new XML::Writer(DATA_INDENT => 2);
174 }
175 4 $self->{x_out}->xmlDecl();
176 4 ▶ $self->{x_out}->getOutput()->print($dtd) if $dtd;
Branch: 1 branch
BranchTrueFalseTotal
if $dtd 2 2 4
177 }
178  
179 4 ▶sub start_tests {
Subroutine start_tests: called CC=1 SCAR=0.0
Pod documented
180 4 my $self = shift;
181  
182 4 $self->_init_xml_writer;
183 # use RFC822-conformant date string including Time Zone
184 4 $self->{x_out}->startTag('testresults',
185 'date' => strftime("%a, %d %b %Y %H:%M:%S %z", localtime()));
186 4 $self->{x_out}->characters("\n");
187 4 $self->SUPER::start_tests;
188 }
189  
190 4 ▶sub report_test {
Subroutine report_test: called CC=8 SCAR=20.9
Pod undocumented
191 4 my $self = shift;
192  
193 4 $self->validate_params(qw(test_name));
194  
195 # get test params we handle
196 4 my $test_name = $self->test_param('test_name', 'untitled');
197 4 my $url = '';
198 4 ▶ if($self->webtest->current_request) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->webtest->current_request) 4 0 4
199 4 $url = $self->webtest->current_request->uri;
200 }
201  
202 # test header
203 4 $self->{x_out}->startTag('group', 'name' => $test_name, 'url' => $url);
204 4 $self->{x_out}->characters("\n");
205 4 my $out = '';
206  
207 4 for my $result (@{$self->webtest->current_results}) {
208 # test results
209 11 ▶ my $group_comment = $$result[0] || '';
Condition50%1/2
$result->[0] || ''
l!l
110
MC/DC0%0/1
$result->[0] || ''
$result->[0]
Truth table50%1/2
A
A= $result->[0]
Aresult
11
00
210 11 my @results = @$result[1 .. @$result - 1];
211  
212 11 ▶ next unless @results;
Branch: 1 branch
BranchTrueFalseTotal
unless @results 0 11 11
213 11 $self->{x_out}->startTag('test', 'name' => $group_comment);
214 11 $self->{x_out}->characters("\n");
215  
216 11 for my $subresult (@$result[1 .. @$result - 1]) {
217 15 ▶ my $comment = $subresult->comment || '';
Condition50%1/2
$subresult->comment || ''
l!l
150
MC/DC0%0/1
$subresult->comment || ''
$subresult->comment
Truth table50%1/2
A
A= $subresult->comment
Aresult
11
00
218 15 ▶ my $ok = $subresult->ok ? 'PASS' : 'FAIL';
Branch: 1 branch
BranchTrueFalseTotal
$subresult->ok ? : 14 1 15
219  
220 15 $self->{x_out}->dataElement('result', $comment, 'status' => $ok);
221 15 $self->{x_out}->characters("\n");
222 }
223 11 $self->{x_out}->endTag('test');
224 11 $self->{x_out}->characters("\n");
225 }
226 4 $self->{x_out}->endTag('group');
227 4 $self->{x_out}->characters("\n");
228 }
229  
230 4 ▶sub end_tests {
Subroutine end_tests: called CC=1 SCAR=0.0
Pod documented
231 4 my $self = shift;
232  
233 4 $self->SUPER::end_tests;
234 4 $self->_cleanup();
235 }
236  
237 7 ▶sub _cleanup {
Subroutine _cleanup: called CC=3 SCAR=11.0
238 7 my $self = shift;
239 7 ▶ if ($self->{x_out}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"x_out"}) 4 3 7
240 4 ▶ while (my $elt = $self->{x_out}->current_element()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $elt = $self->{"x_out"}->current_element) 4 4 8
241 4 $self->{x_out}->endTag($elt);
242 }
243 4 $self->{x_out}->end();
244 4 delete $self->{x_out};
245 }
246 }
247  
248 3 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
249 3 $_[0]->_cleanup();
250 }
251  
252 2 ▶sub _DTD {
Subroutine _DTD: called CC=1 SCAR=0.0
253 2 return <<"DTD";
254 <!ELEMENT testresults (group+)>
255 <!ATTLIST testresults
256 date CDATA #REQUIRED >
257  
258 <!ELEMENT group (test+)>
259 <!ATTLIST group
260 name CDATA #REQUIRED
261 url CDATA #REQUIRED >
262  
263 <!ELEMENT test (result+)>
264 <!ATTLIST test
265 name CDATA #REQUIRED >
266  
267 <!ELEMENT result (#PCDATA)>
268 <!ATTLIST result
269 status (PASS | FAIL) #REQUIRED >
270 DTD
271 }
272  
273 =head1 BUGS
274  
275 If the test run dies, the resulting XML report is not preperly
276 terminated, resulting in malformed format which is not parsable
277 by conforming XML processors.
278  
279 This can happen for example when a document could not be fetched
280 and when subsequently a test with the Click plugin is used.
281  
282 The following limitations apply:
283  
284 These options from the DefaultReport are missing:
285  
286 show_headers
287 show_html
288 show_cookies
289  
290 Sending email from this module is not implemented; a work around
291 would be to parse the resulting XML document and generate an
292 email message whenever a test result has status C<FAIL>.
293  
294  
295 =head1 COPYRIGHT
296  
297 Copyright (c) 2002 Johannes la Poutre. All rights reserved.
298  
299 This program is free software; you can redistribute it and/or modify
300 it under the same terms as Perl itself.
301  
302 =head1 SEE ALSO
303  
304 L<HTTP::WebTest>
305  
306 L<HTTP::WebTest::API>
307  
308 L<HTTP::WebTest::Plugin>
309  
310 L<HTTP::WebTest::Plugins>
311  
312 L<XML::Writer>
313  
314 =cut
315  
316 1 1;
317  
↑ Summary