Alright - I already have written some gitzy little tests for the
tiny, first module I wrote 3 months ago. And it felt great,
too! But now the feeling is a little hollow. All I've done
is one test per sub, testing the numbers I give it yield the
answer I expect. I mean, for me the only way a test should fail is
if the module
isn't there. (or perhaps, if I go and change
the checksum algorithm in a drunken stupor, the tests would
bail me out) I somehow feel there should be more to it.
So my question is - and it's quite newbie - How do I
write a test? Forgive me for being pedantic, but there's not
a lot out there. Test::Harness is briefly described in the
Camel book (Amelia, to some ;) and there's the nodes
Writing tests and
perlman:Test::Harness.
Does make test
run all of the tests that it finds in the
t directory automatically?
Another question - How would you write tests for a program?
The same as you would for a module? (but those are run with
make test)
For added entertainment, I'll throw in my "test suite".
(Comments very welcome - snigger if you like ;)
# Before `make install' is performed this script should
# be runnable with `make test'. After `make install' it
# should work as `perl test.pl'
####### We start with some black magic to print on failure.
# Change 1..1 below to 1..last_test_to_print .
# (It may become useful if the test is moved to ./t
# subdirectory.)
BEGIN { $| = 1; print "1..7\n"; }
END {print "not ok 1\n" unless $loaded;}
use Local::Barcode::Protocol;
$loaded = 1;
print "ok 1\n";
######################### End of black magic.
# Should tighten test code, should use Test::Harness
# do some sanity checking on values
#
$testcode = '123456789/12345/1';
@decoded = decode_string($testcode);
if (scalar @decoded == 3) {
print "ok 2\n";
} else {
print "not ok 2\n
$decoded[0],$decoded[1],$decoded[2]\n";
}
$one = '123456789';
$two = '12345';
$three = '1';
$result = encode_string($one, $two, $three);
if ($testcode eq $result) {
print "ok 3\n";
} else {
print "not ok 3 ... encode_string
Wanted $testcode Got $result\n";
}
$result = generate_checksum($one, $two);
if ($three eq $result) {
print "ok 4\n";
} else {
print "not ok 4\n $result\n";
}
$result = is_valid_student($one);
if (defined $result) {
print "ok 5\n";
} else {
print "not ok 5\n $result\n";
}
$result = is_valid_assignment($two);
if (defined $result) {
print "ok 6\n";
} else {
print "not ok 6\n $result\n";
}
$result = assign_belongto_student($one, $two);
if (defined $result) {
print "ok 7\n";
} else {
print "not ok 7\n $result\n";
}
Good topic!
Ea :wq
Edit Masem 2001-10-11 - Code Tags
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.