unit_test_documentation.html.svn-base
来自「PHP 知识管理系统(基于树结构的知识管理系统), 英文原版的PHP源码。」· SVN-BASE 代码 · 共 432 行 · 第 1/2 页
SVN-BASE
432 行
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>SimpleTest for PHP regression test documentation</title><link rel="stylesheet" type="text/css" href="docs.css" title="Styles"></head><body><div class="menu_back"><div class="menu"><a href="index.html">SimpleTest</a> | <a href="overview.html">Overview</a> | <span class="chosen">Unit tester</span> | <a href="group_test_documentation.html">Group tests</a> | <a href="mock_objects_documentation.html">Mock objects</a> | <a href="partial_mocks_documentation.html">Partial mocks</a> | <a href="reporter_documentation.html">Reporting</a> | <a href="expectation_documentation.html">Expectations</a> | <a href="web_tester_documentation.html">Web tester</a> | <a href="form_testing_documentation.html">Testing forms</a> | <a href="authentication_documentation.html">Authentication</a> | <a href="browser_documentation.html">Scriptable browser</a></div></div><h1>PHP Unit Test documentation</h1> This page... <ul><li> <a href="#unit">Unit test cases</a> and basic assertions. </li><li> <a href="#extending_unit">Extending test cases</a> to customise them for your own project. </li><li> <a href="#running_unit">Running a single case</a> as a single script. </li></ul><div class="content"> <p><a class="target" name="unit"><h2>Unit test cases</h2></a></p> <p> The core system is a regression testing framework built around test cases. A sample test case looks like this...<pre><strong>class FileTestCase extends UnitTestCase {}</strong></pre> Actual tests are added as methods in the test case whose names by default start with the string "test" and when the test case is invoked all such methods are run in the order that PHP introspection finds them. As many test methods can be added as needed. </p> <p> For example...<pre>require_once('simpletest/autorun.php');require_once('../classes/writer.php');class FileTestCase extends UnitTestCase { function FileTestCase() { $this->UnitTestCase('File test'); }<strong> function setUp() { @unlink('../temp/test.txt'); } function tearDown() { @unlink('../temp/test.txt'); } function testCreation() { $writer = &new FileWriter('../temp/test.txt'); $writer->write('Hello'); $this->assertTrue(file_exists('../temp/test.txt'), 'File created'); }</strong>}</pre> The constructor is optional and usually omitted. Without a name, the class name is taken as the name of the test case. </p> <p> Our only test method at the moment is <span class="new_code">testCreation()</span> where we check that a file has been created by our <span class="new_code">Writer</span> object. We could have put the <span class="new_code">unlink()</span> code into this method as well, but by placing it in <span class="new_code">setUp()</span> and <span class="new_code">tearDown()</span> we can use it with other test methods that we add. </p> <p> The <span class="new_code">setUp()</span> method is run just before each and every test method. <span class="new_code">tearDown()</span> is run just after each and every test method. </p> <p> You can place some test case set up into the constructor to be run once for all the methods in the test case, but you risk test inteference that way. This way is slightly slower, but it is safer. Note that if you come from a JUnit background this will not be the behaviour you are used to. JUnit surprisingly reinstantiates the test case for each test method to prevent such interference. SimpleTest requires the end user to use <span class="new_code">setUp()</span>, but supplies additional hooks for library writers. </p> <p> The means of reporting test results (see below) are by a visiting display class that is notified by various <span class="new_code">assert...()</span> methods. Here is the full list for the <span class="new_code">UnitTestCase</span> class, the default for SimpleTest... <table><tbody> <tr><td><span class="new_code">assertTrue($x)</span></td><td>Fail if $x is false</td></tr> <tr><td><span class="new_code">assertFalse($x)</span></td><td>Fail if $x is true</td></tr> <tr><td><span class="new_code">assertNull($x)</span></td><td>Fail if $x is set</td></tr> <tr><td><span class="new_code">assertNotNull($x)</span></td><td>Fail if $x not set</td></tr> <tr><td><span class="new_code">assertIsA($x, $t)</span></td><td>Fail if $x is not the class or type $t</td></tr> <tr><td><span class="new_code">assertNotA($x, $t)</span></td><td>Fail if $x is of the class or type $t</td></tr> <tr><td><span class="new_code">assertEqual($x, $y)</span></td><td>Fail if $x == $y is false</td></tr> <tr><td><span class="new_code">assertNotEqual($x, $y)</span></td><td>Fail if $x == $y is true</td></tr> <tr><td><span class="new_code">assertWithinMargin($x, $y, $m)</span></td><td>Fail if abs($x - $y) < $m is false</td></tr> <tr><td><span class="new_code">assertOutsideMargin($x, $y, $m)</span></td><td>Fail if abs($x - $y) < $m is true</td></tr> <tr><td><span class="new_code">assertIdentical($x, $y)</span></td><td>Fail if $x == $y is false or a type mismatch</td></tr> <tr><td><span class="new_code">assertNotIdentical($x, $y)</span></td><td>Fail if $x == $y is true and types match</td></tr> <tr><td><span class="new_code">assertReference($x, $y)</span></td><td>Fail unless $x and $y are the same variable</td></tr> <tr><td><span class="new_code">assertClone($x, $y)</span></td><td>Fail unless $x and $y are identical copies</td></tr> <tr><td><span class="new_code">assertPattern($p, $x)</span></td><td>Fail unless the regex $p matches $x</td></tr> <tr><td><span class="new_code">assertNoPattern($p, $x)</span></td><td>Fail if the regex $p matches $x</td></tr> <tr><td><span class="new_code">expectError($x)</span></td><td>Swallows any upcoming matching error</td></tr> <tr><td><span class="new_code">assert($e)</span></td><td>Fail on failed <a href="expectation_documentation.html">expectation</a> object $e</td></tr> </tbody></table> All assertion methods can take an optional description as a last parameter. This is to label the displayed result with. If omitted a default message is sent instead, which is usually sufficient. This default message can still be embedded in your own message if you include "%s" within the string. All the assertions return true on a pass or false on failure. </p> <p> Some examples...<pre>$variable = null;<strong>$this->assertNull($variable, 'Should be cleared');</strong></pre>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?