tutorial.html

来自「perl教程」· HTML 代码 · 共 648 行 · 第 1/4 页

HTML
648
字号
<?xml version="1.0" ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<!-- saved from url=(0017)http://localhost/ -->
<script language="JavaScript" src="../../displayToc.js"></script>
<script language="JavaScript" src="../../tocParas.js"></script>
<script language="JavaScript" src="../../tocTab.js"></script>
<link rel="stylesheet" type="text/css" href="../../scineplex.css">
<title>Test::Tutorial - A tutorial about writing really basic tests</title>
<link rel="stylesheet" href="../../Active.css" type="text/css" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rev="made" href="mailto:" />
</head>

<body>

<script>writelinks('__top__',2);</script>
<h1><a>Test::Tutorial - A tutorial about writing really basic tests</a></h1>
<p><a name="__index__"></a></p>

<!-- INDEX BEGIN -->

<ul>

	<li><a href="#name">NAME</a></li>
	<li><a href="#description">DESCRIPTION</a></li>
	<ul>

		<li><a href="#nuts_and_bolts_of_testing_">Nuts and bolts of testing.</a></li>
		<li><a href="#where_to_start">Where to start?</a></li>
		<li><a href="#names">Names</a></li>
		<li><a href="#test_the_manual">Test the manual</a></li>
		<li><a href="#sometimes_the_tests_are_wrong">Sometimes the tests are wrong</a></li>
		<li><a href="#testing_lots_of_values">Testing lots of values</a></li>
		<li><a href="#informative_names">Informative names</a></li>
		<li><a href="#skipping_tests">Skipping tests</a></li>
		<li><a href="#todo_tests">Todo tests</a></li>
		<li><a href="#testing_with_taint_mode_">Testing with taint mode.</a></li>
	</ul>

	<li><a href="#footnotes">FOOTNOTES</a></li>
	<li><a href="#authors">AUTHORS</a></li>
	<li><a href="#copyright">COPYRIGHT</a></li>
</ul>
<!-- INDEX END -->

<hr />
<p>
</p>
<h1><a name="name">NAME</a></h1>
<p>Test::Tutorial - A tutorial about writing really basic tests</p>
<p>
</p>
<hr />
<h1><a name="description">DESCRIPTION</a></h1>
<p><em>AHHHHHHH!!!!  NOT TESTING!  Anything but testing!  
Beat me, whip me, send me to Detroit, but don't make 
me write tests!</em></p>
<p><em>*sob*</em></p>
<p><em>Besides, I don't know how to write the damned things.</em></p>
<p>Is this you?  Is writing tests right up there with writing
documentation and having your fingernails pulled out?  Did you open up
a test and read</p>
<pre>
    <span class="comment">######## We start with some black magic</span>
</pre>
<p>and decide that's quite enough for you?</p>
<p>It's ok.  That's all gone now.  We've done all the black magic for
you.  And here are the tricks...</p>
<p>
</p>
<h2><a name="nuts_and_bolts_of_testing_">Nuts and bolts of testing.</a></h2>
<p>Here's the most basic test program.</p>
<pre>
    <span class="comment">#!/usr/bin/perl -w</span>
</pre>
<pre>
    <span class="keyword">print</span> <span class="string">"1..1\n"</span><span class="operator">;</span>
</pre>
<pre>
    <span class="keyword">print</span> <span class="number">1</span> <span class="operator">+</span> <span class="number">1</span> <span class="operator">==</span> <span class="number">2</span> <span class="operator">?</span> <span class="string">"ok 1\n"</span> <span class="operator">:</span> <span class="string">"not ok 1\n"</span><span class="operator">;</span>
</pre>
<p>since 1 + 1 is 2, it prints:</p>
<pre>
    1..1
    ok 1</pre>
<p>What this says is: <code>1..1</code> &quot;I'm going to run one test.&quot; [1] <code>ok 1</code>
&quot;The first test passed&quot;.  And that's about all magic there is to
testing.  Your basic unit of testing is the <em>ok</em>.  For each thing you
test, an <code>ok</code> is printed.  Simple.  <strong>Test::Harness</strong> interprets your test
results to determine if you succeeded or failed (more on that later).</p>
<p>Writing all these print statements rapidly gets tedious.  Fortunately,
there's <strong>Test::Simple</strong>.  It has one function, <code>ok()</code>.</p>
<pre>
    <span class="comment">#!/usr/bin/perl -w</span>
</pre>
<pre>
    <span class="keyword">use</span> <span class="variable">Test::Simple</span> <span class="string">tests</span> <span class="operator">=&gt;</span> <span class="number">1</span><span class="operator">;</span>
</pre>
<pre>
    <span class="variable">ok</span><span class="operator">(</span> <span class="number">1</span> <span class="operator">+</span> <span class="number">1</span> <span class="operator">==</span> <span class="number">2</span> <span class="operator">);</span>
</pre>
<p>and that does the same thing as the code above.  <code>ok()</code> is the backbone
of Perl testing, and we'll be using it instead of roll-your-own from
here on.  If <code>ok()</code> gets a true value, the test passes.  False, it
fails.</p>
<pre>
    <span class="comment">#!/usr/bin/perl -w</span>
</pre>
<pre>
    <span class="keyword">use</span> <span class="variable">Test::Simple</span> <span class="string">tests</span> <span class="operator">=&gt;</span> <span class="number">2</span><span class="operator">;</span>
    <span class="variable">ok</span><span class="operator">(</span> <span class="number">1</span> <span class="operator">+</span> <span class="number">1</span> <span class="operator">==</span> <span class="number">2</span> <span class="operator">);</span>
    <span class="variable">ok</span><span class="operator">(</span> <span class="number">2</span> <span class="operator">+</span> <span class="number">2</span> <span class="operator">==</span> <span class="number">5</span> <span class="operator">);</span>
</pre>
<p>from that comes</p>
<pre>
    1..2
    ok 1
    not ok 2
    #     Failed test (test.pl at line 5)
    # Looks like you failed 1 tests of 2.</pre>
<p><code>1..2</code> &quot;I'm going to run two tests.&quot;  This number is used to ensure
your test program ran all the way through and didn't die or skip some
tests.  <code>ok 1</code> &quot;The first test passed.&quot;  <code>not ok 2</code> &quot;The second test
failed&quot;.  Test::Simple helpfully prints out some extra commentary about
your tests.</p>
<p>It's not scary.  Come, hold my hand.  We're going to give an example
of testing a module.  For our example, we'll be testing a date
library, <strong>Date::ICal</strong>.  It's on CPAN, so download a copy and follow
along. [2]</p>
<p>
</p>
<h2><a name="where_to_start">Where to start?</a></h2>
<p>This is the hardest part of testing, where do you start?  People often
get overwhelmed at the apparent enormity of the task of testing a
whole module.  Best place to start is at the beginning.  Date::ICal is
an object-oriented module, and that means you start by making an
object.  So we test <code>new()</code>.</p>
<pre>
    <span class="comment">#!/usr/bin/perl -w</span>
</pre>
<pre>
    <span class="keyword">use</span> <span class="variable">Test::Simple</span> <span class="string">tests</span> <span class="operator">=&gt;</span> <span class="number">2</span><span class="operator">;</span>
</pre>
<pre>
    <span class="keyword">use</span> <span class="variable">Date::ICal</span><span class="operator">;</span>
</pre>
<pre>
    <span class="keyword">my</span> <span class="variable">$ical</span> <span class="operator">=</span> <span class="variable">Date::ICal</span><span class="operator">-&gt;</span><span class="variable">new</span><span class="operator">;</span>         <span class="comment"># create an object</span>
    <span class="variable">ok</span><span class="operator">(</span> <span class="keyword">defined</span> <span class="variable">$ical</span> <span class="operator">);</span>                <span class="comment"># check that we got something</span>
    <span class="variable">ok</span><span class="operator">(</span> <span class="variable">$ical</span><span class="operator">-&gt;</span><span class="variable">isa</span><span class="operator">(</span><span class="string">'Date::ICal'</span><span class="operator">)</span> <span class="operator">);</span>     <span class="comment"># and it's the right class</span>
</pre>
<p>run that and you should get:</p>
<pre>
    1..2
    ok 1
    ok 2</pre>
<p>congratulations, you've written your first useful test.</p>
<p>
</p>
<h2><a name="names">Names</a></h2>

⌨️ 快捷键说明

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