forked from houseabsolute/test-class-moose
/
Moose.pm
1006 lines (735 loc) · 27.2 KB
/
Moose.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package Test::Class::Moose;
# ABSTRACT: Serious testing for serious Perl
use 5.10.0;
our $VERSION = '0.67';
use Moose 2.0000;
use Carp;
use namespace::autoclean;
use Sub::Attribute;
use Test::Class::Moose::AttributeRegistry;
use Test::Class::Moose::Config;
use Test::Class::Moose::Deprecated;
sub __sub_attr_declaration_code {
# XXX sharing this behavior here because my first attempt at creating a
# role was a complete failure. MooseX::MethodAttributes can help here, but
# I have to parse the attributes manually (as far as I can tell) and I
# don't have the simple declarative style any more.
return <<'DECLARE_ATTRIBUTES';
sub Tags : ATTR_SUB {
my ( $class, $symbol, undef, undef, $data, undef, $file, $line ) = @_;
my @tags;
if ($data) {
$data =~ s/^\s+//g;
@tags = split /\s+/, $data;
}
if ( $symbol eq 'ANON' ) {
die "Cannot tag anonymous subs at file $file, line $line\n";
}
my $method = *{$symbol}{NAME};
{ # block for localising $@
local $@;
Test::Class::Moose::AttributeRegistry->add_tags(
$class,
$method,
\@tags,
);
if ($@) {
croak "Error in adding tags: $@";
}
}
}
sub Test : ATTR_SUB {
my ( $class, $symbol, undef, undef, undef, undef, $file, $line ) = @_;
if ( $symbol eq 'ANON' ) {
croak("Cannot add plans to anonymous subs at file $file, line $line");
}
my $method = *{$symbol}{NAME};
if ( $method =~ /^test_(?:startup|setup|teardown|shutdown)$/ ) {
croak("Test control method '$method' may not have a Test attribute");
}
Test::Class::Moose::AttributeRegistry->add_plan(
$class,
$method,
1,
);
$class->meta->add_before_method_modifier($method, sub {
my $test = shift;
$test->test_report->plan(1);
});
}
sub Tests : ATTR_SUB {
my ( $class, $symbol, undef, undef, $data, undef, $file, $line ) = @_;
if ( $symbol eq 'ANON' ) {
croak("Cannot add plans to anonymous subs at file $file, line $line");
}
my $method = *{$symbol}{NAME};
if ( $method =~ /^test_(?:startup|setup|teardown|shutdown)$/ ) {
croak("Test control method '$method' may not have a Test attribute");
}
Test::Class::Moose::AttributeRegistry->add_plan(
$class,
$method,
$data,
);
if ( defined $data ) {
$class->meta->add_before_method_modifier($method, sub {
my $test = shift;
$test->test_report->plan($data);
});
}
}
DECLARE_ATTRIBUTES
}
BEGIN {
eval __PACKAGE__->__sub_attr_declaration_code;
croak($@) if $@;
}
has 'test_report' => (
is => 'ro',
isa => 'Test::Class::Moose::Report',
);
has 'test_class' => (
is => 'ro',
isa => 'Str',
init_arg => undef,
default => sub { ref $_[0] },
);
has 'test_skip' => (
is => 'rw',
isa => 'Str',
clearer => 'test_skip_clear',
);
has '_config_p' => (
is => 'ro',
isa => 'HashRef',
);
sub import {
my ( $class, %arg_for ) = @_;
my $caller = caller;
my $preamble = <<"END";
package $caller;
use Moose;
use Test::Most;
use Sub::Attribute;
END
eval $preamble;
croak($@) if $@;
strict->import;
warnings->import;
if ( my $parent
= ( delete $arg_for{parent} || delete $arg_for{extends} ) )
{
my @parents = 'ARRAY' eq ref $parent ? @$parent : $parent;
$caller->meta->superclasses(@parents);
}
else {
$caller->meta->superclasses(__PACKAGE__);
}
}
# XXX - this is only necessary for backwards compatibility, where people call
# Test::Class::Moose->new(...)->runtests() instead of
# Test::Class::Moose::Runner->new(...)->runtests()
my %config_attrs = map { $_->init_arg => 1 }
Test::Class::Moose::Config->meta->get_all_attributes;
around 'BUILDARGS' => sub {
my $orig = shift;
my $class = shift;
my $p = $class->$orig(@_);
my %config_p
= map { $_ => delete $p->{$_} } grep { $config_attrs{$_} } keys %{$p};
$p->{_config_p} = \%config_p;
return $p;
};
# XXX - also for backwards compat
sub runtests {
my $self = shift;
Test::Class::Moose::Deprecated::deprecated(
message =>
'Calling runtests() on a Test::Class::Moose object is deprecated.'
. ' Use Test::Class::Moose::Runner instead.',
feature => 'Test::Class::Moose->runtests',
);
require Test::Class::Moose::Runner;
my $runner = Test::Class::Moose::Runner->new( $self->_config_p );
return $runner->runtests();
}
sub _tcm_make_test_class_instances {
my $test_class = shift;
return ( $test_class => $test_class->new(@_) );
}
sub test_methods {
my $self = shift;
my @method_list;
foreach my $method ( $self->meta->get_all_methods ) {
# attributes cannot be test methods
next if $method->isa('Moose::Meta::Method::Accessor');
my $class = ref $self;
my $name = $method->name;
next
unless $name =~ /^test_/
|| Test::Class::Moose::AttributeRegistry->has_test_attribute(
$class, $name );
# don't use anything defined in this package
next if __PACKAGE__->can($name);
push @method_list => $name;
}
return @method_list;
}
# empty stub methods guarantee that subclasses can always call these
sub test_startup { }
sub test_setup { }
sub test_teardown { }
sub test_shutdown { }
__PACKAGE__->meta->make_immutable;
1;
__END__
=for Pod::Coverage Tags Tests runtests
=head1 SYNOPSIS
package TestsFor::DateTime;
use Test::Class::Moose;
use DateTime;
# methods that begin with test_ are test methods.
sub test_constructor {
my $test = shift;
$test->test_report->plan(3); # strictly optional
can_ok 'DateTime', 'new';
my %args = (
year => 1967,
month => 6,
day => 20,
);
isa_ok my $date = DateTime->new(%args), 'DateTime';
is $date->year, $args{year}, '... and the year should be correct';
}
1;
=head1 DESCRIPTION
See the L<Test::Class::Moose home page|http://test-class-moose.github.io/test-class-moose/> for
a summary.
C<Test::Class::Moose> is a powerful testing framework for Perl. Out of the box
you get:
=over 4
=item * Reporting
=item * Extensibility
=item * Tagging tests
=item * Parallel testing
=item * Test inheritance
=item * Write your tests using Moose
=item * All the testing functions and behavior from Test::Most
=item * Event handlers for startup, setup, teardown, and shutdown of test classes
=back
Better docs will come later. You should already know how to use Moose and
L<Test::Class>.
=head1 BASICS
=head2 Inheriting from Test::Class::Moose
Just C<use Test::Class::Moose>. That's all. You'll get all L<Test::Most> test
functions, too, along with C<strict> and C<warnings>. You can use all L<Moose>
behavior, too.
When you C<use Test::Class::Moose> it inserts itself as a parent class for
your test class. This means that if you try to use C<extends> in your test
class you will break things unless you include C<Test::Class::Moose> as a
parent. We recommend that you use roles in your test classes instead.
=head2 Declare a test method
All method names that begin with C<test_> are test methods. Methods that do
not are not test methods.
sub test_this_is_a_method {
my $test = shift;
$test->this_is_not_a_test_method;
ok 1, 'whee!';
}
sub this_is_not_a_test_method {
my $test = shift;
# but you can, of course, call it like normal
}
You may specify C<Test> and C<Tests> method attributes, just like in
L<Test::Class> and the method will automatically be a test method, even if
does not start with C<test_>:
sub this_is_a_test : Test {
pass 'we have a single test';
}
sub another_test_method : Tests { # like "no_plan"
# a bunch of tests
}
sub yet_another_test_method : Tests(7) { # sets plan to 7 tests
...
}
B<Note>: Prior to version 0.51, this feature only worked if you had the
optional C<Sub::Attribute> installed.
=head2 Plans
No plans needed. The test suite declares a plan of the number of test classes.
Each test class is a subtest declaring a plan of the number of test methods.
Each test method relies on an implicit C<done_testing> call.
If you prefer, you can declare a plan in a test method:
sub test_something {
my $test = shift;
$test->test_report->plan($num_tests);
...
}
Or with a C<Tests> attribute:
sub test_something : Tests(3) {
my $test = shift;
...
}
You may call C<plan()> multiple times for a given test method. Each call to
C<plan()> will add that number of tests to the plan. For example, with a
method modifier:
before 'test_something' => sub {
my $test = shift;
$test->test_report->plan($num_extra_tests);
# more tests
};
Please note that if you call C<plan>, the plan will still show up at the end
of the subtest run, but you'll get the desired failure if the number of tests
run does not match the plan.
=head2 Inheriting from another Test::Class::Moose class
List it as the C<extends> in the import list.
package TestsFor::Some::Class::Subclass;
use Test::Class::Moose extends => 'TestsFor::Some::Class';
sub test_me {
my $test = shift;
my $class = $test->test_class;
ok 1, "I overrode my parent! ($class)";
}
before 'test_this_baby' => sub {
my $test = shift;
my $class = $test->test_class;
pass "This should run before my parent method ($class)";
};
sub this_should_not_run {
my $test = shift;
fail "We should never see this test";
}
sub test_this_should_be_run {
for ( 1 .. 5 ) {
pass "This is test number $_ in this method";
}
}
1;
=head1 TEST CONTROL METHODS
Do not run tests in test control methods. This will cause the test control
method to fail (this is a feature, not a bug). If a test control method
fails, the class/method will fail and testing for that class should stop.
B<Every> test control method will be called as a method. The invocant is the
instance of your test class
The available test control methods are:
=head2 C<test_startup>
sub test_startup {
my $test = shift;
$test->next::method;
# more startup
}
Runs at the start of each test class. If you need to know the name of the
class you're running this in (though usually you shouldn't), use
C<< $test->test_class >>, or you can do this:
sub test_startup {
my $test = shift;
my $report = $test->test_report;
my $instance = $report->current_instance->name;
my $upcoming_test_method = $report->current_method->name;
...
}
The C<< $test->test_report >> object is a L<Test::Class::Moose::Report::Instance>
object.
=head2 C<test_setup>
sub test_setup {
my $test = shift;
$test->next::method;
# more setup
}
Runs at the start of each test method. If you must know the name of the test
you're about to run, you can do this:
sub test_setup {
my $test = shift;
$test->next::method;
my $test_method = $test->test_report->current_method->name;
# do something with it
}
=head2 C<test_teardown>
sub test_teardown {
my $test = shift;
# more teardown
$test->next::method;
}
Runs at the end of each test method.
=head2 C<test_shutdown>
sub test_shutdown {
my $test = shift;
# more teardown
$test->next::method;
}
Runs at the end of each test class.
=head2 Overriding Test Control Methods
To override a test control method, just remember that this is OO:
sub test_setup {
my $test = shift;
$test->next::method; # optional to call parent test_setup
# more setup code here
}
=head1 TEST CLASS INSTANCES
B<This feature is still considered experimental.>
By default, each test class you create will be instantiated once. However, you
can tell the L<Test::Class::Moose::Runner> to create multiple instances of a
test class.
To do this, simply consume the
L<Test::Class::Moose::Role::ParameterizedInstances> role in your test
class. This role requires you to implement a C<_constructor_parameter_sets>
method in your test class. That method will be called as a I<class method>. It
is expected to return a list of key/value pairs. The keys are the name of the
instance and the values are hashrefs of attributes to be passed to your test
class's constructor. Here's a really dumb example:
package TestsFor::PlainAndFancy;
use Test::Class::Moose;
with 'Test::Class::Moose::Role::ParameterizedInstances';
has is_fancy => (
is => 'ro',
isa => 'Bool',
required => 1,
);
sub _constructor_parameter_sets {
my $class = shift;
return (
"$class - plain" => { is_fancy => 0 },
"$class - fancy" => { is_fancy => 1 },
);
}
sub test_something { ... }
The test runner will run all the test methods in your class I<once per
instance>, and each instance will be run in its own subtest. You can
dynamically decide to skip your test class completely by having
C<_constructor_parameter_sets> return an empty list.
Note that this feature has great potential for abuse, so use it
cautiously. That said, there are cases where this feature can greatly simplify
your test code.
=head1 RUNNING THE TEST SUITE
See the docs for L<Test::Class::Moose::Runner> for details on running your
test suite. If you'd like to get up and running quickly, here's a very simple
test file you can use:
use Test::Class::Moose::Load 't/lib';
use Test::Class::Moose::Runner;
Test::Class::Moose::Runner->new->runtests;
Put this in a file like F<t/run-test-class.t>. When you run it with prove it
will load all the test classes defined in F<t/lib> and run them sequentially.
=head2 Skipping Classes and Methods
If you wish to skip a class, set the reason in the C<test_startup> method.
sub test_startup {
my $test = shift;
$test->test_skip("I don't want to run this class");
}
If you are using L<test class instances|/"TEST CLASS INSTANCES">, you
can also make C<_constructor_parameter_sets> return an empty list,
which will result in the class being skipped.
If you wish to skip an individual method, do so in the C<test_setup> method.
sub test_setup {
my $test = shift;
my $test_method = $test->test_report->current_method;
if ( 'test_time_travel' eq $test_method->name ) {
$test->test_skip("Time travel not yet available");
}
}
=head2 The "Tests" and "Test" Attributes
If you're comfortable with L<Test::Class>, you know that test methods methods are
declared in L<Test::Class> with C<Test> (for a method with a single test) or
C<Tests>, for a method with multiple tests. This also works for
C<Test::Class::Moose>. Test methods declared this way do not need to start
with C<test_>.
sub something_we_want_to_check : Test {
# this method may have only one test
}
sub something_else_to_check : Tests {
# this method may have multiple tests
}
sub another_test_method : Tests(3) {
# this method must have exactly 3 tests
}
If a test method overrides a parent test method and calls it, their plans will
be added together:
package TestsFor::Parent;
use Test::Class::Moose;
sub some_test : Tests(3) {
# three tests
}
And later:
package TestsFor::Child;
use Test::Class::Moose extends => 'TestsFor::Parent';
sub some_test : Tests(2) {
my $test = shift;
$test->next::method;
# 2 tests here
}
In the above example, C<TestsFor::Parent::some_test> will run three tests, but
C<TestsFor::Child::some_test> will run I<five> tests (two tests, plus the
three from the parent).
Note that if a plan is explicitly declared, any modifiers or overriding
methods calling the original method will also have to assert the number of
tests to ensure the plan is correct. The above C<TestsFor::Parent> and
C<TestsFor::Child> code would fail if the child's C<some_test> method
attribute was C<Tests> without the number of tests asserted.
Do not use C<Test> or C<Tests> with test control methods because you don't run
tests in those.
=head2 Tagging Methods
Sometimes you want to be able to assign metadata to help you better manage
your test suite. You can do this with tags:
sub test_save_poll_data : Tags(api network) {
...
}
Tags are strictly optional and you can provide one or more tags for each test
method with a space separated list of tags. You can use this to filter your
tests suite, if desired. For example, if your network goes down and all tests
which rely on a network are tagged with C<network>, you can skip those tests
with this:
Test::Class::Moose::Runner->new( exclude_tags => 'network' )->runtests;
Or maybe you want to run all C<api> and C<database> tests, but skip those
marked C<deprecated>:
Test::Class::Moose::Runner->new(
include_tags => [qw/api database/],
exclude_tags => 'deprecated',
)->runtests;
You can also inspect tags withing your test classes:
sub test_setup {
my $test = shift;
my $method_to_run = $test->test_report->current_method;
if ( $method_to_run->has_tag('db') ) {
$test->load_database_fixtures;
}
}
Tagging support relies on L<Sub::Attribute>. If this module is not available,
C<include_tags> and C<exclude_tags> will be ignored, but a warning will be
issued if those are seen. Prior to version 0.51, C<Sub::Attribute> was
optional. Now it's mandatory, so those features should always work.
=head1 THINGS YOU CAN OVERRIDE
... but probably shouldn't.
As a general rule, methods beginning with C</^test_/> are reserved for
L<Test::Class::Moose>. This makes it easier to remember what you can and
cannot override. However, any test with C<Test> or C<Tests> are test methods
regardless of their names.
=head2 C<test_report>
my $report = $test->test_report;
Returns the L<Test::Class::Moose::Report> object. Useful if you want to do
your own reporting and not rely on the default output provided with the
C<statistics> boolean option.
You can also call it in test classes (most useful in the C<test_setup()> method):
sub test_setup {
my $test = shift;
$self->next::method;
my $report = $test->test_report;
my $instance = $test->current_instance;
my $method = $test->current_method; # the test method we're about to run
if ( $method->name =~ /customer/ ) {
$test->load_customer_fixture;
}
# or better still
if ( $method->has_tag('customer') ) {
$test->load_customer_fixture;
}
}
=head2 C<test_class>
my $class = $test->test_class;
Returns the name for this test class. Useful if you rebless an object (such as
applying a role at runtime) and don't want to lose the original class name.
=head2 C<test_methods>
You may override this in a subclass. Currently returns all methods in a test
class that start with C<test_> (except for the test control methods).
Please note that the behavior for C<include> and C<exclude> is also contained
in this method. If you override it, you will need to account for those
yourself.
=head2 C<import>
Sadly, we have an C<import> method. This is used to automatically provide you
with all of the L<Test::Most> behavior.
=head1 SAMPLE TAP OUTPUT
We use nested tests (subtests) at each level:
1..2
#
# Executing tests for TestsFor::Basic::Subclass
#
1..3
# TestsFor::Basic::Subclass->test_me()
ok 1 - I overrode my parent! (TestsFor::Basic::Subclass)
1..1
ok 1 - test_me
# TestsFor::Basic::Subclass->test_this_baby()
ok 1 - This should run before my parent method (TestsFor::Basic::Subclass)
ok 2 - whee! (TestsFor::Basic::Subclass)
1..2
ok 2 - test_this_baby
# TestsFor::Basic::Subclass->test_this_should_be_run()
ok 1 - This is test number 1 in this method
ok 2 - This is test number 2 in this method
ok 3 - This is test number 3 in this method
ok 4 - This is test number 4 in this method
ok 5 - This is test number 5 in this method
1..5
ok 3 - test_this_should_be_run
ok 1 - TestsFor::Basic::Subclass
#
# Executing tests for TestsFor::Basic
#
1..2
# TestsFor::Basic->test_me()
ok 1 - test_me() ran (TestsFor::Basic)
ok 2 - this is another test (TestsFor::Basic)
1..2
ok 1 - test_me
# TestsFor::Basic->test_this_baby()
ok 1 - whee! (TestsFor::Basic)
1..1
ok 2 - test_this_baby
ok 2 - TestsFor::Basic
# Test classes: 2
# Test methods: 5
# Total tests run: 11
ok
All tests successful.
Files=1, Tests=2, 2 wallclock secs ( 0.03 usr 0.00 sys + 0.27 cusr 0.01 csys = 0.31 CPU)
Result: PASS
=head1 REPORTING
See L<Test::Class::Moose::Report> for more detailed information on reporting.
Reporting features are subject to change.
Sometimes you want more information about your test classes, it's time to do
some reporting. Maybe you even want some tests for your reporting. If you do
that, run the test suite in a subtest (because the plans will otherwise be
wrong).
#!/usr/bin/env perl
use lib 'lib';
use Test::Most;
use Test::Class::Moose::Load qw(t/lib);
use Test::Class::Moose::Runner;
my $test_suite = Test::Class::Moose::Runner->new;
subtest 'run the test suite' => sub {
$test_suite->runtests;
};
my $report = $test_suite->test_report;
foreach my $class ( $report->all_test_instances ) {
my $class_name = $class->name;
ok !$class->is_skipped, "$class_name was not skipped";
ok $class->passed, "$class_name passed";
subtest "$class_name methods" => sub {
foreach my $method ( $class->all_test_methods ) {
my $method_name = $method->name;
ok $method->passed, "$method_name passed";
ok !$method->is_skipped, "$method_name was not skipped";
cmp_ok $method->num_tests, '>', 0,
'... and some tests should have been run';
diag "Run time for $method_name: ".$method->time->duration;
}
};
my $time = $class->time;
diag "Run time for $class_name: ".$class->time->duration;
my $real = $time->real;
my $user = $time->user;
my $system = $time->system;
# do with these as you will
}
diag "Number of test classes: " . $report->num_test_classes;
diag "Number of test instances: " . $report->num_test_instances;
diag "Number of test methods: " . $report->num_test_methods;
diag "Number of tests: " . $report->num_tests;
done_testing;
If you just want to output reporting information, you do not need to run the
test suite in a subtest:
my $test_suite = Test::Class::Moose::Runner->new->runtests;
my $report = $test_suite->test_report;
...
Or even shorter:
my $report = Test::Class::Moose::Runner->new->runtests->test_report;
=head1 EXTRAS
If you would like L<Test::Class::Moose> to take care of loading your classes
for you, see L<Test::Class::Moose::Role::AutoUse> in this distribution.
=head1 DEPRECATIONS
=head2 Version 0.55
=over 4
=item * Running tests with Test::Class::Moose is deprecated - use L<Test::Class::Moose::Runner>
As of version 0.55, running tests and being a test class have been
separated. Your test classes should continue to C<use Test::Class::Moose>, but
your test runner script should use L<Test::Class::Moose::Runner>:
use Test::Class::Moose::Load 't/lib';
use Test::Class::Moose::Runner;
Test::Class::Moose::Runner->new->runtests;
Calling C<< Test::Class::Moose->new->runtests >> still works, but is
deprecated and will issue a warning.
=item * Parallel testing is totally different
The C<Test::Class::Moose::Role::Parallel> role won't do anything other than
issue a warning. See the L<Test::Class::Moose::Runner> docs for details on
running tests in parallel.
=item * The L<Test::Class::Moose::Report> C<all_test_classes> method is deprecated
This has been replaced with the C<all_test_instances> method. The
C<all_test_classes> method is still present for backwards compatibility, but
it simply calls C<all_test_instances> under the hood.
=item * The C<Test::Class::Moose::Report::Class> class is gone
It has been replaced by the C<Test::Class::Moose::Report::Instance> class,
which has the same API.
=item * The C<Test::Class::Moose::Report::Method> C<class_report> method has been renamed
This is now called C<instance_report>.
=back
=head2 Version 0.40
=over 4
=item * C<test_reporting>
As of version 0.40, the long deprecated method C<test_reporting> has now been
removed.
=item * C<$report> argument to methods deprecated
Prior to version 0.40, you used to have a second argument to all test methods
and test control methods:
sub test_something {
my ( $test, $report ) = @_;
...
}
This was annoying. It was doubly annoying in test control methods in case you
forgot it:
sub test_setup {
my ( $test, $report ) = @_;
$test->next::method; # oops, needed $report
...
}
That second argument is still passed, but it's deprecated. It's now
recommended that you call the C<< $test->test_report >> method to get that.
Instead of this:
sub test_froblinator {
my ( $test, $report ) = @_;
$report->plan(7);
...
}
You write this:
sub test_froblinator {
my $test = shift;
$test->test_report->plan(7);
...
}
=back
=head1 TODO
=over 4
=item * Callbacks for tags (for example, 'critical' tags could bailout)
=item * New test phases - start and end suite, not just start and end class/method
=back
=head1 BUGS
Please report any bugs or feature requests to C<bug-test-class-moose at rt.cpan.org>,
or through the web interface at
L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Test-Class-Moose>. I will be
notified, and then you'll automatically be notified of progress on your bug as
I make changes.
=head1 SUPPORT
You can find documentation for this module with the perldoc command.
perldoc Test::Class::Moose
You can also look for information at:
=over 4
=item * GitHub Issues (report bugs here)
L<https://github.com/test-class-moose/test-class-moose/issues>
=item * AnnoCPAN: Annotated CPAN documentation
L<http://annocpan.org/dist/Test-Class-Moose>
=item * CPAN Ratings
L<http://cpanratings.perl.org/d/Test-Class-Moose>
=item * MetaCPAN
L<https://metacpan.org/release/Test-Class-Moose/>
=back
=head1 SEE ALSO
=over 4
=item * L<Test::Routine>
I always pointed people to this when they would ask about L<Test::Class> +
L<Moose>, but I would always hear "that's not quite what I'm looking for".
I don't quite understand what the reasoning was, but I strongly encourage you
to take a look at L<Test::Routine>.
=item * L<Test::Roo>
L<Test::Routine>, but with L<Moo> instead of L<Moose>.
=item * L<Test::Class>
xUnit-style testing in Perl.
=item * L<Test::Class::Most>