source: vendor/perl/5.8.8/lib/Test/Tutorial.pod@ 3181

Last change on this file since 3181 was 3181, checked in by bird, 19 years ago

perl 5.8.8

File size: 18.5 KB
Line 
1=head1 NAME
2
3Test::Tutorial - A tutorial about writing really basic tests
4
5=head1 DESCRIPTION
6
7
8I<AHHHHHHH!!!! NOT TESTING! Anything but testing!
9Beat me, whip me, send me to Detroit, but don't make
10me write tests!>
11
12I<*sob*>
13
14I<Besides, I don't know how to write the damned things.>
15
16
17Is this you? Is writing tests right up there with writing
18documentation and having your fingernails pulled out? Did you open up
19a test and read
20
21 ######## We start with some black magic
22
23and decide that's quite enough for you?
24
25It's ok. That's all gone now. We've done all the black magic for
26you. And here are the tricks...
27
28
29=head2 Nuts and bolts of testing.
30
31Here'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
39since 1 + 1 is 2, it prints:
40
41 1..1
42 ok 1
43
44What 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
46testing. Your basic unit of testing is the I<ok>. For each thing you
47test, an C<ok> is printed. Simple. B<Test::Harness> interprets your test
48results to determine if you succeeded or failed (more on that later).
49
50Writing all these print statements rapidly gets tedious. Fortunately,
51there'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
59and that does the same thing as the code above. C<ok()> is the backbone
60of Perl testing, and we'll be using it instead of roll-your-own from
61here on. If C<ok()> gets a true value, the test passes. False, it
62fails.
63
64 #!/usr/bin/perl -w
65
66 use Test::Simple tests => 2;
67 ok( 1 + 1 == 2 );
68 ok( 2 + 2 == 5 );
69
70from 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
78C<1..2> "I'm going to run two tests." This number is used to ensure
79your test program ran all the way through and didn't die or skip some
80tests. C<ok 1> "The first test passed." C<not ok 2> "The second test
81failed". Test::Simple helpfully prints out some extra commentary about
82your tests.
83
84It's not scary. Come, hold my hand. We're going to give an example
85of testing a module. For our example, we'll be testing a date
86library, B<Date::ICal>. It's on CPAN, so download a copy and follow
87along. [2]
88
89
90=head2 Where to start?
91
92This is the hardest part of testing, where do you start? People often
93get overwhelmed at the apparent enormity of the task of testing a
94whole module. Best place to start is at the beginning. Date::ICal is
95an object-oriented module, and that means you start by making an
96object. 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
108run that and you should get:
109
110 1..2
111 ok 1
112 ok 2
113
114congratulations, you've written your first useful test.
115
116
117=head2 Names
118
119That output isn't terribly descriptive, is it? When you have two
120tests you can figure out which one is #2, but what if you have 102?
121
122Each test can be given a little descriptive name as the second
123argument 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
130So 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
139Simplest way to build up a decent testing suite is to just test what
140the manual says it does. [3] Let's pull something out of the
141L<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
162run 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
176Whoops, a failure! [4] Test::Simple helpfully lets us know on what line
177the failure occurred, but not much else. We were supposed to get 17,
178but we didn't. What did we get?? Dunno. We'll have to re-run the
179test in the debugger or throw in some print statements to find out.
180
181Instead, we'll switch from B<Test::Simple> to B<Test::More>. B<Test::More>
182does everything B<Test::Simple> does, and more! In fact, Test::More does
183things I<exactly> the way Test::Simple does. You can literally swap
184Test::Simple out and put Test::More in its place. That's just what
185we're going to do.
186
187Test::More does more than Test::Simple. The most important difference
188at this point is it provides more informative ways to say "ok".
189Although you can write almost any test with a generic C<ok()>, it
190can't tell you what went wrong. Instead, we'll use the C<is()>
191function, which lets us declare that something is supposed to be the
192same 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,
214you get some more information