⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tutorial.pod

📁 SinFP是一种新的识别对方计算机操作系统类型的工具
💻 POD
📖 第 1 页 / 共 2 页
字号:
First, we can calculate the plan dynamically using the C<plan()>function.    use Test::More;    use Date::ICal;    my %ICal_Dates = (        ...same as before...    );    # For each key in the hash we're running 8 tests.    plan tests => keys %ICal_Dates * 8;Or to be even more flexible, we use C<no_plan>.  This means we're justrunning some tests, don't know how many. [6]    use Test::More 'no_plan';   # instead of tests => 32now we can just add tests and not have to do all sorts of math tofigure out how many we're running.=head2 Informative namesTake a look at this line here    ok( defined $ical,            "new(ical => '$ical_str')" );we've added more detail about what we're testing and the ICal stringitself we're trying out to the name.  So you get results like:    ok 25 - new(ical => '19971024T120000')    ok 26 -   and it's the right class    ok 27 -   year()    ok 28 -   month()    ok 29 -   day()    ok 30 -   hour()    ok 31 -   min()    ok 32 -   sec()if something in there fails, you'll know which one it was and thatwill make tracking down the problem easier.  So try to put a bit ofdebugging information into the test names.Describe what the tests test, to make debugging a failed test easierfor you or for the next person who runs your test.=head2 Skipping testsPoking around in the existing Date::ICal tests, I found this inF<t/01sanity.t> [7]    #!/usr/bin/perl -w    use Test::More tests => 7;    use Date::ICal;    # Make sure epoch time is being handled sanely.    my $t1 = Date::ICal->new( epoch => 0 );    is( $t1->epoch, 0,          "Epoch time of 0" );    # XXX This will only work on unix systems.    is( $t1->ical, '19700101Z', "  epoch to ical" );    is( $t1->year,  1970,       "  year()"  );    is( $t1->month, 1,          "  month()" );    is( $t1->day,   1,          "  day()"   );    # like the tests above, but starting with ical instead of epoch    my $t2 = Date::ICal->new( ical => '19700101Z' );    is( $t2->ical, '19700101Z', "Start of epoch in ICal notation" );    is( $t2->epoch, 0,          "  and back to ICal" );The beginning of the epoch is different on most non-Unix operatingsystems [8].  Even though Perl smooths out the differences for the mostpart, certain ports do it differently.  MacPerl is one off the top ofmy head. [9] We I<know> this will never work on MacOS.  So rather thanjust putting a comment in the test, we can explicitly say it's nevergoing to work and skip the test.    use Test::More tests => 7;    use Date::ICal;    # Make sure epoch time is being handled sanely.    my $t1 = Date::ICal->new( epoch => 0 );    is( $t1->epoch, 0,          "Epoch time of 0" );    SKIP: {        skip('epoch to ICal not working on MacOS', 6)             if $^O eq 'MacOS';        is( $t1->ical, '19700101Z', "  epoch to ical" );        is( $t1->year,  1970,       "  year()"  );        is( $t1->month, 1,          "  month()" );        is( $t1->day,   1,          "  day()"   );        # like the tests above, but starting with ical instead of epoch        my $t2 = Date::ICal->new( ical => '19700101Z' );        is( $t2->ical, '19700101Z', "Start of epoch in ICal notation" );        is( $t2->epoch, 0,          "  and back to ICal" );    }A little bit of magic happens here.  When running on anything butMacOS, all the tests run normally.  But when on MacOS, C<skip()> causesthe entire contents of the SKIP block to be jumped over.  It's neverrun.  Instead, it prints special output that tells Test::Harness thatthe tests have been skipped.    1..7    ok 1 - Epoch time of 0    ok 2 # skip epoch to ICal not working on MacOS    ok 3 # skip epoch to ICal not working on MacOS    ok 4 # skip epoch to ICal not working on MacOS    ok 5 # skip epoch to ICal not working on MacOS    ok 6 # skip epoch to ICal not working on MacOS    ok 7 # skip epoch to ICal not working on MacOSThis means your tests won't fail on MacOS.  This means less emailsfrom MacPerl users telling you about failing tests that you know willnever work.  You've got to be careful with skip tests.  These are fortests which don't work and I<never will>.  It is not for skippinggenuine bugs (we'll get to that in a moment).The tests are wholly and completely skipped. [10]  This will work.    SKIP: {        skip("I don't wanna die!");        die, die, die, die, die;    }=head2 Todo testsThumbing through the Date::ICal man page, I came across this:   ical       $ical_string = $ical->ical;   Retrieves, or sets, the date on the object, using any   valid ICal date/time string."Retrieves or sets".  Hmmm, didn't see a test for using C<ical()> to setthe date in the Date::ICal test suite.  So I'll write one.    use Test::More tests => 1;    use Date::ICal;    my $ical = Date::ICal->new;    $ical->ical('20201231Z');    is( $ical->ical, '20201231Z',   'Setting via ical()' );run that and I get    1..1    not ok 1 - Setting via ical()    #     Failed test (- at line 6)    #          got: '20010814T233649Z'    #     expected: '20201231Z'    # Looks like you failed 1 tests of 1.Whoops!  Looks like it's unimplemented.  Let's assume we don't havethe time to fix this. [11] Normally, you'd just comment out the testand put a note in a todo list somewhere.  Instead, we're going toexplicitly state "this test will fail" by wrapping it in a C<TODO> block.    use Test::More tests => 1;    TODO: {        local $TODO = 'ical($ical) not yet implemented';        my $ical = Date::ICal->new;        $ical->ical('20201231Z');        is( $ical->ical, '20201231Z',   'Setting via ical()' );    }Now when you run, it's a little different:    1..1    not ok 1 - Setting via ical() # TODO ical($ical) not yet implemented    #          got: '20010822T201551Z'    #     expected: '20201231Z'Test::More doesn't say "Looks like you failed 1 tests of 1".  That '#TODO' tells Test::Harness "this is supposed to fail" and it treats afailure as a successful test.  So you can write tests even beforeyou've fixed the underlying code.If a TODO test passes, Test::Harness will report it "UNEXPECTEDLYSUCCEEDED".  When that happens, you simply remove the TODO block withC<local $TODO> and turn it into a real test.=head2 Testing with taint mode.Taint mode is a funny thing.  It's the globalest of all globalfeatures.  Once you turn it on, it affects I<all> code in your programand I<all> modules used (and all the modules they use).  If a singlepiece of code isn't taint clean, the whole thing explodes.  With thatin mind, it's very important to ensure your module works under taintmode.It's very simple to have your tests run under taint mode.  Just throwa C<-T> into the C<#!> line.  Test::Harness will read the switchesin C<#!> and use them to run your tests.    #!/usr/bin/perl -Tw    ...test normally here...So when you say C<make test> it will be run with taint mode andwarnings on.=head1 FOOTNOTES=over 4=item 1The first number doesn't really mean anything, but it has to be 1.It's the second number that's important.=item 2For those following along at home, I'm using version 1.31.  It hassome bugs, which is good -- we'll uncover them with our tests.=item 3You can actually take this one step further and test the manualitself.  Have a look at B<Test::Inline> (formerly B<Pod::Tests>).=item 4Yes, there's a mistake in the test suite.  What!  Me, contrived?=item 5We'll get to testing the contents of lists later.=item 6But what happens if your test program dies halfway through?!  Since wedidn't say how many tests we're going to run, how can we know itfailed?  No problem, Test::More employs some magic to catch that deathand turn the test into a failure, even if every test passed up to thatpoint.=item 7I cleaned it up a little.=item 8Most Operating Systems record time as the number of seconds since acertain date.  This date is the beginning of the epoch.  Unix's startsat midnight January 1st, 1970 GMT.=item 9MacOS's epoch is midnight January 1st, 1904.  VMS's is midnight,November 17th, 1858, but vmsperl emulates the Unix epoch so it's not aproblem.=item 10As long as the code inside the SKIP block at least compiles.  Pleasedon't ask how.  No, it's not a filter.=item 11Do NOT be tempted to use TODO tests as a way to avoid fixing simplebugs!=back=head1 AUTHORSMichael G Schwern E<lt>schwern@pobox.comE<gt> and the perl-qa dancers!=head1 COPYRIGHTCopyright 2001 by Michael G Schwern E<lt>schwern@pobox.comE<gt>.This documentation is free; you can redistribute it and/or modify itunder the same terms as Perl itself.Irrespective of its distribution, all code examples in these filesare hereby placed into the public domain.  You are permitted andencouraged to use this code in your own programs for funor for profit as you see fit.  A simple comment in the code givingcredit would be courteous but is not required.=cut

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -