more.html
来自「perl教程」· HTML 代码 · 共 1,178 行 · 第 1/5 页
HTML
1,178 行
<dd>
<pre>
<span class="variable">ok</span><span class="operator">(</span> <span class="keyword">grep</span><span class="operator">(</span><span class="regex">/foo/</span><span class="operator">,</span> <span class="variable">@users</span><span class="operator">),</span> <span class="string">"There's a foo user"</span> <span class="operator">)</span> <span class="keyword">or</span>
<span class="variable">diag</span><span class="operator">(</span><span class="string">"Since there's no foo, check that /etc/bar is set up right"</span><span class="operator">);</span>
</pre>
</dd>
<dd>
<p>which would produce:</p>
</dd>
<dd>
<pre>
not ok 42 - There's a foo user
# Failed test 'There's a foo user'
# in foo.t at line 52.
# Since there's no foo, check that /etc/bar is set up right.</pre>
</dd>
<dd>
<p>You might remember <a href="#item_ok"><code>ok() or diag()</code></a> with the mnemonic <a href="../../lib/Pod/perlfunc.html#item_open"><code>open() or
die()</code></a>.</p>
</dd>
<dd>
<p><strong>NOTE</strong> The exact formatting of the diagnostic output is still
changing, but it is guaranteed that whatever you throw at it it won't
interfere with the test.</p>
</dd>
</dl>
<p>
</p>
<h2><a name="conditional_tests">Conditional tests</a></h2>
<p>Sometimes running a test under certain conditions will cause the
test script to die. A certain function or method isn't implemented
(such as <a href="../../lib/Pod/perlfunc.html#item_fork"><code>fork()</code></a> on MacOS), some resource isn't available (like a
net connection) or a module isn't available. In these cases it's
necessary to skip tests, or declare that they are supposed to fail
but will work in the future (a todo test).</p>
<p>For more details on the mechanics of skip and todo tests see
<a href="../../lib/Test/Harness.html">the Test::Harness manpage</a>.</p>
<p>The way Test::More handles this is with a named block. Basically, a
block of tests which can be skipped over or made todo. It's best if I
just show you...</p>
<dl>
<dt><strong><a name="item_skip_3a_block"><strong>SKIP: BLOCK</strong></a></strong>
<dd>
<pre>
<span class="variable">SKIP</span><span class="operator">:</span> <span class="operator">{</span>
<span class="variable">skip</span> <span class="variable">$why</span><span class="operator">,</span> <span class="variable">$how_many</span> <span class="keyword">if</span> <span class="variable">$condition</span><span class="operator">;</span>
</pre>
</dd>
<dd>
<pre>
...normal testing code goes here...
}</pre>
</dd>
<dd>
<p>This declares a block of tests that might be skipped, $how_many tests
there are, $why and under what $condition to skip them. An example is
the easiest way to illustrate:</p>
</dd>
<dd>
<pre>
<span class="variable">SKIP</span><span class="operator">:</span> <span class="operator">{</span>
<span class="keyword">eval</span> <span class="operator">{</span> <span class="keyword">require</span> <span class="variable">HTML::Lint</span> <span class="operator">};</span>
</pre>
</dd>
<dd>
<pre>
<span class="variable">skip</span> <span class="string">"HTML::Lint not installed"</span><span class="operator">,</span> <span class="number">2</span> <span class="keyword">if</span> <span class="variable">$@</span><span class="operator">;</span>
</pre>
</dd>
<dd>
<pre>
<span class="keyword">my</span> <span class="variable">$lint</span> <span class="operator">=</span> <span class="variable">new</span> <span class="variable">HTML::Lint</span><span class="operator">;</span>
<span class="variable">isa_ok</span><span class="operator">(</span> <span class="variable">$lint</span><span class="operator">,</span> <span class="string">"HTML::Lint"</span> <span class="operator">);</span>
</pre>
</dd>
<dd>
<pre>
<span class="variable">$lint</span><span class="operator">-></span><span class="variable">parse</span><span class="operator">(</span> <span class="variable">$html</span> <span class="operator">);</span>
<span class="variable">is</span><span class="operator">(</span> <span class="variable">$lint</span><span class="operator">-></span><span class="variable">errors</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="string">"No errors found in HTML"</span> <span class="operator">);</span>
<span class="operator">}</span>
</pre>
</dd>
<dd>
<p>If the user does not have HTML::Lint installed, the whole block of
code <em>won't be run at all</em>. Test::More will output special ok's
which Test::Harness interprets as skipped, but passing, tests.</p>
</dd>
<dd>
<p>It's important that $how_many accurately reflects the number of tests
in the SKIP block so the # of tests run will match up with your plan.
If your plan is <code>no_plan</code> $how_many is optional and will default to 1.</p>
</dd>
<dd>
<p>It's perfectly safe to nest SKIP blocks. Each SKIP block must have
the label <code>SKIP</code>, or Test::More can't work its magic.</p>
</dd>
<dd>
<p>You don't skip tests which are failing because there's a bug in your
program, or for which you don't yet have code written. For that you
use TODO. Read on.</p>
</dd>
<dt><strong><a name="item_todo_3a_block"><strong>TODO: BLOCK</strong></a></strong>
<dd>
<pre>
<span class="variable">TODO</span><span class="operator">:</span> <span class="operator">{</span>
<span class="keyword">local</span> <span class="variable">$TODO</span> <span class="operator">=</span> <span class="variable">$why</span> <span class="keyword">if</span> <span class="variable">$condition</span><span class="operator">;</span>
</pre>
</dd>
<dd>
<pre>
...normal testing code goes here...
}</pre>
</dd>
<dd>
<p>Declares a block of tests you expect to fail and $why. Perhaps it's
because you haven't fixed a bug or haven't finished a new feature:</p>
</dd>
<dd>
<pre>
<span class="variable">TODO</span><span class="operator">:</span> <span class="operator">{</span>
<span class="keyword">local</span> <span class="variable">$TODO</span> <span class="operator">=</span> <span class="string">"URI::Geller not finished"</span><span class="operator">;</span>
</pre>
</dd>
<dd>
<pre>
<span class="keyword">my</span> <span class="variable">$card</span> <span class="operator">=</span> <span class="string">"Eight of clubs"</span><span class="operator">;</span>
<span class="variable">is</span><span class="operator">(</span> <span class="variable">URI::Geller</span><span class="operator">-></span><span class="variable">your_card</span><span class="operator">,</span> <span class="variable">$card</span><span class="operator">,</span> <span class="string">'Is THIS your card?'</span> <span class="operator">);</span>
</pre>
</dd>
<dd>
<pre>
<span class="keyword">my</span> <span class="variable">$spoon</span><span class="operator">;</span>
<span class="variable">URI::Geller</span><span class="operator">-></span><span class="variable">bend_spoon</span><span class="operator">;</span>
<span class="variable">is</span><span class="operator">(</span> <span class="variable">$spoon</span><span class="operator">,</span> <span class="string">'bent'</span><span class="operator">,</span> <span class="string">"Spoon bending, that's original"</span> <span class="operator">);</span>
<span class="operator">}</span>
</pre>
</dd>
<dd>
<p>With a todo block, the tests inside are expected to fail. Test::More
will run the tests normally, but print out special flags indicating
they are "todo". Test::Harness will interpret failures as being ok.
Should anything succeed, it will report it as an unexpected success.
You then know the thing you had todo is done and can remove the
TODO flag.</p>
</dd>
<dd>
<p>The nice part about todo tests, as opposed to simply commenting out a
block of tests, is it's like having a programmatic todo list. You know
how much work is left to be done, you're aware of what bugs there are,
and you'll know immediately when they're fixed.</p>
</dd>
<dd>
<p>Once a todo test starts succeeding, simply move it outside the block.
When the block is empty, delete it.</p>
</dd>
<dd>
<p><strong>NOTE</strong>: TODO tests require a Test::Harness upgrade else it will
treat it as a normal failure. See <a href="#caveats_and_notes">CAVEATS and NOTES</a>).</p>
</dd>
<dt><strong><a name="item_todo_skip"><strong>todo_skip</strong></a></strong>
<dd>
<pre>
<span class="variable">TODO</span><span class="operator">:</span> <span class="operator">{</span>
<span class="variable">todo_skip</span> <span class="variable">$why</span><span class="operator">,</span> <span class="variable">$how_many</span> <span class="keyword">if</span> <span class="variable">$condition</span><span class="operator">;</span>
</pre>
</dd>
<dd>
<pre>
...normal testing code...
}</pre>
</dd>
<dd>
<p>With todo tests, it's best to have the tests actually run. That way
you'll know when they start passing. Sometimes this isn't possible.
Often a failing test will cause the whole program to die or hang, even
inside an <a href="../../lib/Pod/perlfunc.html#item_eval"><code>eval BLOCK</code></a> with and using <a href="../../lib/Pod/perlfunc.html#item_alarm"><code>alarm</code></a>. In these extreme
cases you have no choice but to skip over the broken tests entirely.</p>
</dd>
<dd>
<p>The syntax and behavior is similar to a <a href="#item_skip_3a_block"><code>SKIP: BLOCK</code></a> except the
tests will be marked as failing but todo. Test::Harness will
interpret them as passing.</p>
</dd>
<dt><strong><a name="item_when_do_i_use_skip_vs_2e_todo_3f">When do I use SKIP vs. TODO?</a></strong>
<dd>
<p><strong>If it's something the user might not be able to do</strong>, use SKIP.
This includes optional modules that aren't installed, running under
an OS that doesn't have some feature (like <a href="../../lib/Pod/perlfunc.html#item_fork"><code>fork()</code></a> or symlinks), or maybe
you need an Internet connection and one isn't available.</p>
</dd>
<dd>
<p><strong>If it's something the programmer hasn't done yet</strong>, use TODO. This
is for any code you haven't written yet, or bugs you have yet to fix,
but want to put tests in your testing script (always a good idea).</p>
</dd>
</li>
</dl>
<p>
</p>
<h2><a name="test_control">Test control</a></h2>
<dl>
<dt><strong><a name="item_bail_out"><strong>BAIL_OUT</strong></a></strong>
<dd>
<pre>
<span class="variable">BAIL_OUT</span><span class="operator">(</span><span class="variable">$reason</span><span class="operator">);</span>
</pre>
</dd>
<dd>
<p>Incidates to the harness that things are going so badly all testing
should terminate. This includes the running any additional test scripts.</p>
</dd>
<dd>
<p>This is typically used when testing cannot continue such as a critical
module failing to compile or a necessary external utility not being
available such as a database connection failing.</p>
</dd>
<dd>
<p>The test will exit with 255.</p>
</dd>
</dl>
<p>
</p>
<h2><a name="discouraged_comparison_functions">Discouraged comparison functions</a></h2>
<p>The use of the following functions is discouraged as they are not
actually testing functions and produce no diagnostics to help figure
out what went wrong. They were written before <a href="#item_is_deeply"><code>is_deeply()</code></a> existed
because I couldn't figure out how to display a useful diff of two
arbitrary data structures.</p>
<p>These functions are usually used inside an ok().</p>
<pre>
<span class="variable">ok</span><span class="operator">(</span> <span class="variable">eq_array</span><span class="operator">(\</span><span class="variable">@this</span><span class="operator">,</span> <span class="operator">\</span><span class="variable">@that</span><span class="operator">)</span> <span class="operator">);</span>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?