{
    "mode": "perldoc",
    "parameter": "Test::Tutorial",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/Test%3A%3ATutorial/json",
    "generated": "2026-06-09T11:34:22Z",
    "sections": {
        "NAME": {
            "content": "Test::Tutorial - A tutorial about writing really basic tests\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "*AHHHHHHH!!!! NOT TESTING! Anything but testing! Beat me, whip me, send me to Detroit, but don't\nmake me write tests!*\n",
            "subsections": [
                {
                    "name": "sob",
                    "content": "*Besides, I don't know how to write the damned things.*\n\nIs this you? Is writing tests right up there with writing documentation and having your\nfingernails pulled out? Did you open up a test and read\n\n######## We start with some black magic\n\nand decide that's quite enough for you?\n\nIt's ok. That's all gone now. We've done all the black magic for you. And here are the tricks...\n"
                },
                {
                    "name": "Nuts and bolts of testing.",
                    "content": "Here's the most basic test program.\n\n#!/usr/bin/perl -w\n\nprint \"1..1\\n\";\n\nprint 1 + 1 == 2 ? \"ok 1\\n\" : \"not ok 1\\n\";\n\nBecause 1 + 1 is 2, it prints:\n\n1..1\nok 1\n\nWhat this says is: 1..1 \"I'm going to run one test.\" [1] \"ok 1\" \"The first test passed\". And\nthat's about all magic there is to testing. Your basic unit of testing is the *ok*. For each\nthing you test, an \"ok\" is printed. Simple. Test::Harness interprets your test results to\ndetermine if you succeeded or failed (more on that later).\n\nWriting all these print statements rapidly gets tedious. Fortunately, there's Test::Simple. It\nhas one function, \"ok()\".\n\n#!/usr/bin/perl -w\n\nuse Test::Simple tests => 1;\n\nok( 1 + 1 == 2 );\n\nThat does the same thing as the previous code. \"ok()\" is the backbone of Perl testing, and we'll\nbe using it instead of roll-your-own from here on. If \"ok()\" gets a true value, the test passes.\nFalse, it fails.\n\n#!/usr/bin/perl -w\n\nuse Test::Simple tests => 2;\nok( 1 + 1 == 2 );\nok( 2 + 2 == 5 );\n\nFrom that comes:\n\n1..2\nok 1\nnot ok 2\n#     Failed test (test.pl at line 5)\n# Looks like you failed 1 tests of 2.\n\n1..2 \"I'm going to run two tests.\" This number is a *plan*. It helps to ensure your test program\nran all the way through and didn't die or skip some tests. \"ok 1\" \"The first test passed.\" \"not\nok 2\" \"The second test failed\". Test::Simple helpfully prints out some extra commentary about\nyour tests.\n\nIt's not scary. Come, hold my hand. We're going to give an example of testing a module. For our\nexample, we'll be testing a date library, Date::ICal. It's on CPAN, so download a copy and\nfollow along. [2]\n\nWhere to start?\nThis is the hardest part of testing, where do you start? People often get overwhelmed at the\napparent enormity of the task of testing a whole module. The best place to start is at the\nbeginning. Date::ICal is an object-oriented module, and that means you start by making an\nobject. Test \"new()\".\n\n#!/usr/bin/perl -w\n\n# assume these two lines are in all subsequent examples\nuse strict;\nuse warnings;\n\nuse Test::Simple tests => 2;\n\nuse Date::ICal;\n\nmy $ical = Date::ICal->new;         # create an object\nok( defined $ical );                # check that we got something\nok( $ical->isa('Date::ICal') );     # and it's the right class\n\nRun that and you should get:\n\n1..2\nok 1\nok 2\n\nCongratulations! You've written your first useful test.\n"
                },
                {
                    "name": "Names",
                    "content": "That output isn't terribly descriptive, is it? When you have two tests you can figure out which\none is #2, but what if you have 102 tests?\n\nEach test can be given a little descriptive name as the second argument to \"ok()\".\n\nuse Test::Simple tests => 2;\n\nok( defined $ical,              'new() returned something' );\nok( $ical->isa('Date::ICal'),   \"  and it's the right class\" );\n\nNow you'll see:\n\n1..2\nok 1 - new() returned something\nok 2 -   and it's the right class\n"
                },
                {
                    "name": "Test the manual",
                    "content": "The simplest way to build up a decent testing suite is to just test what the manual says it\ndoes. [3] Let's pull something out of the \"SYNOPSIS\" in Date::ICal and test that all its bits\nwork.\n\n#!/usr/bin/perl -w\n\nuse Test::Simple tests => 8;\n\nuse Date::ICal;\n\n$ical = Date::ICal->new( year => 1964, month => 10, day => 16,\nhour => 16,   min   => 12, sec => 47,\ntz   => '0530' );\n\nok( defined $ical,            'new() returned something' );\nok( $ical->isa('Date::ICal'), \"  and it's the right class\" );\nok( $ical->sec   == 47,       '  sec()'   );\nok( $ical->min   == 12,       '  min()'   );\nok( $ical->hour  == 16,       '  hour()'  );\nok( $ical->day   == 17,       '  day()'   );\nok( $ical->month == 10,       '  month()' );\nok( $ical->year  == 1964,     '  year()'  );\n\nRun that and you get:\n\n1..8\nok 1 - new() returned something\nok 2 -   and it's the right class\nok 3 -   sec()\nok 4 -   min()\nok 5 -   hour()\nnot ok 6 -   day()\n#     Failed test (- at line 16)\nok 7 -   month()\nok 8 -   year()\n# Looks like you failed 1 tests of 8.\n\nWhoops, a failure! [4] Test::Simple helpfully lets us know on what line the failure occurred,\nbut not much else. We were supposed to get 17, but we didn't. What did we get?? Dunno. You could\nre-run the test in the debugger or throw in some print statements to find out.\n\nInstead, switch from Test::Simple to Test::More. Test::More does everything Test::Simple does,\nand more! In fact, Test::More does things *exactly* the way Test::Simple does. You can literally\nswap Test::Simple out and put Test::More in its place. That's just what we're going to do.\n\nTest::More does more than Test::Simple. The most important difference at this point is it\nprovides more informative ways to say \"ok\". Although you can write almost any test with a\ngeneric \"ok()\", it can't tell you what went wrong. The \"is()\" function lets us declare that\nsomething is supposed to be the same as something else:\n\nuse Test::More tests => 8;\n\nuse Date::ICal;\n\n$ical = Date::ICal->new( year => 1964, month => 10, day => 16,\nhour => 16,   min   => 12, sec => 47,\ntz   => '0530' );\n\nok( defined $ical,            'new() returned something' );\nok( $ical->isa('Date::ICal'), \"  and it's the right class\" );\nis( $ical->sec,     47,       '  sec()'   );\nis( $ical->min,     12,       '  min()'   );\nis( $ical->hour,    16,       '  hour()'  );\nis( $ical->day,     17,       '  day()'   );\nis( $ical->month,   10,       '  month()' );\nis( $ical->year,    1964,     '  year()'  );\n\n\"Is \"$ical->sec\" 47?\" \"Is \"$ical->min\" 12?\" With \"is()\" in place, you get more information:\n\n1..8\nok 1 - new() returned something\nok 2 -   and it's the right class\nok 3 -   sec()\nok 4 -   min()\nok 5 -   hour()\nnot ok 6 -   day()\n#     Failed test (- at line 16)\n#          got: '16'\n#     expected: '17'\nok 7 -   month()\nok 8 -   year()\n# Looks like you failed 1 tests of 8.\n\nAha. \"$ical->day\" returned 16, but we expected 17. A quick check shows that the code is working\nfine, we made a mistake when writing the tests. Change it to:\n\nis( $ical->day,     16,       '  day()'   );\n\n... and everything works.\n\nAny time you're doing a \"this equals that\" sort of test, use \"is()\". It even works on arrays.\nThe test is always in scalar context, so you can test how many elements are in an array this\nway. [5]\n\nis( @foo, 5, 'foo has 5 elements' );\n"
                },
                {
                    "name": "Sometimes the tests are wrong",
                    "content": "This brings up a very important lesson. Code has bugs. Tests are code. Ergo, tests have bugs. A\nfailing test could mean a bug in the code, but don't discount the possibility that the test is\nwrong.\n\nOn the flip side, don't be tempted to prematurely declare a test incorrect just because you're\nhaving trouble finding the bug. Invalidating a test isn't something to be taken lightly, and\ndon't use it as a cop out to avoid work.\n"
                },
                {
                    "name": "Testing lots of values",
                    "content": "We're going to be wanting to test a lot of dates here, trying to trick the code with lots of\ndifferent edge cases. Does it work before 1970? After 2038? Before 1904? Do years after 10,000\ngive it trouble? Does it get leap years right? We could keep repeating the code above, or we\ncould set up a little try/expect loop.\n\nuse Test::More tests => 32;\nuse Date::ICal;\n\nmy %ICalDates = (\n# An ICal string     And the year, month, day\n#                    hour, minute and second we expect.\n'19971024T120000' =>    # from the docs.\n[ 1997, 10, 24, 12,  0,  0 ],\n'20390123T232832' =>    # after the Unix epoch\n[ 2039,  1, 23, 23, 28, 32 ],\n'19671225T000000' =>    # before the Unix epoch\n[ 1967, 12, 25,  0,  0,  0 ],\n'18990505T232323' =>    # before the MacOS epoch\n[ 1899,  5,  5, 23, 23, 23 ],\n);\n\n\nwhile( my($icalstr, $expect) = each %ICalDates ) {\nmy $ical = Date::ICal->new( ical => $icalstr );\n\nok( defined $ical,            \"new(ical => '$icalstr')\" );\nok( $ical->isa('Date::ICal'), \"  and it's the right class\" );\n\nis( $ical->year,    $expect->[0],     '  year()'  );\nis( $ical->month,   $expect->[1],     '  month()' );\nis( $ical->day,     $expect->[2],     '  day()'   );\nis( $ical->hour,    $expect->[3],     '  hour()'  );\nis( $ical->min,     $expect->[4],     '  min()'   );\nis( $ical->sec,     $expect->[5],     '  sec()'   );\n}\n\nNow we can test bunches of dates by just adding them to %ICalDates. Now that it's less work to\ntest with more dates, you'll be inclined to just throw more in as you think of them. Only\nproblem is, every time we add to that we have to keep adjusting the \"use Test::More tests => ##\"\nline. That can rapidly get annoying. There are ways to make this work better.\n\nFirst, we can calculate the plan dynamically using the \"plan()\" function.\n\nuse Test::More;\nuse Date::ICal;\n\nmy %ICalDates = (\n...same as before...\n);\n\n# For each key in the hash we're running 8 tests.\nplan tests => keys(%ICalDates) * 8;\n\n...and then your tests...\n\nTo be even more flexible, use \"donetesting\". This means we're just running some tests, don't\nknow how many. [6]\n\nuse Test::More;   # instead of tests => 32\n\n... # tests here\n\ndonetesting();   # reached the end safely\n\nIf you don't specify a plan, Test::More expects to see \"donetesting()\" before your program\nexits. It will warn you if you forget it. You can give \"donetesting()\" an optional number of\ntests you expected to run, and if the number ran differs, Test::More will give you another kind\nof warning.\n"
                },
                {
                    "name": "Informative names",
                    "content": "Take a look at the line:\n\nok( defined $ical,            \"new(ical => '$icalstr')\" );\n\nWe've added more detail about what we're testing and the ICal string itself we're trying out to\nthe name. So you get results like:\n\nok 25 - new(ical => '19971024T120000')\nok 26 -   and it's the right class\nok 27 -   year()\nok 28 -   month()\nok 29 -   day()\nok 30 -   hour()\nok 31 -   min()\nok 32 -   sec()\n\nIf something in there fails, you'll know which one it was and that will make tracking down the\nproblem easier. Try to put a bit of debugging information into the test names.\n\nDescribe what the tests test, to make debugging a failed test easier for you or for the next\nperson who runs your test.\n"
                },
                {
                    "name": "Skipping tests",
                    "content": "Poking around in the existing Date::ICal tests, I found this in t/01sanity.t [7]\n\n#!/usr/bin/perl -w\n\nuse Test::More tests => 7;\nuse Date::ICal;\n\n# Make sure epoch time is being handled sanely.\nmy $t1 = Date::ICal->new( epoch => 0 );\nis( $t1->epoch, 0,          \"Epoch time of 0\" );\n\n# XXX This will only work on unix systems.\nis( $t1->ical, '19700101Z', \"  epoch to ical\" );\n\nis( $t1->year,  1970,       \"  year()\"  );\nis( $t1->month, 1,          \"  month()\" );\nis( $t1->day,   1,          \"  day()\"   );\n\n# like the tests above, but starting with ical instead of epoch\nmy $t2 = Date::ICal->new( ical => '19700101Z' );\nis( $t2->ical, '19700101Z', \"Start of epoch in ICal notation\" );\n\nis( $t2->epoch, 0,          \"  and back to ICal\" );\n\nThe beginning of the epoch is different on most non-Unix operating systems [8]. Even though Perl\nsmooths out the differences for the most part, certain ports do it differently. MacPerl is one\noff the top of my head. [9] Rather than putting a comment in the test and hoping someone will\nread the test while debugging the failure, we can explicitly say it's never going to work and\nskip the test.\n\nuse Test::More tests => 7;\nuse Date::ICal;\n\n# Make sure epoch time is being handled sanely.\nmy $t1 = Date::ICal->new( epoch => 0 );\nis( $t1->epoch, 0,          \"Epoch time of 0\" );\n\nSKIP: {\nskip('epoch to ICal not working on Mac OS', 6)\nif $^O eq 'MacOS';\n\nis( $t1->ical, '19700101Z', \"  epoch to ical\" );\n\nis( $t1->year,  1970,       \"  year()\"  );\nis( $t1->month, 1,          \"  month()\" );\nis( $t1->day,   1,          \"  day()\"   );\n\n# like the tests above, but starting with ical instead of epoch\nmy $t2 = Date::ICal->new( ical => '19700101Z' );\nis( $t2->ical, '19700101Z', \"Start of epoch in ICal notation\" );\n\nis( $t2->epoch, 0,          \"  and back to ICal\" );\n}\n\nA little bit of magic happens here. When running on anything but MacOS, all the tests run\nnormally. But when on MacOS, \"skip()\" causes the entire contents of the SKIP block to be jumped\nover. It never runs. Instead, \"skip()\" prints special output that tells Test::Harness that the\ntests have been skipped.\n\n1..7\nok 1 - Epoch time of 0\nok 2 # skip epoch to ICal not working on MacOS\nok 3 # skip epoch to ICal not working on MacOS\nok 4 # skip epoch to ICal not working on MacOS\nok 5 # skip epoch to ICal not working on MacOS\nok 6 # skip epoch to ICal not working on MacOS\nok 7 # skip epoch to ICal not working on MacOS\n\nThis means your tests won't fail on MacOS. This means fewer emails from MacPerl users telling\nyou about failing tests that you know will never work. You've got to be careful with skip tests.\nThese are for tests which don't work and *never will*. It is not for skipping genuine bugs\n(we'll get to that in a moment).\n\nThe tests are wholly and completely skipped. [10] This will work.\n\nSKIP: {\nskip(\"I don't wanna die!\");\n\ndie, die, die, die, die;\n}\n"
                },
                {
                    "name": "Todo tests",
                    "content": "While thumbing through the Date::ICal man page, I came across this:\n\nical\n\n$icalstring = $ical->ical;\n\nRetrieves, or sets, the date on the object, using any\nvalid ICal date/time string.\n\n\"Retrieves or sets\". Hmmm. I didn't see a test for using \"ical()\" to set the date in the\nDate::ICal test suite. So I wrote one:\n\nuse Test::More tests => 1;\nuse Date::ICal;\n\nmy $ical = Date::ICal->new;\n$ical->ical('20201231Z');\nis( $ical->ical, '20201231Z',   'Setting via ical()' );\n\nRun that. I saw:\n\n1..1\nnot ok 1 - Setting via ical()\n#     Failed test (- at line 6)\n#          got: '20010814T233649Z'\n#     expected: '20201231Z'\n# Looks like you failed 1 tests of 1.\n\nWhoops! Looks like it's unimplemented. Assume you don't have the time to fix this. [11]\nNormally, you'd just comment out the test and put a note in a todo list somewhere. Instead,\nexplicitly state \"this test will fail\" by wrapping it in a \"TODO\" block:\n\nuse Test::More tests => 1;\n\nTODO: {\nlocal $TODO = 'ical($ical) not yet implemented';\n\nmy $ical = Date::ICal->new;\n$ical->ical('20201231Z');\n\nis( $ical->ical, '20201231Z',   'Setting via ical()' );\n}\n\nNow when you run, it's a little different:\n\n1..1\nnot ok 1 - Setting via ical() # TODO ical($ical) not yet implemented\n#          got: '20010822T201551Z'\n#     expected: '20201231Z'\n\nTest::More doesn't say \"Looks like you failed 1 tests of 1\". That '# TODO' tells Test::Harness\n\"this is supposed to fail\" and it treats a failure as a successful test. You can write tests\neven before you've fixed the underlying code.\n\nIf a TODO test passes, Test::Harness will report it \"UNEXPECTEDLY SUCCEEDED\". When that happens,\nremove the TODO block with \"local $TODO\" and turn it into a real test.\n"
                },
                {
                    "name": "Testing with taint mode.",
                    "content": "Taint mode is a funny thing. It's the globalest of all global features. Once you turn it on, it\naffects *all* code in your program and *all* modules used (and all the modules they use). If a\nsingle piece of code isn't taint clean, the whole thing explodes. With that in mind, it's very\nimportant to ensure your module works under taint mode.\n\nIt's very simple to have your tests run under taint mode. Just throw a \"-T\" into the \"#!\" line.\nTest::Harness will read the switches in \"#!\" and use them to run your tests.\n\n#!/usr/bin/perl -Tw\n\n...test normally here...\n\nWhen you say \"make test\" it will run with taint mode on.\n"
                }
            ]
        },
        "FOOTNOTES": {
            "content": "1   The first number doesn't really mean anything, but it has to be 1. It's the second number\nthat's important.\n\n2   For those following along at home, I'm using version 1.31. It has some bugs, which is good\n-- we'll uncover them with our tests.\n\n3   You can actually take this one step further and test the manual itself. Have a look at\nTest::Inline (formerly Pod::Tests).\n\n4   Yes, there's a mistake in the test suite. What! Me, contrived?\n\n5   We'll get to testing the contents of lists later.\n\n6   But what happens if your test program dies halfway through?! Since we didn't say how many\ntests we're going to run, how can we know it failed? No problem, Test::More employs some\nmagic to catch that death and turn the test into a failure, even if every test passed up to\nthat point.\n\n7   I cleaned it up a little.\n\n8   Most Operating Systems record time as the number of seconds since a certain date. This date\nis the beginning of the epoch. Unix's starts at midnight January 1st, 1970 GMT.\n\n9   MacOS's epoch is midnight January 1st, 1904. VMS's is midnight, November 17th, 1858, but\nvmsperl emulates the Unix epoch so it's not a problem.\n\n10  As long as the code inside the SKIP block at least compiles. Please don't ask how. No, it's\nnot a filter.\n\n11  Do NOT be tempted to use TODO tests as a way to avoid fixing simple bugs!\n",
            "subsections": []
        },
        "AUTHORS": {
            "content": "Michael G Schwern <schwern@pobox.com> and the perl-qa dancers!\n",
            "subsections": []
        },
        "MAINTAINERS": {
            "content": "Chad Granum <exodist@cpan.org>\n",
            "subsections": []
        },
        "COPYRIGHT": {
            "content": "Copyright 2001 by Michael G Schwern <schwern@pobox.com>.\n\nThis documentation is free; you can redistribute it and/or modify it under the same terms as\nPerl itself.\n\nIrrespective of its distribution, all code examples in these files are hereby placed into the\npublic domain. You are permitted and encouraged to use this code in your own programs for fun or\nfor profit as you see fit. A simple comment in the code giving credit would be courteous but is\nnot required.\n",
            "subsections": []
        }
    },
    "summary": "Test::Tutorial - A tutorial about writing really basic tests",
    "flags": [],
    "examples": [],
    "see_also": []
}