Debian Codegraph @

README

raw
1 NAME
2 Test::Modern - precision testing for modern perl
3
4 SYNOPSIS
5 use Test::Modern;
6
7 # Your tests here
8
9 done_testing;
10
11 DESCRIPTION
12 Test::Modern provides the best features of Test::More, Test::Fatal,
13 Test::Warnings, Test::API, Test::LongString, and Test::Deep, as well as
14 ideas from Test::Requires, Test::DescribeMe, Test::Moose, and
15 Test::CleanNamespaces.
16
17 Test::Modern also automatically imposes strict and warnings on your
18 script, and loads IO::File. (Much of the same stuff Modern::Perl does.)
19
20 Although Test::Modern is a modern testing framework, it should run fine on
21 pre-modern versions of Perl. It should be easy to install on Perl 5.8.9
22 and above; and if you can persuade its dependencies to install (not
23 necessarily easy!), should be OK on anything back to Perl 5.6.1.
24
25 Features from Test::More
26 Test::Modern exports the following subs from Test::More:
27
28 `ok($truth, $description)`
29 `is($got, $expected, $description)`
30 `isnt($got, $unexpected, $description)`
31 `like($got, $regexp, $description)`
32 `unlike($got, $regexp, $description)`
33 `is_deeply($got, $expected, $description)`
34 `cmp_ok($got, $operator, $expected, $description)`
35 `new_ok($class, \@args, $name)`
36 `isa_ok($object|$subclass, $class, $name)`
37 `can_ok($object|$class, @methods)`
38 `pass($description)`
39 `fail($description)`
40 `subtest($description, sub { ... })`
41 `diag(@messages)`
42 `note(@messages)`
43 `explain(@messages)`
44 `skip($why, $count) if $reason`
45 `todo_skip($why, $count) if $reason`
46 $TODO
47 `plan(%plan)`
48 `done_testing`
49 `BAIL_OUT($reason)`
50
51 The `use_ok`, `require_ok`, `eq_array`, `eq_hash`, and `eq_set` functions
52 are also available, but not exported by default. For `use_ok` and
53 `require_ok` it's normally better to use the Perl built-ins `use` and
54 `require` which will die (failing your test) if things are not OK. For the
55 `eq_*` functions, they can usually be replaced by `is_deeply`.
56
57 Features from Test::Fatal
58 Test::Modern exports the following subs from Test::Fatal:
59
60 `exception { BLOCK }`
61
62 Features from Test::Warnings
63 Test::Modern exports the following subs from Test::Warnings:
64
65 `warning { BLOCK }`
66 `warnings { BLOCK }`
67
68 In addition, Test::Modern always enables the `had_no_warnings` test at the
69 end of the file, ensuring that your test script generated no warnings
70 other than the expected ones which were caught by `warnings` blocks. (See
71 also `PERL_TEST_MODERN_ALLOW_WARNINGS` in "ENVIRONMENT".)
72
73 Test::Modern can also export an additional function for testing warnings,
74 but does not export it by default:
75
76 `shouldnt_warn { BLOCK }`
77 Runs a block of code that will hopefully not warn, but might. Tests
78 that it doesn't warn, but performs that test as a "todo" test, so if
79 it fails, your test suite can still pass.
80
81 Features from Test::API
82 Test::Modern exports the following subs from Test::API:
83
84 `public_ok($package, @functions)`
85 `import_ok($package, export => \@functions, export_ok => \@functions)`
86 `class_api_ok($class, @methods)`
87
88 Features from Test::LongString
89 Test::Modern exports the following subs from Test::LongString:
90
91 `is_string($got, $expected, $description)`
92 `is_string_nows($got, $expected, $description)`
93 `like_string($got, $regexp, $description)`
94 `unlike_string($got, $regexp, $description)`
95 `contains_string($haystack, $needle, $description)`
96 `lacks_string($haystack, $needle, $description)`
97
98 Actually Test::Modern provides these via a wrapper. If Test::LongString is
99 not installed then Test::Modern will provide a fallback implementation
100 using Test::More's `is`, `isnt`, `like`, and `unlike` functions. (The
101 diagnostics won't be as good in the case of failures.)
102
103 Features from Test::Deep
104 Test::Modern exports the following subs from Test::Deep:
105
106 `cmp_deeply($got, $expected, $description)`
107
108 The following are not exported by default, but can be exported upon
109 request:
110
111 `ignore()`
112 `methods(%hash)`
113 `listmethods(%hash)`
114 `shallow($thing)`
115 `noclass($thing)`
116 `useclass($thing)`
117 `re($regexp, $capture_data, $flags)`
118 `superhashof(\%hash)`
119 `subhashof(\%hash)`
120 `bag(@elements)`
121 `set(@elements)`
122 `superbagof(@elements)`
123 `subbagof(@elements)`
124 `supersetof(@elements)`
125 `subsetof(@elements)`
126 `all(@expecteds)`
127 `any(@expecteds)`
128 `obj_isa($class)`
129 `array_each($thing)`
130 `str($string)`
131 `num($number, $tolerance)`
132 `bool($value)`
133 `code(\&subref)`
134
135 As an alternative to using those functions, Test::Modern exports a
136 constant `TD` upon which you can call them as methods:
137
138 # like Test::Deep::bag(@elements)
139 TD->bag(@elements)
140
141 Features from Test::Pod and Test::Pod::Coverage
142 These features are currently considered experimental. They may be removed
143 from a future version of Test::Modern.
144
145 Test::Modern can export the following subs from Test::Pod and
146 Test::Pod::Coverage, though they are not exported by default:
147
148 `pod_file_ok($file, $description)`
149 `all_pod_files_ok(@dirs)`
150 `pod_coverage_ok($module, $params, $description)`
151 `all_pod_coverage_ok($params, $description)`
152
153 In fact, Test::Modern wraps these tests in checks to see whether
154 Test::Pod(::Coverage) is installed, and the state of the
155 `RELEASE_TESTING`, `AUTHOR_TESTING`, and `EXTENDED_TESTING` environment
156 variables. If none of those environment variables is set to true, then the
157 test is skipped altogether. If Test::Pod(::Coverage) is not installed,
158 then the test is skipped, unless `RELEASE_TESTING` is true, in which case
159 *Test::Pod(::Coverage) must be installed*.
160
161 This is usually a pretty sensible behaviour. You want authors to be made
162 aware of pod errors if possible. You want to make sure they are tested
163 before doing a release. End users probably don't want a pod formatting
164 error to prevent them from installing the software, unless they opt into
165 it using `EXTENDED_TESTING`.
166
167 Also, Test::Modern wraps the `all_*` functions to run them in a subtest
168 (because otherwise they can interfere with your test plans).
169
170 Features from Test::Version
171 These features are currently considered experimental. They may be removed
172 from a future version of Test::Modern.
173
174 Test::Modern can export the following subs from Test::Version, though they
175 are not exported by default:
176
177 `version_ok($file, $description)`
178 `version_all_ok(@dirs)`
179
180 These are wrapped similarly to those described in the "Features from
181 Test::Pod and Test::Coverage".
182
183 Test::Modern can also export another sub based on `version_all_ok`:
184
185 `version_all_same(@dirs)`
186 Acts like `version_all_ok` but also checks that all modules have the
187 same version number.
188
189 Features inspired by Test::Moose
190 Test::Modern does not use Test::Moose, but does provide the following
191 function inspired by it:
192
193 `does_ok($object|$subclass, $class, $name)`
194 Like `isa_ok`, but calls `$obj->DOES` instead of `$obj->isa`.
195
196 Features inspired by Test::CleanNamespaces
197 Test::Modern does not use Test::CleanNamespaces, but does provide the
198 following function inspired by it:
199
200 `namespaces_clean(@namespaces)`
201 Tests that namespaces don't contain any imported functions. (i.e. you
202 haven't forgotten to use namespace::autoclean or namespace::sweep in a
203 class).
204
205 Unlike the version of this function supplied with
206 Test::CleanNamespaces, if @namespaces contains more than one
207 namespace, these will be run in a subtest, so the whole thing will
208 only count as one test.
209
210 Features inspired by Test::Benchmark
211 Test::Modern does not use Test::Benchmark, but does provide the following
212 feature inspired by it:
213
214 `is_fastest($implementation, $times, \%implementations, $desc)`
215 use Test::Modern qw( is_fastest );
216
217 is_fastest("speedy", -1, {
218 "speedy" => sub { ... },
219 "slowcoach" => sub { ... },
220 });
221
222 This ensures that the named coderef runs the fastest out of a hashref
223 of alternatives. The -1 parameter in the example is the number of
224 times to run the coderefs (see Benchmark for more details, including
225 how numbers less than zero are interpreted).
226
227 Caveat: on fast computers, a set of coderefs that you might expect to
228 differ in speed might all run in a negligible period of time, and thus be
229 rounded to zero, in which case your test case could randomly fail. Use
230 this test with caution!
231
232 Caveat the second: these tests tend to be slow. Use sparingly.
233
234 Because of the aforementioned caveats, it is a good idea to move your
235 benchmarking tests into separate test scripts, keeping an imaginary wall
236 between them and the bulk of your test suite (which tests correctness
237 rather than speed).
238
239 Test::Modern provides an import hint suitable for including at the top of
240 these benchmarking tests to mark them as being primarily concerned with
241 speed:
242
243 use Test::Modern -benchmark;
244
245 This will not only import the `is_fastest` function, but will also *skip
246 the entire script* unless one of the `EXTENDED_TESTING` or
247 `RELEASE_TESTING` environment variables is set.
248
249 Features inspired by Test::Requires
250 Test::Modern does not use Test::Requires, but does provide the following
251 feature inspired by it:
252
253 `use Test::Modern -requires => \%requirements`
254 This will skip the entire test script if the requirements are not met.
255 For example:
256
257 use Test::Modern -requires => {
258 'perl' => '5.010',
259 'Moose' => '2.11',
260 'namespace::autoclean' => undef,
261 };
262
263 Features inspired by Test::RequiresInternet
264 Similarly you can skip the test script if an Internet connection is not
265 available:
266
267 use Test::Modern -internet;
268
269 You can check for the ability to connect to particular hosts and ports:
270
271 use Test::Modern -internet => [
272 'www.example.com' => 'http',
273 '8.8.8.8' => 53,
274 ];
275
276 Test::Modern does not use Test::RequiresInternet but I've stolen much of
277 the latter's implementation.
278
279 Features inspired by Test::Without::Module
280 Test::Modern does not use Test::Without::Module, but does provide the
281 following feature inspired by it:
282
283 `use Test::Modern -without => \@modules`
284 This will run the tests as if the module was not installed. Useful for
285 testing things in the absence of optional dependencies. For example:
286
287 use Test::Modern -without => [ "Class::XSAccessor" ];
288
289 It cannot suppress modules from being loaded if they are required by
290 Test::Modern itself. To get a list of what modules Test::Modern
291 requires, run the following command:
292
293 perl -MTest::Modern -le'print for sort keys %INC'
294
295 (Note that the actual implementation is mostly stolen from Devel::Hide
296 which seems to behave better than Test::Without::Module.)
297
298 Features inspired by Test::DescribeMe
299 These export tags allow you to classify tests as "author tests", "release
300 tests", "extended tests", or "interactive tests".
301
302 They will cause your test script to be skipped depending on various
303 environment variables.
304
305 `use Test::Modern -author`
306 `use Test::Modern -release`
307 `use Test::Modern -extended`
308 `use Test::Modern -interactive`
309
310 Features inspired by Test::Lib
311 These features are currently considered experimental. They may be removed
312 from a future version of Test::Modern.
313
314 Test::Modern tries to find a directory called `t/lib` by traversing up the
315 directory tree from the caller file. If found, this directory will be
316 added to @INC.
317
318 Test::Lib would croak if such a directory cannot be found. Test::Modern
319 carries on if it can't find it. If you want something more like the
320 Test::Lib behaviour, use the `-lib` import tag:
321
322 use Test::Modern -lib;
323
324 Brand Spanking New Features
325 Test::Modern provides a shortcut which combines several features it has
326 pilfered from other testing modules:
327
328 `object_ok($object, $name, %tests)`
329 Runs a gamut of subtests on an object:
330
331 object_ok(
332 $object,
333 $name,
334 isa => \@classes,
335 does => \@roles,
336 can => \@methods,
337 api => \@methods,
338 clean => $boolean,
339 more => sub {
340 my $object = shift;
341 ...;
342 }
343 );
344
345 $object may be a blessed object, or an unblessed coderef which returns
346 a blessed object. The `isa` test runs `isa_ok`; the `does` test runs
347 `does_ok`, the `can` test runs `can_ok`, and the `api` test runs
348 `class_api_ok`. `clean` allows you to run `namespaces_clean` on the
349 object's class.
350
351 `more` introduces a coderef for running more tests. Within this sub
352 you can use any of the standard Test::More, Test::LongString, etc
353 tests. It is automatically run in a `try` block (see Try::Tiny);
354 throwing an exception will cause the test to fail, but not cause the
355 script to end.
356
357 Any of the test hash keys may be omitted, in which case that test will
358 not be run. $name may be omitted.
359
360 If the test succeeds, it returns the object (which may be useful for
361 further tests). Otherwise, returns `undef`.
362
363 Practical example:
364
365 my $bob = object_ok(
366 sub { Employee->new(name => 'Robert Jones') },
367 '$bob',
368 isa => [qw( Employee Person Moo::Object )],
369 does => [qw( Employable )],
370 can => [qw( name employee_number tax_code )],
371 clean => 1,
372 more => sub {
373 my $object = shift;
374 is($object->name, "Robert Jones");
375 like($object->employee_number, qr/^[0-9]+$/);
376 },
377 );
378
379 # make further use of $bob
380 object_ok(
381 sub { $bob->line_manager },
382 isa => [qw( Person )],
383 );
384
385 EXPORT
386 This module uses Exporter::Tiny to perform its exports. This allows
387 exported subs to be renamed, etc.
388
389 The following export tags are supported:
390
391 `-more`
392 Exports the "Features from Test::More", except deprecated ones.
393
394 `-deprecated`
395 Exports the deprecated Test::More features.
396
397 `-fatal`
398 Exports the "Features from Test::Fatal".
399
400 `-warnings`
401 Exports the "Features from Test::Warnings".
402
403 `-api`
404 Exports the "Features from Test::API", including `class_api_ok`.
405
406 `-strings`
407 Exports the "Features from Test::LongString".
408
409 `-deep`
410 Exports cmp_deeply and TD.
411
412 `-deeper`
413 Exports *all* the "Features from Test::Deep".
414
415 `-moose`
416 Exports the "Features inspired by Test::Moose".
417
418 `-clean`
419 Exports the "Features inspired by Test::CleanNamespaces".
420
421 `-pod`
422 Exports the "Features from Test::Pod and Test::Pod::Coverage".
423
424 `-versions`
425 Exports the "Features from Test::Version".
426
427 `-default`
428 Exports the default features -- all of the above except `-deprecated`,
429 `-pod`, `-versions`, and `-deeper`. Also exports `object_ok`.
430
431 `-all`
432 Exports all of the above features *including* `-deprecated`, `-pod`,
433 `-versions`, `-deeper`, `object_ok`, and `shouldnt_warn`.
434
435 `-author`, `-extended`, `-interactive`, and `-release`
436 Classify the test script.
437
438 `-benchmark`
439 The test script consists mostly of benchmarking.
440
441 `-internet`
442 The test script requires Internet access.
443
444 `-requires`, `-without`
445 Specify modules required or hidden for these test cases.
446
447 `-lib`
448 Makes the absence of a `t/lib` directory fatal.
449
450 See "Features inspired by Test::Lib".
451
452 `-verbose`
453 Makes test output more verbose. (Currently only `is_faster` takes
454 notice of this.)
455
456 $TODO is currently *always* exported.
457
458 ENVIRONMENT
459 Test::Modern is affected by the following environment variables:
460
461 `AUTHOR_TESTING`, `AUTOMATED_TESTING`, `EXTENDED_TESTING`,
462 `RELEASE_TESTING`
463 These variables affect the behaviour of Test::Modern's pod-checking
464 and version-checking. See "Features from Test::Pod and Test::Coverage"
465 and "Features from Test::Version".
466
467 They also can trigger certain import tags to skip a test script. See
468 "Features inspired by Test::DescribeMe", and "Features inspired by
469 Test::Benchmark"
470
471 `NO_NETWORK_TESTS`
472 Automatically skips any tests which indicate that they require
473 Internet access, without even checking to see if the Internet is
474 accessible. See "Features inspired by Test::RequiresInternet".
475
476 `PERL_TEST_MODERN_ALLOW_WARNINGS`
477 Setting this to true allows you to disable Test::Warnings' end test.
478
479 Normally the end test will cause a test script to fail if any
480 unexpected warnings are encountered during its execution. New versions
481 of Perl, and upgrades of dependencies can cause a previously good test
482 suite to start emitting warnings. This environment variable can be
483 used as a "quick fix" to get the test suite passing again.
484
485 BUGS
486 Please report any bugs to
487 <http://rt.cpan.org/Dist/Display.html?Queue=Test-Modern>.
488
489 SEE ALSO
490 My Favourite Test::* Modules
491 <http://blogs.perl.org/users/toby_inkster/2014/02/my-favourite-test-module
492 s.html>, Precision Testing for Modern Perl
493 <http://blogs.perl.org/users/toby_inkster/2014/03/precision-testing-for-mo
494 dern-perl.html>.
495
496 Test::More, Test::Fatal, Test::Warnings, Test::API, Test::LongString,
497 Test::Deep, Test::Moose, Test::CleanNamespaces, Test::Requires,
498 Test::Without::Module, Test::RequiresInternet, Test::DescribeMe,
499 Test::Lib, Test::Pod, Test::Pod::Coverage, Test::Version.
500
501 Test::Most is a similar idea, but provides a slightly different
502 combination of features.
503
504 AUTHOR
505 Toby Inkster <[email protected]>.
506
507 COPYRIGHT AND LICENCE
508 This software is copyright (c) 2014 by Toby Inkster.
509
510 This is free software; you can redistribute it and/or modify it under the
511 same terms as the Perl 5 programming language system itself.
512
513 DISCLAIMER OF WARRANTIES
514 THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
515 WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
516 MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
517