1Test::Builder::Tester(3pPme)rl Programmers Reference GuiTdeest::Builder::Tester(3pm)
2
3
4
6 Test::Builder::Tester - test testsuites that have been built with
7 Test::Builder
8
10 use Test::Builder::Tester tests => 1;
11 use Test::More;
12
13 test_out("not ok 1 - foo");
14 test_fail(+1);
15 fail("foo");
16 test_test("fail works");
17
19 A module that helps you test testing modules that are built with
20 Test::Builder.
21
22 The testing system is designed to be used by performing a three step
23 process for each test you wish to test. This process starts with using
24 "test_out" and "test_err" in advance to declare what the testsuite you
25 are testing will output with Test::Builder to stdout and stderr.
26
27 You then can run the test(s) from your test suite that call
28 Test::Builder. At this point the output of Test::Builder is safely
29 captured by Test::Builder::Tester rather than being interpreted as real
30 test output.
31
32 The final stage is to call "test_test" that will simply compare what
33 you predeclared to what Test::Builder actually outputted, and report
34 the results back with a "ok" or "not ok" (with debugging) to the normal
35 output.
36
37 Methods
38
39 These are the six methods that are exported as default.
40
41 test_out
42 test_err
43 Procedures for predeclaring the output that your test suite is
44 expected to produce until "test_test" is called. These procedures
45 automatically assume that each line terminates with "\n". So
46
47 test_out("ok 1","ok 2");
48
49 is the same as
50
51 test_out("ok 1\nok 2");
52
53 which is even the same as
54
55 test_out("ok 1");
56 test_out("ok 2");
57
58 Once "test_out" or "test_err" (or "test_fail" or "test_diag") have
59 been called once all further output from Test::Builder will be cap‐
60 tured by Test::Builder::Tester. This means that your will not be
61 able perform further tests to the normal output in the normal way
62 until you call "test_test" (well, unless you manually meddle with
63 the output filehandles)
64
65 test_fail
66 Because the standard failure message that Test::Builder produces
67 whenever a test fails will be a common occurrence in your test
68 error output, and because has changed between Test::Builder ver‐
69 sions, rather than forcing you to call "test_err" with the string
70 all the time like so
71
72 test_err("# Failed test ($0 at line ".line_num(+1).")");
73
74 "test_fail" exists as a convenience method that can be called
75 instead. It takes one argument, the offset from the current line
76 that the line that causes the fail is on.
77
78 test_fail(+1);
79
80 This means that the example in the synopsis could be rewritten more
81 simply as:
82
83 test_out("not ok 1 - foo");
84 test_fail(+1);
85 fail("foo");
86 test_test("fail works");
87
88 test_diag
89 As most of the remaining expected output to the error stream will
90 be created by Test::Builder's "diag" function,
91 Test::Builder::Tester provides a convience function "test_diag"
92 that you can use instead of "test_err".
93
94 The "test_diag" function prepends comment hashes and spacing to the
95 start and newlines to the end of the expected output passed to it
96 and adds it to the list of expected error output. So, instead of
97 writing
98
99 test_err("# Couldn't open file");
100
101 you can write
102
103 test_diag("Couldn't open file");
104
105 Remember that Test::Builder's diag function will not add newlines
106 to the end of output and test_diag will. So to check
107
108 Test::Builder->new->diag("foo\n","bar\n");
109
110 You would do
111
112 test_diag("foo","bar")
113
114 without the newlines.
115
116 test_test
117 Actually performs the output check testing the tests, comparing the
118 data (with "eq") that we have captured from Test::Builder against
119 that that was declared with "test_out" and "test_err".
120
121 This takes name/value pairs that effect how the test is run.
122
123 title (synonym 'name', 'label')
124 The name of the test that will be displayed after the "ok" or
125 "not ok".
126
127 skip_out
128 Setting this to a true value will cause the test to ignore if
129 the output sent by the test to the output stream does not match
130 that declared with "test_out".
131
132 skip_err
133 Setting this to a true value will cause the test to ignore if
134 the output sent by the test to the error stream does not match
135 that declared with "test_err".
136
137 As a convience, if only one argument is passed then this argument
138 is assumed to be the name of the test (as in the above examples.)
139
140 Once "test_test" has been run test output will be redirected back
141 to the original filehandles that Test::Builder was connected to
142 (probably STDOUT and STDERR,) meaning any further tests you run
143 will function normally and cause success/errors for Test::Harness.
144
145 line_num
146 A utility function that returns the line number that the function
147 was called on. You can pass it an offset which will be added to
148 the result. This is very useful for working out the correct text
149 of diagnostic methods that contain line numbers.
150
151 Essentially this is the same as the "__LINE__" macro, but the
152 "line_num(+3)" idiom is arguably nicer.
153
154 In addition to the six exported functions there there exists one func‐
155 tion that can only be accessed with a fully qualified function call.
156
157 color
158 When "test_test" is called and the output that your tests generate
159 does not match that which you declared, "test_test" will print out
160 debug information showing the two conflicting versions. As this
161 output itself is debug information it can be confusing which part
162 of the output is from "test_test" and which was the original output
163 from your original tests. Also, it may be hard to spot things like
164 extraneous whitespace at the end of lines that may cause your test
165 to fail even though the output looks similar.
166
167 To assist you, if you have the Term::ANSIColor module installed
168 (which you should do by default from perl 5.005 onwards),
169 "test_test" can colour the background of the debug information to
170 disambiguate the different types of output. The debug output will
171 have it's background coloured green and red. The green part repre‐
172 sents the text which is the same between the executed and actual
173 output, the red shows which part differs.
174
175 The "color" function determines if colouring should occur or not.
176 Passing it a true or false value will enable or disable colouring
177 respectively, and the function called with no argument will return
178 the current setting.
179
180 To enable colouring from the command line, you can use the
181 Text::Builder::Tester::Color module like so:
182
183 perl -Mlib=Text::Builder::Tester::Color test.t
184
185 Or by including the Test::Builder::Tester::Color module directly in
186 the PERL5LIB.
187
189 Calls Test::Builder's "no_ending" method turning off the ending tests.
190 This is needed as otherwise it will trip out because we've run more
191 tests than we strictly should have and it'll register any failures we
192 had that we were testing for as real failures.
193
194 The color function doesn't work unless Term::ANSIColor is installed and
195 is compatible with your terminal.
196
197 Bugs (and requests for new features) can be reported to the author
198 though the CPAN RT system: <http://rt.cpan.org/NoAuth/Report‐
199 Bug.html?Queue=Test-Builder-Tester>
200
202 Copyright Mark Fowler <mark@twoshortplanks.com> 2002, 2004.
203
204 Some code taken from Test::More and Test::Catch, written by by Michael
205 G Schwern <schwern@pobox.com>. Hence, those parts Copyright Micheal G
206 Schwern 2001. Used and distributed with permission.
207
208 This program is free software; you can redistribute it and/or modify it
209 under the same terms as Perl itself.
210
212 This code has been tested explicitly on the following versions of perl:
213 5.7.3, 5.6.1, 5.6.0, 5.005_03, 5.004_05 and 5.004.
214
215 Thanks to Richard Clamp <richardc@unixbeard.net> for letting me use his
216 testing system to try this module out on.
217
219 Test::Builder, Test::Builder::Tester::Color, Test::More.
220
221
222
223perl v5.8.8 2001-09-21 Test::Builder::Tester(3pm)