1Test::Unit::Lite(3) User Contributed Perl Documentation Test::Unit::Lite(3)
2
3
4
6 Test::Unit::Lite - Unit testing without external dependencies
7
9 Bundling the Test::Unit::Lite as a part of package distribution:
10
11 perl -MTest::Unit::Lite -e bundle
12
13 Running all test units:
14
15 perl -MTest::Unit::Lite -e all_tests
16
17 Using as a replacement for Test::Unit:
18
19 package FooBarTest;
20 use Test::Unit::Lite; # unnecessary if module isn't directly used
21 use base 'Test::Unit::TestCase';
22
23 sub new {
24 my $self = shift()->SUPER::new(@_);
25 # your state for fixture here
26 return $self;
27 }
28
29 sub set_up {
30 # provide fixture
31 }
32 sub tear_down {
33 # clean up after test
34 }
35 sub test_foo {
36 my $self = shift;
37 my $obj = ClassUnderTest->new(...);
38 $self->assert_not_null($obj);
39 $self->assert_equals('expected result', $obj->foo);
40 $self->assert(qr/pattern/, $obj->foobar);
41 }
42 sub test_bar {
43 # test the bar feature
44 }
45
47 This framework provides lighter version of Test::Unit framework. It
48 implements some of the Test::Unit classes and methods needed to run
49 test units. The Test::Unit::Lite tries to be compatible with public
50 API of Test::Unit. It doesn't implement all classes and methods at 100%
51 and only those necessary to run tests are available.
52
53 The Test::Unit::Lite can be distributed as a part of package
54 distribution, so the package can be distributed without dependency on
55 modules outside standard Perl distribution. The Test::Unit::Lite is
56 provided as a single file.
57
58 Bundling the Test::Unit::Lite as a part of package distribution
59 The Test::Unit::Lite framework can be bundled to the package
60 distribution. Then the Test::Unit::Lite module is copied to the inc
61 directory of the source directory for the package distribution.
62
64 bundle
65 Copies Test::Unit::Lite modules into inc directory. Creates
66 missing subdirectories if needed. Silently overwrites previous
67 module if was existing.
68
69 all_tests
70 Creates new test runner for Test::Unit::Lite::AllTests suite which
71 searches for test units in t/tlib directory.
72
74 Test::Unit::TestCase
75 This is a base class for single unit test module. The user's unit test
76 module can override the default methods that are simple stubs.
77
78 The MESSAGE argument is optional and is included to the default error
79 message when the assertion is false.
80
81 new The default constructor which just bless an empty anonymous hash
82 reference.
83
84 set_up
85 This method is called at the start of each test unit processing.
86 It is empty method and can be overridden in derived class.
87
88 tear_down
89 This method is called at the end of each test unit processing. It
90 is empty method and can be overridden in derived class.
91
92 list_tests
93 Returns the list of test methods in this class and base classes.
94
95 fail([MESSAGE])
96 Immediate fail the test.
97
98 assert(ARG [, MESSAGE])
99 Checks if ARG expression returns true value.
100
101 assert_null(ARG [, MESSAGE])
102 assert_not_null(ARG [, MESSAGE])
103 Checks if ARG is defined or not defined.
104
105 assert_equals(ARG1, ARG2 [, MESSAGE])
106 assert_not_equals(ARG1, ARG2 [, MESSAGE])
107 Checks if ARG1 and ARG2 are equals or not equals. If ARG1 and ARG2
108 look like numbers then they are compared with '==' operator,
109 otherwise the string 'eq' operator is used.
110
111 assert_num_equals(ARG1, ARG2 [, MESSAGE])
112 assert_num_not_equals(ARG1, ARG2 [, MESSAGE])
113 Force numeric comparison.
114
115 assert_str_equals(ARG1, ARG2 [, MESSAGE])
116 assert_str_not_equals(ARG1, ARG2 [, MESSAGE])
117 Force string comparison.
118
119 assert(qr/PATTERN/, ARG [, MESSAGE])
120 assert_matches(qr/PATTERN/, ARG [, MESSAGE])
121 assert_does_not_match(qr/PATTERN/, ARG [, MESSAGE])
122 Checks if ARG matches PATTER regexp.
123
124 assert_deep_equals(ARG1, ARG2 [, MESSAGE])
125 assert_deep_not_equals(ARG1, ARG2 [, MESSAGE])
126 Check if reference ARG1 is a deep copy of reference ARG2 or not.
127 The references can be deep structure. If they are different, the
128 message will display the place where they start differing.
129
130 Test::Unit::TestSuite
131 This is a base class for test suite, which groups several test units.
132
133 empty_new([NAME])
134 Creates a fresh suite with no tests.
135
136 new([CLASS | TEST])
137 Creates a test suite from unit test name or class. If a test suite
138 is provided as the argument, it merely returns that suite. If a
139 test case is provided, it extracts all test case methods (see
140 Test::Unit::TestCase->list_test) from the test case into a new test
141 suite.
142
143 name
144 Contains the name of the current test suite.
145
146 units
147 Contains the list of test units.
148
149 add_test([TEST_CLASSNAME | TEST_OBJECT])
150 Adds the test object to a suite.
151
152 count_test_cases
153 Returns the number of test cases in this suite.
154
155 run Runs the test suite and output the results as TAP report.
156
157 Test::Unit::TestRunner
158 This is the test runner which outputs text report about finished test
159 suite.
160
161 new([$fh_out [, $fh_err]])
162 The constructor for whole test framework. Its optional parameters
163 are filehandles for standard output and error messages.
164
165 fh_out
166 Contains the filehandle for standard output.
167
168 fh_err
169 Contains the filehandle for error messages.
170
171 suite
172 Contains the test suite object.
173
174 print_header
175 Called before running test suite.
176
177 print_error
178 Called after error was occurred on "set_up" or "tear_down" method.
179
180 print_failure
181 Called after test unit is failed.
182
183 print_pass
184 Called after test unit is passed.
185
186 print_footer
187 Called after running test suite.
188
189 start(TEST_SUITE)
190 Starts the test suite.
191
192 Test::Unit::Result
193 This object contains the results of test suite.
194
195 new Creates a new object.
196
197 messages
198 Contains the array of result messages. The single message is a
199 hash which contains:
200
201 test
202 the test unit name,
203
204 type
205 the type of message (PASS, ERROR, FAILURE),
206
207 message
208 the text of message.
209
210 errors
211 Contains the number of collected errors.
212
213 failures
214 Contains the number of collected failures.
215
216 passes
217 Contains the number of collected passes.
218
219 add_error(TEST, MESSAGE)
220 Adds an error to the report.
221
222 add_failure(TEST, MESSAGE)
223 Adds an failure to the report.
224
225 add_pass(TEST, MESSAGE)
226 Adds a pass to the report.
227
228 Test::Unit::HarnessUnit
229 This is the test runner which outputs in the same format that
230 Test::Harness expects (Test Anything Protocol). It is derived from
231 Test::Unit::TestRunner.
232
233 Test::Unit::Debug
234 The empty class which is provided for compatibility with original
235 Test::Unit framework.
236
237 Test::Unit::Lite::AllTests
238 The test suite which searches for test units in t/tlib directory.
239
241 Test::Unit::Lite should be compatible with public API of Test::Unit.
242 The Test::Unit::Lite also has some known incompatibilities:
243
244 · The test methods are sorted alphabetically.
245
246 · It implements new assertion method: assert_deep_not_equals.
247
248 · Does not support ok, assert(CODEREF, @ARGS) and multi_assert.
249
250 "Test::Unit::Lite" is compatible with Test::Assert assertion functions.
251
253 t/tlib/SuccessTest.pm
254 This is the simple unit test module.
255
256 package SuccessTest;
257
258 use strict;
259 use warnings;
260
261 use base 'Test::Unit::TestCase';
262
263 sub test_success {
264 my $self = shift;
265 $self->assert(1);
266 }
267
268 1;
269
270 t/all_tests.t
271 This is the test script for Test::Harness called with "make test".
272
273 #!/usr/bin/perl
274
275 use strict;
276 use warnings;
277
278 use File::Spec;
279 use Cwd;
280
281 BEGIN {
282 unshift @INC, map { /(.*)/; $1 } split(/:/, $ENV{PERL5LIB}) if defined $ENV{PERL5LIB} and ${^TAINT};
283
284 my $cwd = ${^TAINT} ? do { local $_=getcwd; /(.*)/; $1 } : '.';
285 unshift @INC, File::Spec->catdir($cwd, 'inc');
286 unshift @INC, File::Spec->catdir($cwd, 'lib');
287 }
288
289 use Test::Unit::Lite;
290
291 local $SIG{__WARN__} = sub { require Carp; Carp::confess("Warning: $_[0]") };
292
293 Test::Unit::HarnessUnit->new->start('Test::Unit::Lite::AllTests');
294
295 t/test.pl
296 This is the optional script for calling test suite directly.
297
298 #!/usr/bin/perl
299
300 use strict;
301 use warnings;
302
303 use File::Basename;
304 use File::Spec;
305 use Cwd;
306
307 BEGIN {
308 chdir dirname(__FILE__) or die "$!";
309 chdir '..' or die "$!";
310
311 unshift @INC, map { /(.*)/; $1 } split(/:/, $ENV{PERL5LIB}) if defined $ENV{PERL5LIB} and ${^TAINT};
312
313 my $cwd = ${^TAINT} ? do { local $_=getcwd; /(.*)/; $1 } : '.';
314 unshift @INC, File::Spec->catdir($cwd, 'inc');
315 unshift @INC, File::Spec->catdir($cwd, 'lib');
316 }
317
318 use Test::Unit::Lite;
319
320 local $SIG{__WARN__} = sub { require Carp; Carp::confess("Warning: $_[0]") };
321
322 all_tests;
323
324 This is perl equivalent of shell command line:
325
326 perl -Iinc -Ilib -MTest::Unit::Lite -w -e all_tests
327
329 Test::Unit, Test::Assert.
330
332 The Test::Unit::Lite was tested as a Test::Unit replacement for
333 following distributions: Test::C2FIT, XAO::Base, Exception::Base.
334
336 If you find the bug or want to implement new features, please report it
337 at <https://github.com/dex4er/perl-Test-Unit-Lite/issues>
338
339 The code repository is available at
340 <http://github.com/dex4er/perl-Test-Unit-Lite>
341
343 Piotr Roszatycki <dexter@cpan.org>
344
346 Copyright (c) 2007-2009, 2012 by Piotr Roszatycki <dexter@cpan.org>.
347
348 This program is free software; you can redistribute it and/or modify it
349 under the same terms as Perl itself.
350
351 See <http://www.perl.com/perl/misc/Artistic.html>
352
353
354
355perl v5.30.0 2019-07-26 Test::Unit::Lite(3)