File Coverage

blib/lib/Test/Class/Moose.pm
Criterion Covered Total %
statement 120 126 95.2
branch 29 36 80.5
condition 5 6 83.3
subroutine 30 30 100.0
pod 6 9 66.6
total 190 207 91.7


line stmt bran cond sub pod time code
1             package Test::Class::Moose;
2              
3             # ABSTRACT: Serious testing for serious Perl
4              
5 31     31   268712 use strict;
  31         97  
  31         907  
6 31     31   161 use warnings;
  31         66  
  31         765  
7 31     31   1095 use namespace::autoclean;
  31         37162  
  31         206  
8              
9 31     31   2549 use 5.010000;
  31         110  
10              
11             our $VERSION = '0.99';
12              
13 31     31   17481 use Moose 2.0000;
  31         13638752  
  31         360  
14 31     31   239773 use Carp;
  31         100  
  31         2562  
15 31     31   23046 use Import::Into;
  31         19424  
  31         1030  
16 31     31   14553 use Sub::Attribute;
  31         75093  
  31         3111  
17              
18             # We don't use anything from this module in this one, but we want to require a
19             # specific version.
20 31     31   15878 use Test2 1.302118 ();
  31         4159  
  31         833  
21              
22 31     31   16155 use Test::Class::Moose::AttributeRegistry;
  31         114  
  31         1107  
23 31     31   16054 use Test::Class::Moose::Config;
  31         167  
  31         5976  
24 31     31   414 use Test::Class::Moose::Deprecated;
  31         87  
  31         998  
25              
26             sub __sub_attr_declaration_code {
27              
28             # XXX sharing this behavior here because my first attempt at creating a
29             # role was a complete failure. MooseX::MethodAttributes can help here, but
30             # I have to parse the attributes manually (as far as I can tell) and I
31             # don't have the simple declarative style any more.
32 33     33   11130 return <<'DECLARE_ATTRIBUTES';
33             sub Tags : ATTR_SUB {
34             my ( $class, $symbol, undef, undef, $data, undef, $file, $line ) = @_;
35              
36             my @tags;
37             if ($data) {
38             $data =~ s/^\s+//g;
39             @tags = split /\s+/, $data;
40             }
41              
42             if ( $symbol eq 'ANON' ) {
43             die "Cannot tag anonymous subs at file $file, line $line\n";
44             }
45              
46             my $method = *{$symbol}{NAME};
47              
48             { # block for localising $@
49             local $@;
50              
51             Test::Class::Moose::AttributeRegistry->add_tags(
52             $class,
53             $method,
54             \@tags,
55             );
56             if ($@) {
57             croak "Error in adding tags: $@";
58             }
59             }
60             }
61              
62             sub Test : ATTR_SUB {
63             my ( $class, $symbol, undef, undef, undef, undef, $file, $line ) = @_;
64              
65             if ( $symbol eq 'ANON' ) {
66             croak("Cannot add plans to anonymous subs at file $file, line $line");
67             }
68              
69             my $method = *{$symbol}{NAME};
70             if ( $method =~ /^test_(?:startup|setup|teardown|shutdown)$/ ) {
71             croak("Test control method '$method' may not have a Test attribute");
72             }
73              
74             Test::Class::Moose::AttributeRegistry->add_plan(
75             $class,
76             $method,
77             1,
78             );
79             $class->meta->add_before_method_modifier($method, sub {
80             my $test = shift;
81             $test->test_report->plan(1);
82             });
83             }
84              
85             sub Tests : ATTR_SUB {
86             my ( $class, $symbol, undef, undef, $data, undef, $file, $line ) = @_;
87              
88             if ( $symbol eq 'ANON' ) {
89             croak("Cannot add plans to anonymous subs at file $file, line $line");
90             }
91              
92             my $method = *{$symbol}{NAME};
93             if ( $method =~ /^test_(?:startup|setup|teardown|shutdown)$/ ) {
94             croak("Test control method '$method' may not have a Test attribute");
95             }
96              
97             Test::Class::Moose::AttributeRegistry->add_plan(
98             $class,
99             $method,
100             $data,
101             );
102             if ( defined $data ) {
103             $class->meta->add_before_method_modifier($method, sub {
104             my $test = shift;
105             $test->test_report->plan($data);
106             });
107             }
108             }
109             DECLARE_ATTRIBUTES
110             }
111              
112             BEGIN {
113             ## no critic (BuiltinFunctions::ProhibitStringyEval, ErrorHandling::RequireCheckingReturnValueOfEval)
114 31 100   31 0 4758 eval __PACKAGE__->__sub_attr_declaration_code;
  31 50   31 1 265  
  31 50   31 0 98  
  31 50   31   346  
  23 100   23   35040  
  23 50   3   58  
  23 50   6   116  
  22 100   6   81  
  22         124  
  23         94  
  0         0  
  23         38  
  23         73  
  23         56  
  23         62  
  23         211  
  23         245  
  0         0  
  31         16838  
  31         754  
  31         251  
  3         7024  
  3         20  
  0         0  
  3         9  
  3         12  
  3         23  
  1         22  
  2         19  
  2         10  
  2         81  
  2         67  
  31         12304  
  31         98  
  31         170  
  6         2012  
  6         26  
  0         0  
  6         8  
  6         15  
  6         20  
  0         0  
  6         26  
  6         29  
  4         14  
  6         337  
  6         165  
115 31 50       28800 croak($@) if $@;
116             }
117              
118             has 'test_report' => (
119             is => 'ro',
120             isa => 'Test::Class::Moose::Report',
121             );
122              
123             has 'test_class' => (
124             is => 'ro',
125             isa => 'Str',
126             init_arg => undef,
127             default => sub { ref $_[0] },
128             );
129              
130             has 'test_instance_name' => (
131             is => 'rw',
132             writer => '_set_test_instance_name',
133             isa => 'Str',
134             init_arg => undef,
135             );
136              
137             has 'test_skip' => (
138             is => 'rw',
139             isa => 'Str',
140             clearer => 'test_skip_clear',
141             );
142              
143             sub import {
144 168     168   60177 shift;
145 168         800 my %args = @_;
146              
147 168         564 my $caller = caller;
148              
149 168         774 my @imports = qw(
150             Moose
151             Sub::Attribute
152             strict
153             warnings
154             );
155              
156 168 100       856 unless ( $args{bare} ) {
157 24         6538 require Test::Most;
158 24         559386 push @imports, 'Test::Most';
159             }
160              
161 168         2268 $_->import::into($caller) for @imports;
162              
163 168 100 66     1668372 if ( my $parent = ( delete $args{parent} || delete $args{extends} ) ) {
164 35 100       229 my @parents = 'ARRAY' eq ref $parent ? @$parent : $parent;
165 35         280 $caller->meta->superclasses(@parents);
166 35 100       68733 unless ( $caller->isa(__PACKAGE__) ) {
167 3         38 croak( _bad_parents_error($caller) );
168             }
169             }
170             else {
171 133         1099 $caller->meta->superclasses(__PACKAGE__);
172             }
173             }
174              
175             sub _bad_parents_error {
176 3     3   9 my $class = shift;
177              
178 3         14 my @ancestors = $class->meta->linearized_isa;
179              
180             # The first element is always $class
181 3         77 shift @ancestors;
182              
183 3         13 my $me = "The $class class";
184 3 100       12 if ( scalar @ancestors == 1 ) {
185 2         57 return "$me inherits from $ancestors[0] which does not inherit from "
186             . __PACKAGE__;
187             }
188              
189 1         4 my $heritage;
190 1 50       7 if ( scalar @ancestors == 2 ) {
191 0         0 $heritage .= join ' and ', @ancestors;
192             }
193             else {
194 1         4 my $final = pop @ancestors;
195 1         5 $heritage .= join ', ', @ancestors;
196 1         4 $heritage .= ", and $final";
197             }
198              
199             return
200 1         33 "$me has $heritage as ancestors "
201             . 'but none of these classes inherit from '
202             . __PACKAGE__;
203             }
204              
205             ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines)
206             sub _tcm_make_test_class_instances {
207 40     40   128 my $test_class = shift;
208              
209 40         1656 my $instance = $test_class->new(@_);
210 40         41081 $instance->_set_test_instance_name($test_class);
211              
212 40         212 return $instance;
213             }
214              
215             sub test_methods {
216 190     190 1 3187 my $self = shift;
217              
218 190         503 my @method_list;
219 190         1621 foreach my $method ( $self->meta->get_all_methods ) {
220              
221             # attributes cannot be test methods
222 6957 100       149048 next if $method->isa('Moose::Meta::Method::Accessor');
223              
224 5690         8281 my $class = ref $self;
225 5690         11195 my $name = $method->name;
226             next
227 5690 100 100     16542 unless $name =~ /^test_/
228             || Test::Class::Moose::AttributeRegistry->has_test_attribute(
229             $class, $name );
230              
231             # don't use anything defined in this package
232 1500 100       6434 next if __PACKAGE__->can($name);
233 550         1224 push @method_list => $name;
234             }
235              
236 190         1199 return @method_list;
237             }
238              
239             # empty stub methods guarantee that subclasses can always call these
240       38 1   sub test_startup { }
241       45 1   sub test_setup { }
242       102 1   sub test_teardown { }
243       40 1   sub test_shutdown { }
244              
245 41     41 0 102692 sub run_control_methods_on_skip {0}
246              
247             __PACKAGE__->meta->make_immutable;
248              
249             1;
250              
251             __END__
252              
253             =pod
254              
255             =encoding UTF-8
256              
257             =head1 NAME
258              
259             Test::Class::Moose - Serious testing for serious Perl
260              
261             =head1 VERSION
262              
263             version 0.99
264              
265             =head1 SYNOPSIS
266              
267             package TestsFor::DateTime;
268             use Test::Class::Moose;
269             use DateTime;
270              
271             # methods that begin with test_ are test methods.
272             sub test_constructor {
273             my $test = shift;
274             $test->test_report->plan(3); # strictly optional
275              
276             can_ok 'DateTime', 'new';
277             my %args = (
278             year => 1967,
279             month => 6,
280             day => 20,
281             );
282             isa_ok my $date = DateTime->new(%args), 'DateTime';
283             is $date->year, $args{year}, '... and the year should be correct';
284             }
285              
286             1;
287              
288             =head1 DESCRIPTION
289              
290             See the L<Test::Class::Moose home page|http://houseabsolute.github.io/test-class-moose/> for
291             a summary.
292              
293             C<Test::Class::Moose> is a powerful testing framework for Perl. Out of the box
294             you get:
295              
296             =over 4
297              
298             =item * Reporting
299              
300             =item * Extensibility
301              
302             =item * Tagging tests
303              
304             =item * Parallel testing
305              
306             =item * Test inheritance
307              
308             =item * Write your tests using Moose
309              
310             =item * All the testing functions and behavior from Test::Most
311              
312             =item * Event handlers for startup, setup, teardown, and shutdown of test classes
313              
314             =back
315              
316             Better docs will come later. You should already know how to use Moose and
317             L<Test::Class>.
318              
319             =for Pod::Coverage Tags Tests runtests run_control_methods_on_skip
320              
321             =head1 BASICS
322              
323             =head2 Inheriting from Test::Class::Moose
324              
325             Just C<use Test::Class::Moose>. That's all. You'll get all L<Test::Most> test
326             functions, too, along with C<strict> and C<warnings>. You can use all L<Moose>
327             behavior, too.
328              
329             When you C<use Test::Class::Moose> it inserts itself as a parent class for
330             your test class. This means that if you try to use C<extends> in your test
331             class you will break things unless you include C<Test::Class::Moose> as a
332             parent. We recommend that you use roles in your test classes instead.
333              
334             =head2 Declare a test method
335              
336             All method names that begin with C<test_> are test methods. Methods that do
337             not are not test methods.
338              
339             sub test_this_is_a_method {
340             my $test = shift;
341              
342             $test->this_is_not_a_test_method;
343             ok 1, 'whee!';
344             }
345              
346             sub this_is_not_a_test_method {
347             my $test = shift;
348             # but you can, of course, call it like normal
349             }
350              
351             You may specify C<Test> and C<Tests> method attributes, just like in
352             L<Test::Class> and the method will automatically be a test method, even if
353             does not start with C<test_>:
354              
355             sub this_is_a_test : Test {
356             pass 'we have a single test';
357             }
358              
359             sub another_test_method : Tests { # like "no_plan"
360             # a bunch of tests
361             }
362              
363             sub yet_another_test_method : Tests(7) { # sets plan to 7 tests
364             ...
365             }
366              
367             B<Note>: Prior to version 0.51, this feature only worked if you had the
368             optional C<Sub::Attribute> installed.
369              
370             =head2 Plans
371              
372             No plans needed. The test suite declares a plan of the number of test classes.
373              
374             Each test class is a subtest declaring a plan of the number of test methods.
375              
376             Each test method relies on an implicit C<done_testing> call.
377              
378             If you prefer, you can declare a plan in a test method:
379              
380             sub test_something {
381             my $test = shift;
382             $test->test_report->plan($num_tests);
383             ...
384             }
385              
386             Or with a C<Tests> attribute:
387              
388             sub test_something : Tests(3) {
389             my $test = shift;
390             ...
391             }
392              
393             You may call C<plan()> multiple times for a given test method. Each call to
394             C<plan()> will add that number of tests to the plan. For example, with a
395             method modifier:
396              
397             before 'test_something' => sub {
398             my $test = shift;
399             $test->test_report->plan($num_extra_tests);
400              
401             # more tests
402             };
403              
404             Please note that if you call C<plan>, the plan will still show up at the end
405             of the subtest run, but you'll get the desired failure if the number of tests
406             run does not match the plan.
407              
408             =head2 Inheriting from another Test::Class::Moose class
409              
410             List it as the C<extends> in the import list. If the base class does not use
411             (or extend) Test::Class::Moose, then a compile-time error is thrown.
412              
413             package TestsFor::Some::Class::Subclass;
414             use Test::Class::Moose extends => 'TestsFor::Some::Class';
415              
416             sub test_me {
417             my $test = shift;
418             my $class = $test->test_class;
419             ok 1, "I overrode my parent! ($class)";
420             }
421              
422             before 'test_this_baby' => sub {
423             my $test = shift;
424             my $class = $test->test_class;
425             pass "This should run before my parent method ($class)";
426             };
427              
428             sub this_should_not_run {
429             my $test = shift;
430             fail "We should never see this test";
431             }
432              
433             sub test_this_should_be_run {
434             for ( 1 .. 5 ) {
435             pass "This is test number $_ in this method";
436             }
437             }
438              
439             1;
440              
441             =head2 Skipping Test::Most
442              
443             By default, when you C<use Test::Class::Moose> in your own test class, it
444             exports all the subs from L<Test::Most> into your class. If you'd prefer to
445             import a different set of test tools, you can pass C<< bare => 1 >> when using
446             C<Test::Class::Moose>:
447              
448             use Test::Class::Moose bare => 1;
449              
450             When you pass this, C<Test::Class::Moose> will not export L<Test::Most>'s subs
451             into your class. You will have to explicitly import something like
452             L<Test::More> or L<Test2::Tools::Compare> in order to actually perform tests.
453              
454             =head2 Custom Test Toolkits
455              
456             If you'd like to provide a custom set of test modules to all of your test
457             classes, this is easily done with L<Import::Into>:
458              
459             package MM::Test::Class::Moose;
460              
461             use strict;
462             use warnings;
463             use namespace::autoclean ();
464              
465             use Import::Into;
466             use Test::Class::Moose ();
467             use Test::Fatal;
468             use Test::More;
469              
470             sub import {
471             my @imports = qw(
472             Test::Class::Moose
473             namespace::autoclean
474             Test::Fatal
475             Test::More
476             );
477              
478             my $caller_level = 1;
479             $_->import::into($caller_level) for @imports;
480             }
481              
482             You could also create a kit in a separate module like C<My::Test::Kit> using
483             L<Test::Kit> and then simply export that from your C<My::Test::Class::Moose>
484             module with L<Import::Into>.
485              
486             =head1 TEST CONTROL METHODS
487              
488             The test control methods are there to ensure that your tests are running with
489             all of the resources they need. For example, database transactions might be
490             started before a test method and rolled back after the test method. Fixtures
491             needed for every test might be loaded and cleaned up. However you use them,
492             it's important to understand when and how they're run.
493              
494             =over 4
495              
496             =item 1. C<test_startup> — Runs once when the test class starts up
497              
498             =item 2. C<test_setup> - Runs before each test method
499              
500             =item 3. C<test_teardown> - Runs after each test method
501              
502             =item 4. C<test_shutdown> - Runs once when the test class shuts down
503              
504             =back
505              
506             B<Important>: Do not run tests in test control methods. This will cause the
507             test control method to fail (this is a feature, not a bug). If a test control
508             method fails, the class/method will fail and testing for that class should
509             stop.
510              
511             The available test control methods are:
512              
513             =head2 C<test_startup>
514              
515             sub test_startup {
516             my $test = shift;
517             $test->next::method; # run this before your test_startup code
518             # more startup
519             }
520              
521             Runs at the start of each test class. Quite often the base class that you
522             inherit from will have its own C<test_startup> code running (such as starting
523             a database transaction or connecting to an external resource). You almost
524             always want to call C<< $test->next::method >> I<before> your own setup code.
525             This ensures that the environment is set up to safely run your code. For
526             example, if the parent C<test_startup> starts a database transaction with the
527             expectation that the C<test_teardown> will end the database transactions, you
528             can safely load database fixtures I<after> that is run.
529              
530             If you need to know the name of the class you're running this in (though
531             usually you shouldn't), use C<< $test->test_class >>, or you can do this:
532              
533             sub test_startup {
534             my $test = shift;
535             my $report = $test->test_report;
536             my $instance = $report->current_instance->name;
537             my $upcoming_test_method = $report->current_method->name;
538             ...
539             }
540              
541             The C<< $test->test_report >> object is a L<Test::Class::Moose::Report::Instance>
542             object.
543              
544             =head2 C<test_setup>
545              
546             sub test_setup {
547             my $test = shift;
548             $test->next::method; # run this before your test_setup code
549             # more setup
550             }
551              
552             Runs at the start of each test method. If you must know the name of the test
553             you're about to run, you can do this:
554              
555             sub test_setup {
556             my $test = shift;
557             $test->next::method;
558             my $test_method = $test->test_report->current_method->name;
559             # do something with it
560             }
561              
562             =head2 C<test_teardown>
563              
564             sub test_teardown {
565             my $test = shift;
566             # more teardown
567             $test->next::method; # run this after your test_teardown code
568             }
569              
570             Runs at the end of each test method.
571              
572             By default, this is not run if the test class is skipped entirely. You can
573             override the C<run_control_methods_on_skip> in your class to return a true
574             value in order to force this method to be run when the class is skipped.
575              
576             =head2 C<test_shutdown>
577              
578             sub test_shutdown {
579             my $test = shift;
580             # more teardown
581             $test->next::method; # run this after your test_shutdown code
582             }
583              
584             Runs at the end of each test class.
585              
586             By default, this is not run if the test class is skipped entirely. You can
587             override the C<run_control_methods_on_skip> in your class to return a true
588             value in order to force this method to be run when the class is skipped.
589              
590             =head2 Overriding Test Control Methods
591              
592             To override a test control method, just remember that this is OO:
593              
594             sub test_setup {
595             my $test = shift;
596             $test->next::method; # call parent test_setup
597             # more setup code here
598             }
599              
600             =head1 TEST CLASS INSTANCES
601              
602             B<This feature is still considered experimental.>
603              
604             By default, each test class you create will be instantiated once. However, you
605             can tell the L<Test::Class::Moose::Runner> to create multiple instances of a
606             test class.
607              
608             To do this, simply consume the
609             L<Test::Class::Moose::Role::ParameterizedInstances> role in your test
610             class. This role requires you to implement a C<_constructor_parameter_sets>
611             method in your test class. That method will be called as a I<class method>. It
612             is expected to return a list of key/value pairs. The keys are the name of the
613             instance and the values are hashrefs of attributes to be passed to your test
614             class's constructor. Here's a really dumb example:
615              
616             package TestsFor::PlainAndFancy;
617             use Test::Class::Moose;
618             with 'Test::Class::Moose::Role::ParameterizedInstances';
619              
620             has is_fancy => (
621             is => 'ro',
622             isa => 'Bool',
623             required => 1,
624             );
625              
626             sub _constructor_parameter_sets {
627             my $class = shift;
628             return (
629             "$class - plain" => { is_fancy => 0 },
630             "$class - fancy" => { is_fancy => 1 },
631             );
632             }
633              
634             sub test_something { ... }
635              
636             The test runner will run all the test methods in your class I<once per
637             instance>, and each instance will be run in its own subtest. You can
638             dynamically decide to skip your test class completely by having
639             C<_constructor_parameter_sets> return an empty list.
640              
641             Note that this feature has great potential for abuse, so use it
642             cautiously. That said, there are cases where this feature can greatly simplify
643             your test code.
644              
645             =head1 RUNNING THE TEST SUITE
646              
647             See the docs for L<Test::Class::Moose::Runner> for details on running your
648             test suite. If you'd like to get up and running quickly, here's a very simple
649             test file you can use:
650              
651             use Test::Class::Moose::CLI;
652             Test::Class::Moose::CLI->new_with_options->run;
653              
654             Put this in a file like F<t/run-test-class.t>. When you run it with prove it
655             will load all the test classes defined in F<t/lib> and run them sequentially.
656              
657             See the documentation for L<Test::Class::Moose::CLI> on the options you can
658             pass when running tests.
659              
660             =head2 Skipping Classes and Methods
661              
662             If you wish to skip a class, set the reason in the C<test_startup> method.
663              
664             sub test_startup {
665             my $test = shift;
666             $test->test_skip("I don't want to run this class");
667             }
668              
669             If you are using L<test class instances|/"TEST CLASS INSTANCES">, you
670             can also make C<_constructor_parameter_sets> return an empty list,
671             which will result in the class being skipped.
672              
673             Note that if you run C<test_skip>, the C<test_shutdown> method will also be
674             skipped. This is due to the assumption that you might not have run any setup
675             code and thus you don't need shutdown code. However, if you do need to run
676             shutdown, you can override the C<run_control_methods_on_skip> method to return
677             true:
678              
679             sub run_control_methods_on_skip {1}
680              
681             If you wish to skip an individual method, do so in the C<test_setup> method.
682              
683             sub test_setup {
684             my $test = shift;
685             my $test_method = $test->test_report->current_method;
686              
687             if ( 'test_time_travel' eq $test_method->name ) {
688             $test->test_skip("Time travel not yet available");
689             }
690             }
691              
692             =head2 The "Tests" and "Test" Attributes
693              
694             If you're comfortable with L<Test::Class>, you know that test methods methods are
695             declared in L<Test::Class> with C<Test> (for a method with a single test) or
696             C<Tests>, for a method with multiple tests. This also works for
697             C<Test::Class::Moose>. Test methods declared this way do not need to start
698             with C<test_>.
699              
700             sub something_we_want_to_check : Test {
701             # this method may have only one test
702             }
703              
704             sub something_else_to_check : Tests {
705             # this method may have multiple tests
706             }
707              
708             sub another_test_method : Tests(3) {
709             # this method must have exactly 3 tests
710             }
711              
712             If a test method overrides a parent test method and calls it, their plans will
713             be added together:
714              
715             package TestsFor::Parent;
716              
717             use Test::Class::Moose;
718              
719             sub some_test : Tests(3) {
720             # three tests
721             }
722              
723             And later:
724              
725             package TestsFor::Child;
726              
727             use Test::Class::Moose extends => 'TestsFor::Parent';
728              
729             sub some_test : Tests(2) {
730             my $test = shift;
731             $test->next::method;
732             # 2 tests here
733             }
734              
735             In the above example, C<TestsFor::Parent::some_test> will run three tests, but
736             C<TestsFor::Child::some_test> will run I<five> tests (two tests, plus the
737             three from the parent).
738              
739             Note that if a plan is explicitly declared, any modifiers or overriding
740             methods calling the original method will also have to assert the number of
741             tests to ensure the plan is correct. The above C<TestsFor::Parent> and
742             C<TestsFor::Child> code would fail if the child's C<some_test> method
743             attribute was C<Tests> without the number of tests asserted.
744              
745             Do not use C<Test> or C<Tests> with test control methods because you don't run
746             tests in those.
747              
748             =head2 Tagging Methods
749              
750             Sometimes you want to be able to assign metadata to help you better manage
751             your test suite. You can do this with tags:
752              
753             sub test_save_poll_data : Tags(api network) {
754             ...
755             }
756              
757             Tags are strictly optional and you can provide one or more tags for each test
758             method with a space separated list of tags. You can use this to filter your
759             tests suite, if desired. For example, if your network goes down and all tests
760             which rely on a network are tagged with C<network>, you can skip those tests
761             with this:
762              
763             Test::Class::Moose::Runner->new( exclude_tags => 'network' )->runtests;
764              
765             Or maybe you want to run all C<api> and C<database> tests, but skip those
766             marked C<deprecated>:
767              
768             Test::Class::Moose::Runner->new(
769             include_tags => [qw/api database/],
770             exclude_tags => 'deprecated',
771             )->runtests;
772              
773             You can also inspect tags within your test classes:
774              
775             sub test_setup {
776             my $test = shift;
777             my $method_to_run = $test->test_report->current_method;
778             if ( $method_to_run->has_tag('db') ) {
779             $test->load_database_fixtures;
780             }
781             }
782              
783             Tagging support relies on L<Sub::Attribute>. If this module is not available,
784             C<include_tags> and C<exclude_tags> will be ignored, but a warning will be
785             issued if those are seen. Prior to version 0.51, C<Sub::Attribute> was
786             optional. Now it's mandatory, so those features should always work.
787              
788             =head1 THINGS YOU CAN OVERRIDE
789              
790             ... but probably shouldn't.
791              
792             As a general rule, methods beginning with C</^test_/> are reserved for
793             L<Test::Class::Moose>. This makes it easier to remember what you can and
794             cannot override. However, any test with C<Test> or C<Tests> are test methods
795             regardless of their names.
796              
797             =head2 C<test_report>
798              
799             my $report = $test->test_report;
800              
801             Returns the L<Test::Class::Moose::Report> object. Useful if you want to do
802             your own reporting and not rely on the default output provided with the
803             C<statistics> boolean option.
804              
805             You can also call it in test classes (most useful in the C<test_setup()> method):
806              
807             sub test_setup {
808             my $test = shift;
809             $self->next::method;
810             my $report = $test->test_report;
811             my $instance = $test->current_instance;
812             my $method = $test->current_method; # the test method we're about to run
813             if ( $method->name =~ /customer/ ) {
814             $test->load_customer_fixture;
815             }
816             # or better still
817             if ( $method->has_tag('customer') ) {
818             $test->load_customer_fixture;
819             }
820             }
821              
822             =head2 C<test_class>
823              
824             my $class = $test->test_class;
825              
826             Returns the name for this test class. Useful if you rebless an object (such as
827             applying a role at runtime) and don't want to lose the original class name.
828              
829             =head2 C<test_methods>
830              
831             You may override this in a subclass. Currently returns all methods in a test
832             class that start with C<test_> (except for the test control methods).
833              
834             Please note that the behavior for C<include> and C<exclude> is also contained
835             in this method. If you override it, you will need to account for those
836             yourself.
837              
838             =head2 C<import>
839              
840             Sadly, we have an C<import> method. This is used to automatically provide you
841             with all of the L<Test::Most> behavior.
842              
843             =head1 SAMPLE TAP OUTPUT
844              
845             We use nested tests (subtests) at each level:
846              
847             1..2
848             #
849             # Executing tests for TestsFor::Basic::Subclass
850             #
851             1..3
852             # TestsFor::Basic::Subclass->test_me()
853             ok 1 - I overrode my parent! (TestsFor::Basic::Subclass)
854             1..1
855             ok 1 - test_me
856             # TestsFor::Basic::Subclass->test_this_baby()
857             ok 1 - This should run before my parent method (TestsFor::Basic::Subclass)
858             ok 2 - whee! (TestsFor::Basic::Subclass)
859             1..2
860             ok 2 - test_this_baby
861             # TestsFor::Basic::Subclass->test_this_should_be_run()
862             ok 1 - This is test number 1 in this method
863             ok 2 - This is test number 2 in this method
864             ok 3 - This is test number 3 in this method
865             ok 4 - This is test number 4 in this method
866             ok 5 - This is test number 5 in this method
867             1..5
868             ok 3 - test_this_should_be_run
869             ok 1 - TestsFor::Basic::Subclass
870             #
871             # Executing tests for TestsFor::Basic
872             #
873             1..2
874             # TestsFor::Basic->test_me()
875             ok 1 - test_me() ran (TestsFor::Basic)
876             ok 2 - this is another test (TestsFor::Basic)
877             1..2
878             ok 1 - test_me
879             # TestsFor::Basic->test_this_baby()
880             ok 1 - whee! (TestsFor::Basic)
881             1..1
882             ok 2 - test_this_baby
883             ok 2 - TestsFor::Basic
884             # Test classes: 2
885             # Test methods: 5
886             # Total tests run: 11
887             ok
888             All tests successful.
889             Files=1, Tests=2, 2 wallclock secs ( 0.03 usr 0.00 sys + 0.27 cusr 0.01 csys = 0.31 CPU)
890             Result: PASS
891              
892             =head1 REPORTING
893              
894             See L<Test::Class::Moose::Report> for more detailed information on reporting.
895              
896             Reporting features are subject to change.
897              
898             Sometimes you want more information about your test classes, it's time to do
899             some reporting. Maybe you even want some tests for your reporting. If you do
900             that, run the test suite in a subtest (because the plans will otherwise be
901             wrong).
902              
903             #!/usr/bin/env perl
904             use lib 'lib';
905             use Test::Most;
906             use Test::Class::Moose::Load qw(t/lib);
907             use Test::Class::Moose::Runner;
908              
909             my $test_suite = Test::Class::Moose::Runner->new;
910              
911             subtest 'run the test suite' => sub {
912             $test_suite->runtests;
913             };
914             my $report = $test_suite->test_report;
915              
916             foreach my $class ( $report->all_test_instances ) {
917             my $class_name = $class->name;
918             ok !$class->is_skipped, "$class_name was not skipped";
919             ok $class->passed, "$class_name passed";
920              
921             subtest "$class_name methods" => sub {
922             foreach my $method ( $class->all_test_methods ) {
923             my $method_name = $method->name;
924             ok $method->passed, "$method_name passed";
925              
926             ok !$method->is_skipped, "$method_name was not skipped";
927             cmp_ok $method->num_tests, '>', 0,
928             '... and some tests should have been run';
929             diag "Run time for $method_name: ".$method->time->duration;
930             }
931             };
932             my $time = $class->time;
933             diag "Run time for $class_name: ".$class->time->duration;
934              
935             my $real = $time->real;
936             my $user = $time->user;
937             my $system = $time->system;
938             # do with these as you will
939             }
940             diag "Number of test classes: " . $report->num_test_classes;
941             diag "Number of test instances: " . $report->num_test_instances;
942             diag "Number of test methods: " . $report->num_test_methods;
943             diag "Number of tests: " . $report->num_tests;
944              
945             done_testing;
946              
947             If you just want to output reporting information, you do not need to run the
948             test suite in a subtest:
949              
950             my $test_suite = Test::Class::Moose::Runner->new->runtests;
951             my $report = $test_suite->test_report;
952             ...
953              
954             Or even shorter:
955              
956             my $report = Test::Class::Moose::Runner->new->runtests->test_report;
957              
958             =head1 EXTRAS
959              
960             If you would like L<Test::Class::Moose> to take care of loading your classes
961             for you, see L<Test::Class::Moose::Role::AutoUse> in this distribution.
962              
963             =head1 DEPRECATIONS AND BACKWARDS INCOMPATIBILITIES
964              
965             =head2 Version 0.79
966              
967             =over 4
968              
969             =item *
970              
971             The L<Test::Class::Moose::Config> class's C<args> method is now
972             deprecated. This was a holdover from when Test::Class::Moose was both a parent
973             class for your test classes and the test class runner.
974              
975             =back
976              
977             =head2 Version 0.77
978              
979             =over 4
980              
981             =item *
982              
983             The passing of the report object as an argument to test methods and test
984             control methods is now deprecated. You can get the report from the test class
985             object itself via the C<< $test->test_report >> method.
986              
987             =item *
988              
989             The C<< Test::Class::Moose->runtests >> method has been removed. Use
990             L<Test::Class::Moose::Runner> to run your test classes.
991              
992             =item *
993              
994             The C<Test::Class::Moose::Role::Parallel> role has been removed. This has not
995             done anything except issue a warning since version 0.55.
996              
997             =back
998              
999             =head2 Version 0.75
1000              
1001             =over 4
1002              
1003             =item *
1004              
1005             The C<test_teardown method> is no longer run when a test is skipped unless
1006             C<run_control_methods_on_skip> returns a true value. The C<test_teardown>
1007             method was never intended to be run unconditionally.
1008              
1009             =item *
1010              
1011             Parallel testing now parallelizes test classes rather than individual test
1012             instances. This is only relevant if your test suite contains parameterized
1013             test classes. This is slightly less efficient, but made the internal test
1014             running code much simpler and made it possible to fix reporting for parallel
1015             test runs.
1016              
1017             =item *
1018              
1019             The L<Test::Class::Moose::Config> C<builder> method has been removed.
1020              
1021             =item *
1022              
1023             The L<Test::Class::Moose::Runner> C<builder> method has been removed.
1024              
1025             =back
1026              
1027             =head2 Version 0.67
1028              
1029             =over 4
1030              
1031             =item * The L<Test::Class::Moose::Report> class's C<all_test_classes> method is un-deprecated
1032              
1033             This method now returns a list of L<Test::Class::Moose::Report::Class>
1034             objects. A class report contains one or more instance reports.
1035              
1036             =item *
1037              
1038             Removed the L<Test::Class::Moose::Report::Instance>'s error
1039             attribute. Contrary to the documentation, this attribute was never populated.
1040              
1041             =item *
1042              
1043             Renamed the L<Test::Class::Moose::Report::Method> C<instance_report> method to
1044             C<instance>. This is a better match for other report-related methods, which
1045             don't include a "_report" suffix.
1046              
1047             =item *
1048              
1049             Removed the long-deprecated C<tests_run> methods from
1050             L<Test::Class::Moose::Report> and L<Test::Class::Moose::Report::Method>.
1051              
1052             =item *
1053              
1054             Removed the long-deprecated C<<
1055             Test::Class::Moose::Report::Method->add_to_plan method >>.
1056              
1057             =back
1058              
1059             =head2 Version 0.55
1060              
1061             =over 4
1062              
1063             =item * Running tests with Test::Class::Moose is deprecated - use L<Test::Class::Moose::Runner>
1064              
1065             As of version 0.55, running tests and being a test class have been
1066             separated. Your test classes should continue to C<use Test::Class::Moose>, but
1067             your test runner script should use L<Test::Class::Moose::Runner>:
1068              
1069             use Test::Class::Moose::Load 't/lib';
1070             use Test::Class::Moose::Runner;
1071             Test::Class::Moose::Runner->new->runtests;
1072              
1073             Calling C<< Test::Class::Moose->new->runtests >> still works, but is
1074             deprecated and will issue a warning.
1075              
1076             =item * Parallel testing is totally different
1077              
1078             The C<Test::Class::Moose::Role::Parallel> role won't do anything other than
1079             issue a warning. See the L<Test::Class::Moose::Runner> docs for details on
1080             running tests in parallel.
1081              
1082             =item * The L<Test::Class::Moose::Report> C<all_test_classes> method is deprecated
1083              
1084             This has been replaced with the C<all_test_instances> method. The
1085             C<all_test_classes> method is still present for backwards compatibility, but
1086             it simply calls C<all_test_instances> under the hood.
1087              
1088             =item * The C<Test::Class::Moose::Report::Class> class is gone
1089              
1090             It has been replaced by the C<Test::Class::Moose::Report::Instance> class,
1091             which has the same API.
1092              
1093             =item * The C<Test::Class::Moose::Report::Method> C<class_report> method has been renamed
1094              
1095             This is now called C<instance_report>.
1096              
1097             =back
1098              
1099             =head2 Version 0.40
1100              
1101             =over 4
1102              
1103             =item * C<test_reporting>
1104              
1105             As of version 0.40, the long deprecated method C<test_reporting> has now been
1106             removed.
1107              
1108             =item * C<$report> argument to methods deprecated
1109              
1110             Prior to version 0.40, you used to have a second argument to all test methods
1111             and test control methods:
1112              
1113             sub test_something {
1114             my ( $test, $report ) = @_;
1115             ...
1116             }
1117              
1118             This was annoying. It was doubly annoying in test control methods in case you
1119             forgot it:
1120              
1121             sub test_setup {
1122             my ( $test, $report ) = @_;
1123             $test->next::method; # oops, needed $report
1124             ...
1125             }
1126              
1127             That second argument is still passed, but it's deprecated. It's now
1128             recommended that you call the C<< $test->test_report >> method to get that.
1129             Instead of this:
1130              
1131             sub test_froblinator {
1132             my ( $test, $report ) = @_;
1133             $report->plan(7);
1134             ...
1135             }
1136              
1137             You write this:
1138              
1139             sub test_froblinator {
1140             my $test = shift;
1141             $test->test_report->plan(7);
1142             ...
1143             }
1144              
1145             =back
1146              
1147             =head1 TODO
1148              
1149             =over 4
1150              
1151             =item * Callbacks for tags (for example, 'critical' tags could bailout)
1152              
1153             =item * New test phases - start and end suite, not just start and end class/method
1154              
1155             =back
1156              
1157             =head1 MORE INFO
1158              
1159             You can find documentation for this module with the perldoc command.
1160              
1161             perldoc Test::Class::Moose
1162              
1163             You can also look for information at:
1164              
1165             =over 4
1166              
1167             =item * CPAN Ratings
1168              
1169             L<http://cpanratings.perl.org/d/Test-Class-Moose>
1170              
1171             =item * MetaCPAN
1172              
1173             L<https://metacpan.org/release/Test-Class-Moose/>
1174              
1175             =back
1176              
1177             =head1 SEE ALSO
1178              
1179             =over 4
1180              
1181             =item * L<Test::Routine>
1182              
1183             I always pointed people to this when they would ask about L<Test::Class> +
1184             L<Moose>, but I would always hear "that's not quite what I'm looking for".
1185             I don't quite understand what the reasoning was, but I strongly encourage you
1186             to take a look at L<Test::Routine>.
1187              
1188             =item * L<Test::Roo>
1189              
1190             L<Test::Routine>, but with L<Moo> instead of L<Moose>.
1191              
1192             =item * L<Test::Class>
1193              
1194             xUnit-style testing in Perl.
1195              
1196             =item * L<Test::Class::Most>
1197              
1198             L<Test::Class> + L<Test::Most>.
1199              
1200             =back
1201              
1202             =head1 SUPPORT
1203              
1204             Bugs may be submitted at L<https://github.com/houseabsolute/test-class-moose/issues>.
1205              
1206             I am also usually active on IRC as 'autarch' on C<irc://irc.perl.org>.
1207              
1208             =head1 SOURCE
1209              
1210             The source code repository for Test-Class-Moose can be found at L<https://github.com/houseabsolute/test-class-moose>.
1211              
1212             =head1 AUTHORS
1213              
1214             =over 4
1215              
1216             =item *
1217              
1218             Curtis "Ovid" Poe <ovid@cpan.org>
1219              
1220             =item *
1221              
1222             Dave Rolsky <autarch@urth.org>
1223              
1224             =back
1225              
1226             =head1 CONTRIBUTORS
1227              
1228             =for stopwords Andy Jack Christopher Layne Chuck Adams Denny de la Haye Desmond Daignault Doug Bell Gregory Oschwald Harald Jörg Iain Loasby Jeremy Krieg Jonathan C. Otsuka Stowe jrubinator Karen Etheridge Larry Leszczynski mark-5 mephinet Neil Bowers Olaf Alders Paul Boyd Williams Petrea Corneliu Stefan Steven Humphrey Stuckdownawell Tim Vroom Tom Beresford Heady Udo Oji
1229              
1230             =over 4
1231              
1232             =item *
1233              
1234             Andy Jack <github@veracity.ca>
1235              
1236             =item *
1237              
1238             Christopher Layne <clayne@apple.com>
1239              
1240             =item *
1241              
1242             Chuck Adams <charles_adams@symantec.com>
1243              
1244             =item *
1245              
1246             Denny de la Haye <denny@users.noreply.github.com>
1247              
1248             =item *
1249              
1250             Desmond Daignault <nawglan@users.noreply.github.com>
1251              
1252             =item *
1253              
1254             Doug Bell <madcityzen@gmail.com>
1255              
1256             =item *
1257              
1258             Gregory Oschwald <goschwald@maxmind.com>
1259              
1260             =item *
1261              
1262             Harald Jörg <Harald.Joerg@arcor.de>
1263              
1264             =item *
1265              
1266             Iain Loasby <iain@dogwood-digital.com>
1267              
1268             =item *
1269              
1270             Jeremy Krieg <Jeremy.Krieg@YourAmigo.com>
1271              
1272             =item *
1273              
1274             Jonathan C. Otsuka <djgoku@gmail.com>
1275              
1276             =item *
1277              
1278             Jonathan Stowe <jns@gellyfish.co.uk>
1279              
1280             =item *
1281              
1282             jrubinator <jjrs.pam+github@gmail.com>
1283              
1284             =item *
1285              
1286             Karen Etheridge <ether@cpan.org>
1287              
1288             =item *
1289              
1290             Larry Leszczynski <larryl@cpan.org>
1291              
1292             =item *
1293              
1294             mark-5 <maflick88@gmail.com>
1295              
1296             =item *
1297              
1298             mephinet <mephinet@gmx.net>
1299              
1300             =item *
1301              
1302             Neil Bowers <neil@bowers.com>
1303              
1304             =item *
1305              
1306             Olaf Alders <olaf@wundersolutions.com>
1307              
1308             =item *
1309              
1310             Paul Boyd <pboyd@dev3l.net>
1311              
1312             =item *
1313              
1314             Paul Williams <kwakwaversal@gmail.com>
1315              
1316             =item *
1317              
1318             Petrea Corneliu Stefan <stefan@garage-coding.com>
1319              
1320             =item *
1321              
1322             Steven Humphrey <catchgithub@33k.co.uk>
1323              
1324             =item *
1325              
1326             Stuckdownawell <stuckdownawell@gmail.com>
1327              
1328             =item *
1329              
1330             Tim Vroom <vroom@blockstackers.com>
1331              
1332             =item *
1333              
1334             Tom Beresford <tom.beresford@bskyb.com>
1335              
1336             =item *
1337              
1338             Tom Heady <tom@punch.net>
1339              
1340             =item *
1341              
1342             Udo Oji <Velti@signor.com>
1343              
1344             =back
1345              
1346             =head1 COPYRIGHT AND LICENSE
1347              
1348             This software is copyright (c) 2012 - 2021 by Curtis "Ovid" Poe.
1349              
1350             This is free software; you can redistribute it and/or modify it under
1351             the same terms as the Perl 5 programming language system itself.
1352              
1353             The full text of the license can be found in the
1354             F<LICENSE> file included with this distribution.
1355              
1356             =cut