| 1 | =head1 NAME
|
|---|
| 2 |
|
|---|
| 3 | Test::Tutorial - A tutorial about writing really basic tests
|
|---|
| 4 |
|
|---|
| 5 | =head1 DESCRIPTION
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 | I<AHHHHHHH!!!! NOT TESTING! Anything but testing!
|
|---|
| 9 | Beat me, whip me, send me to Detroit, but don't make
|
|---|
| 10 | me write tests!>
|
|---|
| 11 |
|
|---|
| 12 | I<*sob*>
|
|---|
| 13 |
|
|---|
| 14 | I<Besides, I don't know how to write the damned things.>
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 | Is this you? Is writing tests right up there with writing
|
|---|
| 18 | documentation and having your fingernails pulled out? Did you open up
|
|---|
| 19 | a test and read
|
|---|
| 20 |
|
|---|
| 21 | ######## We start with some black magic
|
|---|
| 22 |
|
|---|
| 23 | and decide that's quite enough for you?
|
|---|
| 24 |
|
|---|
| 25 | It's ok. That's all gone now. We've done all the black magic for
|
|---|
| 26 | you. And here are the tricks...
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 | =head2 Nuts and bolts of testing.
|
|---|
| 30 |
|
|---|
| 31 | Here's the most basic test program.
|
|---|
| 32 |
|
|---|
| 33 | #!/usr/bin/perl -w
|
|---|
| 34 |
|
|---|
| 35 | print "1..1\n";
|
|---|
| 36 |
|
|---|
| 37 | print 1 + 1 == 2 ? "ok 1\n" : "not ok 1\n";
|
|---|
| 38 |
|
|---|
| 39 | since 1 + 1 is 2, it prints:
|
|---|
| 40 |
|
|---|
| 41 | 1..1
|
|---|
| 42 | ok 1
|
|---|
| 43 |
|
|---|
| 44 | What this says is: C<1..1> "I'm going to run one test." [1] C<ok 1>
|
|---|
| 45 | "The first test passed". And that's about all magic there is to
|
|---|
| 46 | testing. Your basic unit of testing is the I<ok>. For each thing you
|
|---|
| 47 | test, an C<ok> is printed. Simple. B<Test::Harness> interprets your test
|
|---|
| 48 | results to determine if you succeeded or failed (more on that later).
|
|---|
| 49 |
|
|---|
| 50 | Writing all these print statements rapidly gets tedious. Fortunately,
|
|---|
| 51 | there's B<Test::Simple>. It has one function, C<ok()>.
|
|---|
| 52 |
|
|---|
| 53 | #!/usr/bin/perl -w
|
|---|
| 54 |
|
|---|
| 55 | use Test::Simple tests => 1;
|
|---|
| 56 |
|
|---|
| 57 | ok( 1 + 1 == 2 );
|
|---|
| 58 |
|
|---|
| 59 | and that does the same thing as the code above. C<ok()> is the backbone
|
|---|
| 60 | of Perl testing, and we'll be using it instead of roll-your-own from
|
|---|
| 61 | here on. If C<ok()> gets a true value, the test passes. False, it
|
|---|
| 62 | fails.
|
|---|
| 63 |
|
|---|
| 64 | #!/usr/bin/perl -w
|
|---|
| 65 |
|
|---|
| 66 | use Test::Simple tests => 2;
|
|---|
| 67 | ok( 1 + 1 == 2 );
|
|---|
| 68 | ok( 2 + 2 == 5 );
|
|---|
| 69 |
|
|---|
| 70 | from that comes
|
|---|
| 71 |
|
|---|
| 72 | 1..2
|
|---|
| 73 | ok 1
|
|---|
| 74 | not ok 2
|
|---|
| 75 | # Failed test (test.pl at line 5)
|
|---|
| 76 | # Looks like you failed 1 tests of 2.
|
|---|
| 77 |
|
|---|
| 78 | C<1..2> "I'm going to run two tests." This number is used to ensure
|
|---|
| 79 | your test program ran all the way through and didn't die or skip some
|
|---|
| 80 | tests. C<ok 1> "The first test passed." C<not ok 2> "The second test
|
|---|
| 81 | failed". Test::Simple helpfully prints out some extra commentary about
|
|---|
| 82 | your tests.
|
|---|
| 83 |
|
|---|
| 84 | It's not scary. Come, hold my hand. We're going to give an example
|
|---|
| 85 | of testing a module. For our example, we'll be testing a date
|
|---|
| 86 | library, B<Date::ICal>. It's on CPAN, so download a copy and follow
|
|---|
| 87 | along. [2]
|
|---|
| 88 |
|
|---|
| 89 |
|
|---|
| 90 | =head2 Where to start?
|
|---|
| 91 |
|
|---|
| 92 | This is the hardest part of testing, where do you start? People often
|
|---|
| 93 | get overwhelmed at the apparent enormity of the task of testing a
|
|---|
| 94 | whole module. Best place to start is at the beginning. Date::ICal is
|
|---|
| 95 | an object-oriented module, and that means you start by making an
|
|---|
| 96 | object. So we test C<new()>.
|
|---|
| 97 |
|
|---|
| 98 | #!/usr/bin/perl -w
|
|---|
| 99 |
|
|---|
| 100 | use Test::Simple tests => 2;
|
|---|
| 101 |
|
|---|
| 102 | use Date::ICal;
|
|---|
| 103 |
|
|---|
| 104 | my $ical = Date::ICal->new; # create an object
|
|---|
| 105 | ok( defined $ical ); # check that we got something
|
|---|
| 106 | ok( $ical->isa('Date::ICal') ); # and it's the right class
|
|---|
| 107 |
|
|---|
| 108 | run that and you should get:
|
|---|
| 109 |
|
|---|
| 110 | 1..2
|
|---|
| 111 | ok 1
|
|---|
| 112 | ok 2
|
|---|
| 113 |
|
|---|
| 114 | congratulations, you've written your first useful test.
|
|---|
| 115 |
|
|---|
| 116 |
|
|---|
| 117 | =head2 Names
|
|---|
| 118 |
|
|---|
| 119 | That output isn't terribly descriptive, is it? When you have two
|
|---|
| 120 | tests you can figure out which one is #2, but what if you have 102?
|
|---|
| 121 |
|
|---|
| 122 | Each test can be given a little descriptive name as the second
|
|---|
| 123 | argument to C<ok()>.
|
|---|
| 124 |
|
|---|
| 125 | use Test::Simple tests => 2;
|
|---|
| 126 |
|
|---|
| 127 | ok( defined $ical, 'new() returned something' );
|
|---|
| 128 | ok( $ical->isa('Date::ICal'), " and it's the right class" );
|
|---|
| 129 |
|
|---|
| 130 | So now you'd see...
|
|---|
| 131 |
|
|---|
| 132 | 1..2
|
|---|
| 133 | ok 1 - new() returned something
|
|---|
| 134 | ok 2 - and it's the right class
|
|---|
| 135 |
|
|---|
| 136 |
|
|---|
| 137 | =head2 Test the manual
|
|---|
| 138 |
|
|---|
| 139 | Simplest way to build up a decent testing suite is to just test what
|
|---|
| 140 | the manual says it does. [3] Let's pull something out of the
|
|---|
| 141 | L<Date::ICal/SYNOPSIS> and test that all its bits work.
|
|---|
| 142 |
|
|---|
| 143 | #!/usr/bin/perl -w
|
|---|
| 144 |
|
|---|
| 145 | use Test::Simple tests => 8;
|
|---|
| 146 |
|
|---|
| 147 | use Date::ICal;
|
|---|
| 148 |
|
|---|
| 149 | $ical = Date::ICal->new( year => 1964, month => 10, day => 16,
|
|---|
| 150 | hour => 16, min => 12, sec => 47,
|
|---|
| 151 | tz => '0530' );
|
|---|
| 152 |
|
|---|
| 153 | ok( defined $ical, 'new() returned something' );
|
|---|
| 154 | ok( $ical->isa('Date::ICal'), " and it's the right class" );
|
|---|
| 155 | ok( $ical->sec == 47, ' sec()' );
|
|---|
| 156 | ok( $ical->min == 12, ' min()' );
|
|---|
| 157 | ok( $ical->hour == 16, ' hour()' );
|
|---|
| 158 | ok( $ical->day == 17, ' day()' );
|
|---|
| 159 | ok( $ical->month == 10, ' month()' );
|
|---|
| 160 | ok( $ical->year == 1964, ' year()' );
|
|---|
| 161 |
|
|---|
| 162 | run that and you get:
|
|---|
| 163 |
|
|---|
| 164 | 1..8
|
|---|
| 165 | ok 1 - new() returned something
|
|---|
| 166 | ok 2 - and it's the right class
|
|---|
| 167 | ok 3 - sec()
|
|---|
| 168 | ok 4 - min()
|
|---|
| 169 | ok 5 - hour()
|
|---|
| 170 | not ok 6 - day()
|
|---|
| 171 | # Failed test (- at line 16)
|
|---|
| 172 | ok 7 - month()
|
|---|
| 173 | ok 8 - year()
|
|---|
| 174 | # Looks like you failed 1 tests of 8.
|
|---|
| 175 |
|
|---|
| 176 | Whoops, a failure! [4] Test::Simple helpfully lets us know on what line
|
|---|
| 177 | the failure occurred, but not much else. We were supposed to get 17,
|
|---|
| 178 | but we didn't. What did we get?? Dunno. We'll have to re-run the
|
|---|
| 179 | test in the debugger or throw in some print statements to find out.
|
|---|
| 180 |
|
|---|
| 181 | Instead, we'll switch from B<Test::Simple> to B<Test::More>. B<Test::More>
|
|---|
| 182 | does everything B<Test::Simple> does, and more! In fact, Test::More does
|
|---|
| 183 | things I<exactly> the way Test::Simple does. You can literally swap
|
|---|
| 184 | Test::Simple out and put Test::More in its place. That's just what
|
|---|
| 185 | we're going to do.
|
|---|
| 186 |
|
|---|
| 187 | Test::More does more than Test::Simple. The most important difference
|
|---|
| 188 | at this point is it provides more informative ways to say "ok".
|
|---|
| 189 | Although you can write almost any test with a generic C<ok()>, it
|
|---|
| 190 | can't tell you what went wrong. Instead, we'll use the C<is()>
|
|---|
| 191 | function, which lets us declare that something is supposed to be the
|
|---|
| 192 | same as something else:
|
|---|
| 193 |
|
|---|
| 194 | #!/usr/bin/perl -w
|
|---|
| 195 |
|
|---|
| 196 | use Test::More tests => 8;
|
|---|
| 197 |
|
|---|
| 198 | use Date::ICal;
|
|---|
| 199 |
|
|---|
| 200 | $ical = Date::ICal->new( year => 1964, month => 10, day => 16,
|
|---|
| 201 | hour => 16, min => 12, sec => 47,
|
|---|
| 202 | tz => '0530' );
|
|---|
| 203 |
|
|---|
| 204 | ok( defined $ical, 'new() returned something' );
|
|---|
| 205 | ok( $ical->isa('Date::ICal'), " and it's the right class" );
|
|---|
| 206 | is( $ical->sec, 47, ' sec()' );
|
|---|
| 207 | is( $ical->min, 12, ' min()' );
|
|---|
| 208 | is( $ical->hour, 16, ' hour()' );
|
|---|
| 209 | is( $ical->day, 17, ' day()' );
|
|---|
| 210 | is( $ical->month, 10, ' month()' );
|
|---|
| 211 | is( $ical->year, 1964, ' year()' );
|
|---|
| 212 |
|
|---|
| 213 | "Is C<$ical-E<gt>sec> 47?" "Is C<$ical-E<gt>min> 12?" With C<is()> in place,
|
|---|
| 214 | you get some more information
|
|---|
|
|---|