📄 test::tutorial.3
字号:
.\" Automatically generated by Pod::Man 2.16 (Pod::Simple 3.05).\".\" Standard preamble:.\" ========================================================================.de Sh \" Subsection heading.br.if t .Sp.ne 5.PP\fB\\$1\fR.PP...de Sp \" Vertical space (when we can't use .PP).if t .sp .5v.if n .sp...de Vb \" Begin verbatim text.ft CW.nf.ne \\$1...de Ve \" End verbatim text.ft R.fi...\" Set up some character translations and predefined strings. \*(-- will.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left.\" double quote, and \*(R" will give a right double quote. \*(C+ will.\" give a nicer C++. Capital omega is used to do unbreakable dashes and.\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff,.\" nothing in troff, for use with C<>..tr \(*W-.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'.ie n \{\. ds -- \(*W-. ds PI pi. if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch. if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch. ds L" "". ds R" "". ds C` "". ds C' ""'br\}.el\{\. ds -- \|\(em\|. ds PI \(*p. ds L" ``. ds R" '''br\}.\".\" Escape single quotes in literal strings from groff's Unicode transform..ie \n(.g .ds Aq \(aq.el .ds Aq '.\".\" If the F register is turned on, we'll generate index entries on stderr for.\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index.\" entries marked with X<> in POD. Of course, you'll have to process the.\" output yourself in some meaningful fashion..ie \nF \{\. de IX. tm Index:\\$1\t\\n%\t"\\$2"... nr % 0. rr F.\}.el \{\. de IX...\}.\".\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2)..\" Fear. Run. Save yourself. No user-serviceable parts.. \" fudge factors for nroff and troff.if n \{\. ds #H 0. ds #V .8m. ds #F .3m. ds #[ \f1. ds #] \fP.\}.if t \{\. ds #H ((1u-(\\\\n(.fu%2u))*.13m). ds #V .6m. ds #F 0. ds #[ \&. ds #] \&.\}. \" simple accents for nroff and troff.if n \{\. ds ' \&. ds ` \&. ds ^ \&. ds , \&. ds ~ ~. ds /.\}.if t \{\. ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u". ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'. ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'. ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'. ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'. ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'.\}. \" troff and (daisy-wheel) nroff accents.ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'.ds 8 \h'\*(#H'\(*b\h'-\*(#H'.ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#].ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'.ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'.ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#].ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#].ds ae a\h'-(\w'a'u*4/10)'e.ds Ae A\h'-(\w'A'u*4/10)'E. \" corrections for vroff.if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'.if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'. \" for low resolution devices (crt and lpr).if \n(.H>23 .if \n(.V>19 \\{\. ds : e. ds 8 ss. ds o a. ds d- d\h'-1'\(ga. ds D- D\h'-1'\(hy. ds th \o'bp'. ds Th \o'LP'. ds ae ae. ds Ae AE.\}.rm #[ #] #H #V #F C.\" ========================================================================.\".IX Title "Test::Tutorial 3".TH Test::Tutorial 3 "2006-11-05" "perl v5.10.0" "User Contributed Perl Documentation".\" For nroff, turn off justification. Always turn off hyphenation; it makes.\" way too many mistakes in technical documents..if n .ad l.nh.SH "NAME"Test::Tutorial \- A tutorial about writing really basic tests.SH "DESCRIPTION".IX Header "DESCRIPTION"\&\fI\s-1AHHHHHHH\s0!!!! \s-1NOT\s0 \s-1TESTING\s0! Anything but testing! Beat me, whip me, send me to Detroit, but don't make me write tests!\fR.PP\&\fI*sob*\fR.PP\&\fIBesides, I don't know how to write the damned things.\fR.PPIs this you? Is writing tests right up there with writingdocumentation and having your fingernails pulled out? Did you open upa test and read.PP.Vb 1\& ######## We start with some black magic.Ve.PPand decide that's quite enough for you?.PPIt's ok. That's all gone now. We've done all the black magic foryou. And here are the tricks....Sh "Nuts and bolts of testing.".IX Subsection "Nuts and bolts of testing."Here's the most basic test program..PP.Vb 1\& #!/usr/bin/perl \-w\&\& print "1..1\en";\&\& print 1 + 1 == 2 ? "ok 1\en" : "not ok 1\en";.Ve.PPsince 1 + 1 is 2, it prints:.PP.Vb 2\& 1..1\& ok 1.Ve.PPWhat this says is: \f(CW1..1\fR \*(L"I'm going to run one test.\*(R" [1] \f(CW\*(C`ok 1\*(C'\fR\&\*(L"The first test passed\*(R". And that's about all magic there is totesting. Your basic unit of testing is the \fIok\fR. For each thing youtest, an \f(CW\*(C`ok\*(C'\fR is printed. Simple. \fBTest::Harness\fR interprets your testresults to determine if you succeeded or failed (more on that later)..PPWriting all these print statements rapidly gets tedious. Fortunately,there's \fBTest::Simple\fR. It has one function, \f(CW\*(C`ok()\*(C'\fR..PP.Vb 1\& #!/usr/bin/perl \-w\&\& use Test::Simple tests => 1;\&\& ok( 1 + 1 == 2 );.Ve.PPand that does the same thing as the code above. \f(CW\*(C`ok()\*(C'\fR is the backboneof Perl testing, and we'll be using it instead of roll-your-own fromhere on. If \f(CW\*(C`ok()\*(C'\fR gets a true value, the test passes. False, itfails..PP.Vb 1\& #!/usr/bin/perl \-w\&\& use Test::Simple tests => 2;\& ok( 1 + 1 == 2 );\& ok( 2 + 2 == 5 );.Ve.PPfrom that comes.PP.Vb 5\& 1..2\& ok 1\& not ok 2\& # Failed test (test.pl at line 5)\& # Looks like you failed 1 tests of 2..Ve.PP\&\f(CW1..2\fR \*(L"I'm going to run two tests.\*(R" This number is used to ensureyour test program ran all the way through and didn't die or skip sometests. \f(CW\*(C`ok 1\*(C'\fR \*(L"The first test passed.\*(R" \f(CW\*(C`not ok 2\*(C'\fR \*(L"The second testfailed\*(R". Test::Simple helpfully prints out some extra commentary aboutyour tests..PPIt'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, \fBDate::ICal\fR. It's on \s-1CPAN\s0, so download a copy and followalong. [2].Sh "Where to start?".IX Subsection "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 \f(CW\*(C`new()\*(C'\fR..PP.Vb 1\& #!/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(\*(AqDate::ICal\*(Aq) ); # and it\*(Aqs the right class.Ve.PPrun that and you should get:.PP.Vb 3\& 1..2\& ok 1\& ok 2.Ve.PPcongratulations, you've written your first useful test..Sh "Names".IX Subsection "Names"That 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?.PPEach test can be given a little descriptive name as the secondargument to \f(CW\*(C`ok()\*(C'\fR..PP.Vb 1\& use Test::Simple tests => 2;\&\& ok( defined $ical, \*(Aqnew() returned something\*(Aq );\& ok( $ical\->isa(\*(AqDate::ICal\*(Aq), " and it\*(Aqs the right class" );.Ve.PPSo now you'd see....PP.Vb 3\& 1..2\& ok 1 \- new() returned something\& ok 2 \- and it\*(Aqs the right class.Ve.Sh "Test the manual".IX Subsection "Test the manual"Simplest 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"\s-1SYNOPSIS\s0\*(R" in Date::ICal and test that all its bits work..PP.Vb 1\& #!/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 => \*(Aq0530\*(Aq );\&\& ok( defined $ical, \*(Aqnew() returned something\*(Aq );\& ok( $ical\->isa(\*(AqDate::ICal\*(Aq), " and it\*(Aqs the right class" );\& ok( $ical\->sec == 47, \*(Aq sec()\*(Aq );\& ok( $ical\->min == 12, \*(Aq min()\*(Aq ); \& ok( $ical\->hour == 16, \*(Aq hour()\*(Aq );\& ok( $ical\->day == 17, \*(Aq day()\*(Aq );\& ok( $ical\->month == 10, \*(Aq month()\*(Aq );\& ok( $ical\->year == 1964, \*(Aq year()\*(Aq );.Ve.PPrun that and you get:.PP.Vb 11\& 1..8\& ok 1 \- new() returned something\& ok 2 \- and it\*(Aqs 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..Ve.PPWhoops, 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..PPInstead, we'll switch from \fBTest::Simple\fR to \fBTest::More\fR. \fBTest::More\fRdoes everything \fBTest::Simple\fR does, and more! In fact, Test::More doesthings \fIexactly\fR 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..PPTest::More does more than Test::Simple. The most important differenceat this point is it provides more informative ways to say \*(L"ok\*(R".Although you can write almost any test with a generic \f(CW\*(C`ok()\*(C'\fR, itcan't tell you what went wrong. Instead, we'll use the \f(CW\*(C`is()\*(C'\fRfunction, which lets us declare that something is supposed to be thesame as something else:.PP.Vb 1\& #!/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 => \*(Aq0530\*(Aq );\&\& ok( defined $ical, \*(Aqnew() returned something\*(Aq );\& ok( $ical\->isa(\*(AqDate::ICal\*(Aq), " and it\*(Aqs the right class" );\& is( $ical\->sec, 47, \*(Aq sec()\*(Aq );\& is( $ical\->min, 12, \*(Aq min()\*(Aq ); \& is( $ical\->hour, 16, \*(Aq hour()\*(Aq );\& is( $ical\->day, 17, \*(Aq day()\*(Aq );\& is( $ical\->month, 10, \*(Aq month()\*(Aq );\& is( $ical\->year, 1964, \*(Aq year()\*(Aq );.Ve.PP"Is \f(CW\*(C`$ical\->sec\*(C'\fR 47?\*(L" \*(R"Is \f(CW\*(C`$ical\->min\*(C'\fR 12?" With \f(CW\*(C`is()\*(C'\fR in place,you get some more information.PP.Vb 10\& 1..8\& ok 1 \- new() returned something\& ok 2 \- and it\*(Aqs the right class\& ok 3 \- sec()\& ok 4 \- min()\& ok 5 \- hour()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -