📄 tutorial.pod
字号:
=head1 NAMETest::Tutorial - A tutorial about writing really basic tests=head1 DESCRIPTIONI<AHHHHHHH!!!! NOT TESTING! Anything but testing! Beat me, whip me, send me to Detroit, but don't make me write tests!>I<*sob*>I<Besides, I don't know how to write the damned things.>Is this you? Is writing tests right up there with writingdocumentation and having your fingernails pulled out? Did you open upa test and read ######## We start with some black magicand decide that's quite enough for you?It's ok. That's all gone now. We've done all the black magic foryou. And here are the tricks...=head2 Nuts and bolts of testing.Here's the most basic test program. #!/usr/bin/perl -w print "1..1\n"; print 1 + 1 == 2 ? "ok 1\n" : "not ok 1\n";since 1 + 1 is 2, it prints: 1..1 ok 1What this says is: C<1..1> "I'm going to run one test." [1] C<ok 1>"The first test passed". And that's about all magic there is totesting. Your basic unit of testing is the I<ok>. For each thing youtest, an C<ok> is printed. Simple. B<Test::Harness> interprets your testresults to determine if you succeeded or failed (more on that later).Writing all these print statements rapidly gets tedious. Fortunately,there's B<Test::Simple>. It has one function, C<ok()>. #!/usr/bin/perl -w use Test::Simple tests => 1; ok( 1 + 1 == 2 );and that does the same thing as the code above. C<ok()> is the backboneof Perl testing, and we'll be using it instead of roll-your-own fromhere on. If C<ok()> gets a true value, the test passes. False, itfails. #!/usr/bin/perl -w use Test::Simple tests => 2; ok( 1 + 1 == 2 ); ok( 2 + 2 == 5 );from that comes 1..2 ok 1 not ok 2 # Failed test (test.pl at line 5) # Looks like you failed 1 tests of 2.C<1..2> "I'm going to run two tests." This number is used to ensureyour test program ran all the way through and didn't die or skip sometests. C<ok 1> "The first test passed." C<not ok 2> "The second testfailed". Test::Simple helpfully prints out some extra commentary aboutyour tests.It's not scary. Come, hold my hand. We're going to give an exampleof testing a module. For our example, we'll be testing a datelibrary, B<Date::ICal>. It's on CPAN, so download a copy and followalong. [2]=head2 Where to start?This is the hardest part of testing, where do you start? People oftenget overwhelmed at the apparent enormity of the task of testing awhole module. Best place to start is at the beginning. Date::ICal isan object-oriented module, and that means you start by making anobject. So we test C<new()>. #!/usr/bin/perl -w use Test::Simple tests => 2; use Date::ICal; my $ical = Date::ICal->new; # create an object ok( defined $ical ); # check that we got something ok( $ical->isa('Date::ICal') ); # and it's the right classrun that and you should get: 1..2 ok 1 ok 2congratulations, you've written your first useful test.=head2 NamesThat output isn't terribly descriptive, is it? When you have twotests you can figure out which one is #2, but what if you have 102?Each test can be given a little descriptive name as the secondargument to C<ok()>. use Test::Simple tests => 2; ok( defined $ical, 'new() returned something' ); ok( $ical->isa('Date::ICal'), " and it's the right class" );So now you'd see... 1..2 ok 1 - new() returned something ok 2 - and it's the right class=head2 Test the manualSimplest way to build up a decent testing suite is to just test whatthe manual says it does. [3] Let's pull something out of the L<Date::ICal/SYNOPSIS> and test that all its bits work. #!/usr/bin/perl -w use Test::Simple tests => 8; use Date::ICal; $ical = Date::ICal->new( year => 1964, month => 10, day => 16, hour => 16, min => 12, sec => 47, tz => '0530' ); ok( defined $ical, 'new() returned something' ); ok( $ical->isa('Date::ICal'), " and it's the right class" ); ok( $ical->sec == 47, ' sec()' ); ok( $ical->min == 12, ' min()' ); ok( $ical->hour == 16, ' hour()' ); ok( $ical->day == 17, ' day()' ); ok( $ical->month == 10, ' month()' ); ok( $ical->year == 1964, ' year()' );run that and you get: 1..8 ok 1 - new() returned something ok 2 - and it's the right class ok 3 - sec() ok 4 - min() ok 5 - hour() not ok 6 - day() # Failed test (- at line 16) ok 7 - month() ok 8 - year() # Looks like you failed 1 tests of 8.Whoops, a failure! [4] Test::Simple helpfully lets us know on what linethe failure occurred, but not much else. We were supposed to get 17,but we didn't. What did we get?? Dunno. We'll have to re-run thetest in the debugger or throw in some print statements to find out.Instead, we'll switch from B<Test::Simple> to B<Test::More>. B<Test::More>does everything B<Test::Simple> does, and more! In fact, Test::More doesthings I<exactly> the way Test::Simple does. You can literally swapTest::Simple out and put Test::More in its place. That's just whatwe're going to do.Test::More does more than Test::Simple. The most important differenceat this point is it provides more informative ways to say "ok".Although you can write almost any test with a generic C<ok()>, itcan't tell you what went wrong. Instead, we'll use the C<is()>function, which lets us declare that something is supposed to be thesame as something else: #!/usr/bin/perl -w use Test::More tests => 8; use Date::ICal; $ical = Date::ICal->new( year => 1964, month => 10, day => 16, hour => 16, min => 12, sec => 47, tz => '0530' ); ok( defined $ical, 'new() returned something' ); ok( $ical->isa('Date::ICal'), " and it's the right class" ); is( $ical->sec, 47, ' sec()' ); is( $ical->min, 12, ' min()' ); is( $ical->hour, 16, ' hour()' ); is( $ical->day, 17, ' day()' ); is( $ical->month, 10, ' month()' ); is( $ical->year, 1964, ' year()' );"Is C<$ical-E<gt>sec> 47?" "Is C<$ical-E<gt>min> 12?" With C<is()> in place,you get some more information 1..8 ok 1 - new() returned something ok 2 - and it's the right class ok 3 - sec() ok 4 - min() ok 5 - hour() not ok 6 - day() # Failed test (- at line 16) # got: '16' # expected: '17' ok 7 - month() ok 8 - year() # Looks like you failed 1 tests of 8.letting us know that C<$ical-E<gt>day> returned 16, but we expected 17. Aquick check shows that the code is working fine, we made a mistakewhen writing up the tests. Just change it to: is( $ical->day, 16, ' day()' );and everything works.So any time you're doing a "this equals that" sort of test, use C<is()>.It even works on arrays. The test is always in scalar context, so youcan test how many elements are in a list this way. [5] is( @foo, 5, 'foo has 5 elements' );=head2 Sometimes the tests are wrongWhich brings us to a very important lesson. Code has bugs. Tests arecode. Ergo, tests have bugs. A failing test could mean a bug in thecode, but don't discount the possibility that the test is wrong.On the flip side, don't be tempted to prematurely declare a testincorrect just because you're having trouble finding the bug.Invalidating a test isn't something to be taken lightly, and don't useit as a cop out to avoid work.=head2 Testing lots of valuesWe're going to be wanting to test a lot of dates here, trying to trickthe code with lots of different edge cases. Does it work before 1970?After 2038? Before 1904? Do years after 10,000 give it trouble?Does it get leap years right? We could keep repeating the code above,or we could set up a little try/expect loop. use Test::More tests => 32; use Date::ICal; my %ICal_Dates = ( # An ICal string And the year, month, date # hour, minute and second we expect. '19971024T120000' => # from the docs. [ 1997, 10, 24, 12, 0, 0 ], '20390123T232832' => # after the Unix epoch [ 2039, 1, 23, 23, 28, 32 ], '19671225T000000' => # before the Unix epoch [ 1967, 12, 25, 0, 0, 0 ], '18990505T232323' => # before the MacOS epoch [ 1899, 5, 5, 23, 23, 23 ], ); while( my($ical_str, $expect) = each %ICal_Dates ) { my $ical = Date::ICal->new( ical => $ical_str ); ok( defined $ical, "new(ical => '$ical_str')" ); ok( $ical->isa('Date::ICal'), " and it's the right class" ); is( $ical->year, $expect->[0], ' year()' ); is( $ical->month, $expect->[1], ' month()' ); is( $ical->day, $expect->[2], ' day()' ); is( $ical->hour, $expect->[3], ' hour()' ); is( $ical->min, $expect->[4], ' min()' ); is( $ical->sec, $expect->[5], ' sec()' ); }So now we can test bunches of dates by just adding them toC<%ICal_Dates>. Now that it's less work to test with more dates, you'llbe inclined to just throw more in as you think of them.Only problem is, every time we add to that we have to keep adjustingthe C<use Test::More tests =E<gt> ##> line. That can rapidly getannoying. There's two ways to make this work better.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -