Debian Codegraph @

upstream/debian-unstable/man3/Test2::Manual::Testing::Introduction.3perl

raw
1 .\" -*- mode: troff; coding: utf-8 -*-
2 .\" Automatically generated by Pod::Man 5.0102 (Pod::Simple 3.45)
3 .\"
4 .\" Standard preamble:
5 .\" ========================================================================
6 .de Sp \" Vertical space (when we can't use .PP)
7 .if t .sp .5v
8 .if n .sp
9 ..
10 .de Vb \" Begin verbatim text
11 .ft CW
12 .nf
13 .ne \\$1
14 ..
15 .de Ve \" End verbatim text
16 .ft R
17 .fi
18 ..
19 .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>.
20 .ie n \{\
21 . ds C` ""
22 . ds C' ""
23 'br\}
24 .el\{\
25 . ds C`
26 . ds C'
27 'br\}
28 .\"
29 .\" Escape single quotes in literal strings from groff's Unicode transform.
30 .ie \n(.g .ds Aq \(aq
31 .el .ds Aq '
32 .\"
33 .\" If the F register is >0, we'll generate index entries on stderr for
34 .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
35 .\" entries marked with X<> in POD. Of course, you'll have to process the
36 .\" output yourself in some meaningful fashion.
37 .\"
38 .\" Avoid warning from groff about undefined register 'F'.
39 .de IX
40 ..
41 .nr rF 0
42 .if \n(.g .if rF .nr rF 1
43 .if (\n(rF:(\n(.g==0)) \{\
44 . if \nF \{\
45 . de IX
46 . tm Index:\\$1\t\\n%\t"\\$2"
47 ..
48 . if !\nF==2 \{\
49 . nr % 0
50 . nr F 2
51 . \}
52 . \}
53 .\}
54 .rr rF
55 .\" ========================================================================
56 .\"
57 .IX Title "Test2::Manual::Testing::Introduction 3perl"
58 .TH Test2::Manual::Testing::Introduction 3perl 2025-11-16 "perl v5.40.1" "Perl Programmers Reference Guide"
59 .\" For nroff, turn off justification. Always turn off hyphenation; it makes
60 .\" way too many mistakes in technical documents.
61 .if n .ad l
62 .nh
63 .SH NAME
64 Test2::Manual::Testing::Introduction \- Introduction to testing with Test2.
65 .SH DESCRIPTION
66 .IX Header "DESCRIPTION"
67 This tutorial is a beginners introduction to testing. This will take you
68 through writing a test file, making assertions, and running your test.
69 .SH BOILERPLATE
70 .IX Header "BOILERPLATE"
71 .SS "THE TEST FILE"
72 .IX Subsection "THE TEST FILE"
73 Test files typically are placed inside the \f(CW\*(C`t/\*(C'\fR directory, and end with the
74 \&\f(CW\*(C`.t\*(C'\fR file extension.
75 .PP
76 \&\f(CW\*(C`t/example.t\*(C'\fR:
77 .PP
78 .Vb 1
79 \& use Test2::V0;
80 \&
81 \& # Assertions will go here
82 \&
83 \& done_testing;
84 .Ve
85 .PP
86 This is all the boilerplate you need.
87 .IP "use Test2::V0;" 4
88 .IX Item "use Test2::V0;"
89 This loads a collection of testing tools that will be described later in the
90 tutorial. This will also turn on \f(CW\*(C`strict\*(C'\fR and \f(CW\*(C`warnings\*(C'\fR for you.
91 .IP done_testing; 4
92 .IX Item "done_testing;"
93 This should always be at the end of your test files. This tells Test2 that
94 you are done making assertions. This is important as \f(CW\*(C`test2\*(C'\fR will assume the
95 test did not complete successfully without this, or some other form of test
96 "plan".
97 .SS "DIST CONFIG"
98 .IX Subsection "DIST CONFIG"
99 You should always list bundles and tools directly. You should not simply list
100 Test2::Suite and call it done, bundles and tools may be moved out of
101 Test2::Suite to their own dists at any time.
102 .PP
103 \fIDist::Zilla\fR
104 .IX Subsection "Dist::Zilla"
105 .PP
106 .Vb 2
107 \& [Prereqs / TestRequires]
108 \& Test2::V0 = 0.000060
109 .Ve
110 .PP
111 \fIExtUtils::MakeMaker\fR
112 .IX Subsection "ExtUtils::MakeMaker"
113 .PP
114 .Vb 7
115 \& my %WriteMakefileArgs = (
116 \& ...,
117 \& "TEST_REQUIRES" => {
118 \& "Test2::V0" => "0.000060"
119 \& },
120 \& ...
121 \& );
122 .Ve
123 .PP
124 \fIModule::Install\fR
125 .IX Subsection "Module::Install"
126 .PP
127 .Vb 1
128 \& test_requires \*(AqTest2::V0\*(Aq => \*(Aq0.000060\*(Aq;
129 .Ve
130 .PP
131 \fIModule::Build\fR
132 .IX Subsection "Module::Build"
133 .PP
134 .Vb 7
135 \& my $build = Module::Build\->new(
136 \& ...,
137 \& test_requires => {
138 \& "Test2::V0" => "0.000060",
139 \& },
140 \& ...
141 \& );
142 .Ve
143 .SH "MAKING ASSERTIONS"
144 .IX Header "MAKING ASSERTIONS"
145 The most simple tool for making assertions is \f(CWok()\fR. \f(CWok()\fR lets you assert
146 that a condition is true.
147 .PP
148 .Vb 1
149 \& ok($CONDITION, "Description of the condition");
150 .Ve
151 .PP
152 Here is a complete \f(CW\*(C`t/example.t\*(C'\fR:
153 .PP
154 .Vb 1
155 \& use Test2::V0;
156 \&
157 \& ok(1, "1 is true, so this will pass");
158 \&
159 \& done_testing;
160 .Ve
161 .SH "RUNNING THE TEST"
162 .IX Header "RUNNING THE TEST"
163 Test files are simply scripts. Just like any other script you can run the test
164 directly with perl. Another option is to use a test "harness" which runs the
165 test for you, and provides extra information and checks the scripts exit value
166 for you.
167 .SS "RUN DIRECTLY"
168 .IX Subsection "RUN DIRECTLY"
169 .Vb 1
170 \& $ perl \-Ilib t/example.t
171 .Ve
172 .PP
173 Which should produce output like this:
174 .PP
175 .Vb 3
176 \& # Seeded srand with seed \*(Aq20161028\*(Aq from local date.
177 \& ok 1 \- 1 is true, so this will pass
178 \& 1..1
179 .Ve
180 .PP
181 If the test had failed (\f(CW\*(C`ok(0, ...)\*(C'\fR) it would look like this:
182 .PP
183 .Vb 3
184 \& # Seeded srand with seed \*(Aq20161028\*(Aq from local date.
185 \& not ok 1 \- 0 is false, so this will fail
186 \& 1..1
187 .Ve
188 .PP
189 Test2 will also set the exit value of the script, a successful run will have an
190 exit value of 0, a failed run will have a non-zero exit value.
191 .SS "USING YATH"
192 .IX Subsection "USING YATH"
193 The \f(CW\*(C`yath\*(C'\fR command line tool is provided by Test2::Harness which you may
194 need to install yourself from cpan. \f(CW\*(C`yath\*(C'\fR is the harness written specifically
195 for Test2.
196 .PP
197 .Vb 1
198 \& $ yath \-Ilib t/example.t
199 .Ve
200 .PP
201 This will produce output similar to this:
202 .PP
203 .Vb 1
204 \& ( PASSED ) job 1 t/example.t
205 \&
206 \& ================================================================================
207 \&
208 \& Run ID: 1508027909
209 \&
210 \& All tests were successful!
211 .Ve
212 .PP
213 You can also request verbose output with the \f(CW\*(C`\-v\*(C'\fR flag:
214 .PP
215 .Vb 1
216 \& $ yath \-Ilib \-v t/example.t
217 .Ve
218 .PP
219 Which produces:
220 .PP
221 .Vb 5
222 \& ( LAUNCH ) job 1 example.t
223 \& ( NOTE ) job 1 Seeded srand with seed \*(Aq20171014\*(Aq from local date.
224 \& [ PASS ] job 1 + 1 is true, so this will pass
225 \& [ PLAN ] job 1 Expected asserions: 1
226 \& ( PASSED ) job 1 example.t
227 \&
228 \& ================================================================================
229 \&
230 \& Run ID: 1508028002
231 \&
232 \& All tests were successful!
233 .Ve
234 .SS "USING PROVE"
235 .IX Subsection "USING PROVE"
236 The \f(CW\*(C`prove\*(C'\fR command line tool is provided by the Test::Harness module which
237 comes with most versions of perl. Test::Harness is dual-life, which means
238 you can also install the latest version from cpan.
239 .PP
240 .Vb 1
241 \& $ prove \-Ilib t/example.t
242 .Ve
243 .PP
244 This will produce output like this:
245 .PP
246 .Vb 4
247 \& example.t .. ok
248 \& All tests successful.
249 \& Files=1, Tests=1, 0 wallclock secs ( 0.01 usr 0.00 sys + 0.05 cusr 0.00 csys = 0.06 CPU)
250 \& Result: PASS
251 .Ve
252 .PP
253 You can also request verbose output with the \f(CW\*(C`\-v\*(C'\fR flag:
254 .PP
255 .Vb 1
256 \& $ prove \-Ilib \-v t/example.t
257 .Ve
258 .PP
259 The verbose output looks like this:
260 .PP
261 .Vb 8
262 \& example.t ..
263 \& # Seeded srand with seed \*(Aq20161028\*(Aq from local date.
264 \& ok 1 \- 1 is true, so this will pass
265 \& 1..1
266 \& ok
267 \& All tests successful.
268 \& Files=1, Tests=1, 0 wallclock secs ( 0.02 usr 0.00 sys + 0.06 cusr 0.00 csys = 0.08 CPU)
269 \& Result: PASS
270 .Ve
271 .SH "THE ""PLAN"""
272 .IX Header "THE ""PLAN"""
273 All tests need a "plan". The job of a plan is to make sure you ran all the
274 tests you expected. The plan prevents a passing result from a test that exits
275 before all the tests are run.
276 .PP
277 There are 2 primary ways to set the plan:
278 .IP \fBdone_testing()\fR 4
279 .IX Item "done_testing()"
280 The most common, and recommended way to set a plan is to add \f(CW\*(C`done_testing\*(C'\fR at
281 the end of your test file. This will automatically calculate the plan for you
282 at the end of the test. If the test were to exit early then \f(CW\*(C`done_testing\*(C'\fR
283 would not run and no plan would be found, forcing a failure.
284 .IP plan($COUNT) 4
285 .IX Item "plan($COUNT)"
286 The \f(CWplan()\fR function allows you to specify an exact number of assertions you
287 want to run. If you run too many or too few assertions then the plan will not
288 match and it will be counted as a failure. The primary problem with this way of
289 planning is that you need to add up the number of assertions, and adjust the
290 count whenever you update the test file.
291 .Sp
292 \&\f(CWplan()\fR must be used before all assertions, or after all assertions, it
293 cannot be done in the middle of making assertions.
294 .SH "ADDITIONAL ASSERTION TOOLS"
295 .IX Header "ADDITIONAL ASSERTION TOOLS"
296 The Test2::V0 bundle provides a lot more than \f(CWok()\fR,
297 \&\f(CWplan()\fR, and \f(CWdone_testing()\fR. The biggest tools to note are:
298 .ie n .IP "is($a, $b, $description)" 4
299 .el .IP "is($a, \f(CW$b\fR, \f(CW$description\fR)" 4
300 .IX Item "is($a, $b, $description)"
301 \&\f(CWis()\fR allows you to compare 2 structures and insure they are identical. You
302 can use it for simple string comparisons, or even deep data structure
303 comparisons.
304 .Sp
305 .Vb 1
306 \& is("foo", "foo", "Both strings are identical");
307 \&
308 \& is(["foo", 1], ["foo", 1], "Both arrays contain the same elements");
309 .Ve
310 .ie n .IP "like($a, $b, $description)" 4
311 .el .IP "like($a, \f(CW$b\fR, \f(CW$description\fR)" 4
312 .IX Item "like($a, $b, $description)"
313 \&\f(CWlike()\fR is similar to \f(CWis()\fR except that it only checks items listed on the
314 right, it ignores any extra values found on the left.
315 .Sp
316 .Vb 1
317 \& like([1, 2, 3, 4], [1, 2, 3], "Passes, the extra element on the left is ignored");
318 .Ve
319 .Sp
320 You can also used regular expressions on the right hand side:
321 .Sp
322 .Vb 1
323 \& like("foo bar baz", qr/bar/, "The string matches the regex, this passes");
324 .Ve
325 .Sp
326 You can also nest the regexes:
327 .Sp
328 .Vb 1
329 \& like([1, 2, \*(Aqfoo bar baz\*(Aq, 3], [1, 2, qr/bar/], "This passes");
330 .Ve
331 .SH "SEE ALSO"
332 .IX Header "SEE ALSO"
333 Test2::Manual \- Primary index of the manual.
334 .SH SOURCE
335 .IX Header "SOURCE"
336 The source code repository for Test2\-Manual can be found at
337 \&\fIhttps://github.com/Test\-More/Test2\-Suite/\fR.
338 .SH MAINTAINERS
339 .IX Header "MAINTAINERS"
340 .IP "Chad Granum <[email protected]>" 4
341 .IX Item "Chad Granum <[email protected]>"
342 .SH AUTHORS
343 .IX Header "AUTHORS"
344 .PD 0
345 .IP "Chad Granum <[email protected]>" 4
346 .IX Item "Chad Granum <[email protected]>"
347 .PD
348 .SH COPYRIGHT
349 .IX Header "COPYRIGHT"
350 Copyright 2018 Chad Granum <[email protected]>.
351 .PP
352 This program is free software; you can redistribute it and/or
353 modify it under the same terms as Perl itself.
354 .PP
355 See \fIhttp://dev.perl.org/licenses/\fR