1Test::Class::Load(3) User Contributed Perl Documentation Test::Class::Load(3)
2
3
4
6 Test::Class::Load - Load "Test::Class" classes automatically.
7
9 version 0.50
10
12 use Test::Class::Load qw(t/tests t/lib);
13 Test::Class->runtests;
14
16 None.
17
19 "Test::Class" typically uses a helper script to load the test classes.
20 It often looks something like this:
21
22 #!/usr/bin/perl -T
23
24 use strict;
25 use warnings;
26
27 use lib 't/tests';
28
29 use MyTest::Foo;
30 use MyTest::Foo::Bar;
31 use MyTest::Foo::Baz;
32
33 Test::Class->runtests;
34
35 This causes a problem, though. When you're writing a test class, it's
36 easy to forget to add it to the helper script. Then you run your huge
37 test suite and see that all tests pass, even though you don't notice
38 that it didn't run your new test class. Or you delete a test class and
39 you forget to remove it from the helper script.
40
41 "Test::Class::Load" automatically finds and loads your test classes for
42 you. There is no longer a need to list them individually.
43
45 Using "Test::Class::Load" is as simple as this:
46
47 #!/usr/bin/perl -T
48
49 use strict;
50 use warnings;
51
52 use Test::Class::Load 't/tests';
53
54 Test::Class->runtests;
55
56 That will search through all files in the "t/tests" directory and
57 automatically load anything which ends in ".pm". You should only put
58 test classes in those directories.
59
60 If you have test classes in more than one directory, that's OK. Just
61 list all of them in the import list.
62
63 use Test::Class::Load qw<
64 t/customer
65 t/order
66 t/inventory
67 >;
68 Test::Class->runtests;
69
71 Here's some examples of advanced usage of "Test::Class::Load".
72
73 FILTER LOADED CLASSES
74 You can redefine the filtering criteria, that is, decide what classes
75 are picked up and what others are not. You do this simply by
76 subclassing "Test::Class::Load" overriding the "is_test_class()"
77 method. You might want to do this to only load modules which inherit
78 from "Test::Class", or anything else for that matter.
79
80 is_test_class
81 $is_test_class = $class->is_test_class( $file, $directory )
82
83 Returns true if $file in $directory should be considered a test
84 class and be loaded by Test::Class::Load. The default filter simply
85 returns true if $file ends with ".pm"
86
87 For example:
88
89 use strict;
90 use warnings;
91
92 package My::Loader;
93 use base qw( Test::Class::Load );
94
95 # Overriding this selects what test classes
96 # are considered by T::C::Load
97 sub is_test_class {
98 my ( $class, $file, $dir ) = @_;
99
100 # return unless it's a .pm (the default)
101 return unless $class->SUPER::is_test_class( $file, $dir );
102
103 # and only allow .pm files with "Good" in their filename
104 return $file =~ m{Good};
105 }
106
107 1;
108
109 CUSTOMIZING TEST RUNS
110 One problem with this style of testing is that you run all of the tests
111 every time you need to test something. If you want to run only one
112 test class, it's problematic. The easy way to do this is to change
113 your helper script by deleting the "runtests" call:
114
115 #!/usr/bin/perl -T
116
117 use strict;
118 use warnings;
119
120 use Test::Class::Load 't/tests';
121
122 Then, just make sure that all of your test classes inherit from your
123 own base class which runs the tests for you. It might looks something
124 like this:
125
126 package My::Test::Class;
127
128 use strict;
129 use warnings;
130
131 use base 'Test::Class';
132
133 INIT { Test::Class->runtests } # here's the magic!
134
135 1;
136
137 Then you can run an individual test class by using the "prove" utility,
138 tell it the directory of the test classes and the name of the test
139 package you wish to run:
140
141 prove -lv -It/tests Some::Test::Class
142
143 You can even automate this by binding it to a key in "vim":
144
145 noremap ,t :!prove -lv -It/tests %<CR>
146
147 Then you can just type ",t" ('comma', 'tee') and it will run the tests
148 for your test class or the tests for your test script (if you're using
149 a traditional "Test::More" style script).
150
151 Of course, you can still run your helper script with "prove", "make
152 test" or "./Build test" to run all of your test classes.
153
154 If you do that, you'll have to make sure that the "-I" switches point
155 to your test class directories.
156
158 "Test::Class::Load" is taint safe. Because we're reading the class
159 names from the directory structure, they're marked as tainted when
160 running under taint mode. We use the following ultra-paranoid bit of
161 code to untaint them. Please file a bug report if this is too
162 restrictive.
163
164 my ($package) = $_package =~ /^([[:word:]]+(?:::[[:word:]]+)*)$/;
165
167 Curtis "Ovid" Poe, "<ovid@cpan.org>"
168
170 Please report any bugs or feature requests to
171 "bug-test-class-load@rt.cpan.org", or through the web interface at
172 <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Test-Class-Load>. I
173 will be notified, and then you'll automatically be notified of progress
174 on your bug as I make changes.
175
177 Thanks to David Wheeler for the idea and Adrian Howard for
178 "Test::Class".
179
181 Copyright 2006 Curtis "Ovid" Poe, all rights reserved.
182
183 This program is free software; you can redistribute it and/or modify it
184 under the same terms as Perl itself.
185
186
187
188perl v5.32.0 2020-07-28 Test::Class::Load(3)