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   2155798 use strict;
  31         87  
  31         1649  
6 31     31   198 use warnings;
  31         60  
  31         1790  
7 31     31   1331 use namespace::autoclean;
  31         49581  
  31         296  
8              
9 31     31   3956 use 5.010000;
  31         142  
10              
11             our $VERSION = '1.00';
12              
13 31     31   19115 use Moose 2.0000;
  31         17098761  
  31         300  
14 31     31   305792 use Carp;
  31         72  
  31         3153  
15 31     31   23638 use Import::Into;
  31         24280  
  31         1315  
16 31     31   16763 use Sub::Attribute;
  31         100554  
  31         2997  
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   18740 use Test2 1.302118 ();
  31         5610  
  31         1140  
21              
22 31     31   18571 use Test::Class::Moose::AttributeRegistry;
  31         129  
  31         1518  
23 31     31   17259 use Test::Class::Moose::Config;
  31         493  
  31         1772  
24 31     31   349 use Test::Class::Moose::Deprecated;
  31         84  
  31         212  
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   14811 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 5099 eval __PACKAGE__->__sub_attr_declaration_code;
  31 50   31 1 292  
  31 50   31 0 81  
  31 50   31   303  
  31 100   23   18811  
  31 50   3   74  
  31 50   6   177  
  31 100   6   15800  
  31         65  
  31         181  
  23         35332  
  23         63  
  23         106  
  22         95  
  22         74  
  23         101  
  0         0  
  23         98  
  23         91  
  23         55  
  23         46  
  23         272  
  23         319  
  0         0  
  3         8821  
  3         18  
  0         0  
  3         9  
  3         13  
  3         22  
  1         23  
  2         23  
  2         12  
  2         112  
  2         100  
  6         2583  
  6         51  
  0         0  
  6         13  
  6         20  
  6         23  
  0         0  
  6         30  
  6         38  
  4         38  
  6         473  
  6         255  
115 31 50       33207 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   72361 shift;
145 168         725 my %args = @_;
146              
147 168         459 my $caller = caller;
148              
149 168         1080 my @imports = qw(
150             Moose
151             Sub::Attribute
152             strict
153             warnings
154             );
155              
156 168 100       806 unless ( $args{bare} ) {
157 24         7161 require Test::Most;
158 24         668021 push @imports, 'Test::Most';
159             }
160              
161 168         2780 $_->import::into($caller) for @imports;
162              
163 168 100 66     1885508 if ( my $parent = ( delete $args{parent} || delete $args{extends} ) ) {
164 35 100       259 my @parents = 'ARRAY' eq ref $parent ? @$parent : $parent;
165 35         262 $caller->meta->superclasses(@parents);
166 35 100       74208 unless ( $caller->isa(__PACKAGE__) ) {
167 3         81 croak( _bad_parents_error($caller) );
168             }
169             }
170             else {
171 133         848 $caller->meta->superclasses(__PACKAGE__);
172             }
173             }
174              
175             sub _bad_parents_error {
176 3     3   10 my $class = shift;
177              
178 3         17 my @ancestors = $class->meta->linearized_isa;
179              
180             # The first element is always $class
181 3         102 shift @ancestors;
182              
183 3         11 my $me = "The $class class";
184 3 100       13 if ( scalar @ancestors == 1 ) {
185 2         74 return "$me inherits from $ancestors[0] which does not inherit from "
186             . __PACKAGE__;
187             }
188              
189 1         4 my $heritage;
190 1 50       6 if ( scalar @ancestors == 2 ) {
191 0         0 $heritage .= join ' and ', @ancestors;
192             }
193             else {
194 1         4 my $final = pop @ancestors;
195 1         7 $heritage .= join ', ', @ancestors;
196 1         4 $heritage .= ", and $final";
197             }
198              
199             return
200 1         32 "$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   208 my $test_class = shift;
208              
209 40         2420 my $instance = $test_class->new(@_);
210 40         58685 $instance->_set_test_instance_name($test_class);
211              
212 40         235 return $instance;
213             }
214              
215             sub test_methods {
216 190     190 1 4686 my $self = shift;
217              
218 190         363 my @method_list;
219 190         1727 foreach my $method ( $self->meta->get_all_methods ) {
220              
221             # attributes cannot be test methods
222 7147 100       185536 next if $method->isa('Moose::Meta::Method::Accessor');
223              
224 5880         9923 my $class = ref $self;
225 5880         16191 my $name = $method->name;
226             next
227 5880 100 100     20524 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       9358 next if __PACKAGE__->can($name);
233 550         1511 push @method_list => $name;
234             }
235              
236 190         1536 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 120200 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 1.00
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
291             page|http://test-more.github.io/test-class-moose/> for 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 your
330             test class. This means that if you try to use C<extends> in your test class you
331             will break things unless you include C<Test::Class::Moose> as a parent. We
332             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 not
337             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 does
353             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 of
405             the subtest run, but you'll get the desired failure if the number of tests run
406             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 a
523             database transaction or connecting to an external resource). You almost always
524             want to call C<< $test->next::method >> I<before> your own setup code. This
525             ensures that the environment is set up to safely run your code. For example, if
526             the parent C<test_startup> starts a database transaction with the expectation
527             that the C<test_teardown> will end the database transactions, you can safely
528             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
542             L<Test::Class::Moose::Report::Instance> 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 class.
610             This role requires you to implement a C<_constructor_parameter_sets> method in
611             your test class. That method will be called as a I<class method>. It is
612             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 cautiously.
642             That said, there are cases where this feature can greatly simplify your test
643             code.
644              
645             =head1 RUNNING THE TEST SUITE
646              
647             See the docs for L<Test::Class::Moose::Runner> for details on running your test
648             suite. If you'd like to get up and running quickly, here's a very simple test
649             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 can also
670             make C<_constructor_parameter_sets> return an empty list, which will result in
671             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
695             are declared in L<Test::Class> with C<Test> (for a method with a single test)
696             or 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 with
698             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 three
737             from the parent).
738              
739             Note that if a plan is explicitly declared, any modifiers or overriding methods
740             calling the original method will also have to assert the number of tests to
741             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 attribute
743             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 your
751             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 cannot
794             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 your
802             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()>
806             method):
807              
808             sub test_setup {
809             my $test = shift;
810             $self->next::method;
811             my $report = $test->test_report;
812             my $instance = $test->current_instance;
813             my $method = $test->current_method; # the test method we're about to run
814             if ( $method->name =~ /customer/ ) {
815             $test->load_customer_fixture;
816             }
817             # or better still
818             if ( $method->has_tag('customer') ) {
819             $test->load_customer_fixture;
820             }
821             }
822              
823             =head2 C<test_class>
824              
825             my $class = $test->test_class;
826              
827             Returns the name for this test class. Useful if you rebless an object (such as
828             applying a role at runtime) and don't want to lose the original class name.
829              
830             =head2 C<test_methods>
831              
832             You may override this in a subclass. Currently returns all methods in a test
833             class that start with C<test_> (except for the test control methods).
834              
835             Please note that the behavior for C<include> and C<exclude> is also contained
836             in this method. If you override it, you will need to account for those
837             yourself.
838              
839             =head2 C<import>
840              
841             Sadly, we have an C<import> method. This is used to automatically provide you
842             with all of the L<Test::Most> behavior.
843              
844             =head1 SAMPLE TAP OUTPUT
845              
846             We use nested tests (subtests) at each level:
847              
848             1..2
849             #
850             # Executing tests for TestsFor::Basic::Subclass
851             #
852             1..3
853             # TestsFor::Basic::Subclass->test_me()
854             ok 1 - I overrode my parent! (TestsFor::Basic::Subclass)
855             1..1
856             ok 1 - test_me
857             # TestsFor::Basic::Subclass->test_this_baby()
858             ok 1 - This should run before my parent method (TestsFor::Basic::Subclass)
859             ok 2 - whee! (TestsFor::Basic::Subclass)
860             1..2
861             ok 2 - test_this_baby
862             # TestsFor::Basic::Subclass->test_this_should_be_run()
863             ok 1 - This is test number 1 in this method
864             ok 2 - This is test number 2 in this method
865             ok 3 - This is test number 3 in this method
866             ok 4 - This is test number 4 in this method
867             ok 5 - This is test number 5 in this method
868             1..5
869             ok 3 - test_this_should_be_run
870             ok 1 - TestsFor::Basic::Subclass
871             #
872             # Executing tests for TestsFor::Basic
873             #
874             1..2
875             # TestsFor::Basic->test_me()
876             ok 1 - test_me() ran (TestsFor::Basic)
877             ok 2 - this is another test (TestsFor::Basic)
878             1..2
879             ok 1 - test_me
880             # TestsFor::Basic->test_this_baby()
881             ok 1 - whee! (TestsFor::Basic)
882             1..1
883             ok 2 - test_this_baby
884             ok 2 - TestsFor::Basic
885             # Test classes: 2
886             # Test methods: 5
887             # Total tests run: 11
888             ok
889             All tests successful.
890             Files=1, Tests=2, 2 wallclock secs ( 0.03 usr 0.00 sys + 0.27 cusr 0.01 csys = 0.31 CPU)
891             Result: PASS
892              
893             =head1 REPORTING
894              
895             See L<Test::Class::Moose::Report> for more detailed information on reporting.
896              
897             Reporting features are subject to change.
898              
899             Sometimes you want more information about your test classes, it's time to do
900             some reporting. Maybe you even want some tests for your reporting. If you do
901             that, run the test suite in a subtest (because the plans will otherwise be
902             wrong).
903              
904             #!/usr/bin/env perl
905             use lib 'lib';
906             use Test::Most;
907             use Test::Class::Moose::Load qw(t/lib);
908             use Test::Class::Moose::Runner;
909              
910             my $test_suite = Test::Class::Moose::Runner->new;
911              
912             subtest 'run the test suite' => sub {
913             $test_suite->runtests;
914             };
915             my $report = $test_suite->test_report;
916              
917             foreach my $class ( $report->all_test_instances ) {
918             my $class_name = $class->name;
919             ok !$class->is_skipped, "$class_name was not skipped";
920             ok $class->passed, "$class_name passed";
921              
922             subtest "$class_name methods" => sub {
923             foreach my $method ( $class->all_test_methods ) {
924             my $method_name = $method->name;
925             ok $method->passed, "$method_name passed";
926              
927             ok !$method->is_skipped, "$method_name was not skipped";
928             cmp_ok $method->num_tests, '>', 0,
929             '... and some tests should have been run';
930             diag "Run time for $method_name: ".$method->time->duration;
931             }
932             };
933             my $time = $class->time;
934             diag "Run time for $class_name: ".$class->time->duration;
935              
936             my $real = $time->real;
937             my $user = $time->user;
938             my $system = $time->system;
939             # do with these as you will
940             }
941             diag "Number of test classes: " . $report->num_test_classes;
942             diag "Number of test instances: " . $report->num_test_instances;
943             diag "Number of test methods: " . $report->num_test_methods;
944             diag "Number of tests: " . $report->num_tests;
945              
946             done_testing;
947              
948             If you just want to output reporting information, you do not need to run the
949             test suite in a subtest:
950              
951             my $test_suite = Test::Class::Moose::Runner->new->runtests;
952             my $report = $test_suite->test_report;
953             ...
954              
955             Or even shorter:
956              
957             my $report = Test::Class::Moose::Runner->new->runtests->test_report;
958              
959             =head1 EXTRAS
960              
961             If you would like L<Test::Class::Moose> to take care of loading your classes
962             for you, see L<Test::Class::Moose::Role::AutoUse> in this distribution.
963              
964             =head1 DEPRECATIONS AND BACKWARDS INCOMPATIBILITIES
965              
966             =head2 Version 0.79
967              
968             =over 4
969              
970             =item *
971              
972             The L<Test::Class::Moose::Config> class's C<args> method is now deprecated.
973             This was a holdover from when Test::Class::Moose was both a parent class for
974             your test classes and the test class runner.
975              
976             =back
977              
978             =head2 Version 0.77
979              
980             =over 4
981              
982             =item *
983              
984             The passing of the report object as an argument to test methods and test
985             control methods is now deprecated. You can get the report from the test class
986             object itself via the C<< $test->test_report >> method.
987              
988             =item *
989              
990             The C<< Test::Class::Moose->runtests >> method has been removed. Use
991             L<Test::Class::Moose::Runner> to run your test classes.
992              
993             =item *
994              
995             The C<Test::Class::Moose::Role::Parallel> role has been removed. This has not
996             done anything except issue a warning since version 0.55.
997              
998             =back
999              
1000             =head2 Version 0.75
1001              
1002             =over 4
1003              
1004             =item *
1005              
1006             The C<test_teardown method> is no longer run when a test is skipped unless
1007             C<run_control_methods_on_skip> returns a true value. The C<test_teardown>
1008             method was never intended to be run unconditionally.
1009              
1010             =item *
1011              
1012             Parallel testing now parallelizes test classes rather than individual test
1013             instances. This is only relevant if your test suite contains parameterized test
1014             classes. This is slightly less efficient, but made the internal test running
1015             code much simpler and made it possible to fix reporting for parallel 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> objects.
1034             A class report contains one or more instance reports.
1035              
1036             =item *
1037              
1038             Removed the L<Test::Class::Moose::Report::Instance>'s error attribute. Contrary
1039             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<< Test::Class::Moose::Report::Method->add_to_plan
1055             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 separated.
1066             Your test classes should continue to C<use Test::Class::Moose>, but your test
1067             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 deprecated
1074             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 it
1086             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 recommended
1128             that you call the C<< $test->test_report >> method to get that. Instead of
1129             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". I
1185             don't quite understand what the reasoning was, but I strongly encourage you to
1186             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/Test-More/test-class-moose/issues>.
1205              
1206             =head1 SOURCE
1207              
1208             The source code repository for Test-Class-Moose can be found at L<https://github.com/Test-More/test-class-moose>.
1209              
1210             =head1 AUTHORS
1211              
1212             =over 4
1213              
1214             =item *
1215              
1216             Curtis "Ovid" Poe <ovid@cpan.org>
1217              
1218             =item *
1219              
1220             Dave Rolsky <autarch@urth.org>
1221              
1222             =item *
1223              
1224             Chad Granum <exodist@cpan.org>
1225              
1226             =back
1227              
1228             =head1 CONTRIBUTORS
1229              
1230             =for stopwords Andy Jack Chad Granum 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 Mike Jones Neil Bowers Olaf Alders Paul Boyd Williams Petrea Corneliu Stefan Steven Humphrey Stuckdownawell Tim Vroom Tom Beresford Heady Udo Oji
1231              
1232             =over 4
1233              
1234             =item *
1235              
1236             Andy Jack <github@veracity.ca>
1237              
1238             =item *
1239              
1240             Chad Granum <exodist7@gmail.com>
1241              
1242             =item *
1243              
1244             Christopher Layne <clayne@apple.com>
1245              
1246             =item *
1247              
1248             Chuck Adams <charles_adams@symantec.com>
1249              
1250             =item *
1251              
1252             Denny de la Haye <denny@users.noreply.github.com>
1253              
1254             =item *
1255              
1256             Desmond Daignault <nawglan@users.noreply.github.com>
1257              
1258             =item *
1259              
1260             Doug Bell <madcityzen@gmail.com>
1261              
1262             =item *
1263              
1264             Gregory Oschwald <goschwald@maxmind.com>
1265              
1266             =item *
1267              
1268             Harald Jörg <Harald.Joerg@arcor.de>
1269              
1270             =item *
1271              
1272             Iain Loasby <iain@dogwood-digital.com>
1273              
1274             =item *
1275              
1276             Jeremy Krieg <Jeremy.Krieg@YourAmigo.com>
1277              
1278             =item *
1279              
1280             Jonathan C. Otsuka <djgoku@gmail.com>
1281              
1282             =item *
1283              
1284             Jonathan Stowe <jns@gellyfish.co.uk>
1285              
1286             =item *
1287              
1288             jrubinator <jjrs.pam+github@gmail.com>
1289              
1290             =item *
1291              
1292             Karen Etheridge <ether@cpan.org>
1293              
1294             =item *
1295              
1296             Larry Leszczynski <larryl@cpan.org>
1297              
1298             =item *
1299              
1300             mark-5 <maflick88@gmail.com>
1301              
1302             =item *
1303              
1304             mephinet <mephinet@gmx.net>
1305              
1306             =item *
1307              
1308             Mike Jones <mike@netsplit.org.uk>
1309              
1310             =item *
1311              
1312             Neil Bowers <neil@bowers.com>
1313              
1314             =item *
1315              
1316             Olaf Alders <olaf@wundersolutions.com>
1317              
1318             =item *
1319              
1320             Paul Boyd <pboyd@dev3l.net>
1321              
1322             =item *
1323              
1324             Paul Williams <kwakwaversal@gmail.com>
1325              
1326             =item *
1327              
1328             Petrea Corneliu Stefan <stefan@garage-coding.com>
1329              
1330             =item *
1331              
1332             Steven Humphrey <catchgithub@33k.co.uk>
1333              
1334             =item *
1335              
1336             Stuckdownawell <stuckdownawell@gmail.com>
1337              
1338             =item *
1339              
1340             Tim Vroom <vroom@blockstackers.com>
1341              
1342             =item *
1343              
1344             Tom Beresford <tom.beresford@bskyb.com>
1345              
1346             =item *
1347              
1348             Tom Heady <tom@punch.net>
1349              
1350             =item *
1351              
1352             Udo Oji <Velti@signor.com>
1353              
1354             =back
1355              
1356             =head1 COPYRIGHT AND LICENSE
1357              
1358             This software is copyright (c) 2012 - 2025 by Curtis "Ovid" Poe.
1359              
1360             This is free software; you can redistribute it and/or modify it under
1361             the same terms as the Perl 5 programming language system itself.
1362              
1363             The full text of the license can be found in the
1364             F<LICENSE> file included with this distribution.
1365              
1366             =cut