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
|
Test::SimpleUnit
a simplified unit testing framework
Authors
-------
Michael Granger <ged@FaerieMUD.org>
General Information
-------------------
This is a simplified Perl unit-testing framework for creating unit tests to be
run either standalone or under Test::Harness.
Testing
Testing in Test::SimpleUnit is done by running a test suite, either via 'make
test', which uses the Test::Harness 'test' target written by
ExtUtils::MakeMaker, or as a standalone script.
If errors occur while running tests via the 'make test' method, you can get
more verbose output about the test run by adding "TEST_VERBOSE=1" to the end
of the "make" invocation:
$ make test TEST_VERBOSE=1
If you want to display only the messages caused by failing assertions, you can
add a "VERBOSE=1" to the end of the "make" invocation instead:
$ make test VERBOSE=1
Test Suites
A test suite is one or more test cases, each of which tests a specific unit of
functionality.
Test Cases
A test case is a unit of testing which consists of one or more tests, combined
with setup and teardown functions that make the necessary preparations for
testing.
You may wish to split test cases up into separate files under a "t/" directory
so they will run under a Test::Harness-style "make test".
Tests
A test is a hashref which contains two key-value pairs: a name key with the
name of the test as the value, and a code reference under a test key:
{
name => 'This is the name of the test',
test => sub { ...testing code... }
}
Each test's "test" function can make one or more assertions by using the
Assertion Functions provided, or can indicate that it or any trailing tests in
the same test case should be skipped by calling one of the provided Skip
Functions.
Setup and Teardown Functions
If a test has the name 'setup' or 'teardown', it is run before or after each
test that follows it, respectively. A second or succeeding setup or teardown
function will supersede any function of the same type which preceded it. This
allows a test designer to change the setup function as the tests progress. See
the EXAMPLE section for an example of how to use this.
If a test is preceded by multiple new setup/teardown functions, the last one
to be specified is kept, and any others are discarded after being executed
once. This allows one to specify one-time setup and/or teardown functions at a
given point of testing.
The code reference value within a *setup* or *teardown* test case can
optionally be named "func" instead of "test" for clarity. If there are both
"func" and "test" key-value pairs in a *setup* or *teardown* case, the "test"
pair is silently ignored.
Saving Test Data
If the test suite requires configuration, or some other data which should
persist between test cases, it can be dumped via Data::Dumper to a file with
the saveTestData() function. In succeeding tests, it can be reloaded using the
loadTestData() function.
Example
-------
use Test::SimpleUnit qw{:functions};
# If a setup function fails, skip the rest of the tests
Test::SimpleUnit::AutoskipFailedSetup( 1 );
my $Instance;
my $RequireWasOkay = 0;
my @tests = (
# Require the module
{
name => 'require',
test => sub {
# Make sure we can load the module to be tested.
assertNoException { require MyClass };
# Try to import some functions, generating a custom error message if it
# fails.
assertNoException { MyClass->import(':myfuncs') } "Failed to import :myfuncs";
# Make sure calling 'import()' actually imported the functions
assertRef 'CODE', *::myfunc{CODE};
assertRef 'CODE', *::myotherfunc{CODE};
# Set the flag to let the setup function know the module loaded okay
$RequireWasOkay = 1;
},
},
# Setup function (this will be run before any tests which follow)
{
name => 'setup',
test => sub {
# If the previous test didn't finish, it's untestable, so just skip the
# rest of the tests
skipAll "Module failed to load" unless $RequireWasOkay;
$Instance = new MyClass;
},
},
# Teardown function (this will be run after any tests which follow)
{
name => 'teardown',
test => sub {
undef $Instance;
},
},
# One-time setup function -- overrides the previous setup, but is
# immediately discarded after executing once.
{
name => 'setup',
func => sub {
MyClass::prepNetwork();
},
},
# Test the connect() and disconnect() methods
{
name => 'connect() and disconnect()',
test => sub {
my $rval;
assertNoException { $rval = $Instance->connect };
assert $rval, "Connect failed without error.";
assertNoException { $Instance->disconnect };
},
},
# Now override the previous setup function with a new one that does
# a connect() before each remaining test.
{
name => 'setup',
test => sub {
$Instance = new MyClass;
$Instance->connect;
},
}
# Same thing for teardown/disconnect()
{
name => 'teardown',
test => sub {
$Instance->disconnect;
undef $Instance;
},
},
...
);
runTests( @testSuite );
Caveats
-------
I would greatly appreciate feedback on any aspect of this software. Suggestions,
feature requests, questions, design critiques, and bug reports are most
welcome. Relevant patches are particularly helpful. I may be reached at
<ged@FaerieMUD.org>.
Installation
------------
$ perl Makefile.PL
$ make
$ make test
(become root)
# make install
== Legal
This module is Open Source Software which is Copyright (c) 1999-2003 by The
FaerieMUD Consortium.
You may use, modify, and/or redistribute this software under the terms of either
the Perl Artistic License or the GNU Public License (version 2 or later),
whichever you prefer. A copy of the Artistic license should have been included
in this distribution (See the file Artistic). If it was not, a copy of it may be
obtained from http://language.perl.com/misc/Artistic.html or
http://www.faeriemud.org/artistic.html).
THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND
FITNESS FOR A PARTICULAR PURPOSE.
Rev: $Id: README,v 1.5 2003/01/15 21:48:39 deveiant Exp $
|