unit_test_documentation.html.svn-base
来自「PHP 知识管理系统(基于树结构的知识管理系统), 英文原版的PHP源码。」· SVN-BASE 代码 · 共 432 行 · 第 1/2 页
SVN-BASE
432 行
...will pass and normally show no message. If you have <a href="http://www.lastcraft.com/display_subclass_tutorial.php">set up the tester to display passes</a> as well then the message will be displayed as is.<pre><strong>$this->assertIdentical(0, false, 'Zero is not false [%s]');</strong></pre> This will fail as it performs a type check, as well as a comparison, between the two values. The "%s" part is replaced by the default error message that would have been shown if we had not supplied our own.<pre>$a = 1;$b = $a;<strong>$this->assertReference($a, $b);</strong></pre> Will fail as the variable <span class="new_code">$a</span> is a copy of <span class="new_code">$b</span>.<pre><strong>$this->assertPattern('/hello/i', 'Hello world');</strong></pre> This will pass as using a case insensitive match the string <span class="new_code">hello</span> is contained in <span class="new_code">Hello world</span>.<pre><strong>$this->expectError();</strong>trigger_error('Catastrophe');</pre> Here the check catches the "Catastrophe" message without checking the text and passes. This removes the error from the queue.<pre><strong>$this->expectError('Catastrophe');</strong>trigger_error('Catastrophe');</pre> The next error check tests not only the existence of the error, but also the text which, here matches so another pass. If any unchecked errors are left at the end of a test method then an exception will be reported in the test. </p> <p> Note that SimpleTest cannot catch compile time PHP errors. </p> <p> The test cases also have some convenience methods for debugging code or extending the suite... <table><tbody> <tr><td><span class="new_code">setUp()</span></td><td>Runs this before each test method</td></tr> <tr><td><span class="new_code">tearDown()</span></td><td>Runs this after each test method</td></tr> <tr><td><span class="new_code">pass()</span></td><td>Sends a test pass</td></tr> <tr><td><span class="new_code">fail()</span></td><td>Sends a test failure</td></tr> <tr><td><span class="new_code">error()</span></td><td>Sends an exception event</td></tr> <tr><td><span class="new_code">signal($type, $payload)</span></td><td>Sends a user defined message to the test reporter</td></tr> <tr><td><span class="new_code">dump($var)</span></td><td>Does a formatted <span class="new_code">print_r()</span> for quick and dirty debugging</td></tr> </tbody></table> </p> <p><a class="target" name="extending_unit"><h2>Extending test cases</h2></a></p> <p> Of course additional test methods can be added to create specific types of test case, so as to extend framework...<pre>require_once('simpletest/autorun.php');<strong>class FileTester extends UnitTestCase { function FileTester($name = false) { $this->UnitTestCase($name); } function assertFileExists($filename, $message = '%s') { $this->assertTrue( file_exists($filename), sprintf($message, 'File [$filename] existence check')); }</strong>}</pre> Here the SimpleTest library is held in a folder called <em>simpletest</em> that is local. Substitute your own path for this. </p> <p> To prevent this test case being run accidently, it is advisable to mark it as <span class="new_code">abstract</span>. </p> <p> Alternatively you could add a <span class="new_code">SimpleTestOptions::ignore('FileTester');</span> directive in your code. </p> <p> This new case can be now be inherited just like a normal test case...<pre>class FileTestCase extends <strong>FileTester</strong> { function setUp() { @unlink('../temp/test.txt'); } function tearDown() { @unlink('../temp/test.txt'); } function testCreation() { $writer = &new FileWriter('../temp/test.txt'); $writer->write('Hello');<strong> $this->assertFileExists('../temp/test.txt');</strong> }}</pre> </p> <p> If you want a test case that does not have all of the <span class="new_code">UnitTestCase</span> assertions, only your own and a few basics, you need to extend the <span class="new_code">SimpleTestCase</span> class instead. It is found in <em>simple_test.php</em> rather than <em>unit_tester.php</em>. See <a href="group_test_documentation.html">later</a> if you want to incorporate other unit tester's test cases in your test suites. </p> <p><a class="target" name="running_unit"><h2>Running a single test case</h2></a></p> <p> You won't often run single test cases except when bashing away at a module that is having difficulty, and you don't want to upset the main test suite. With <em>autorun</em> no particular scaffolding is needed, just launch your particular test file and you're ready to go. </p> <p> You can even decide which reporter (for example, <span class="new_code">TextReporter</span> or <span class="new_code">HtmlReporter</span>) you prefer for a specific file when launched on its own...<pre><?phprequire_once('simpletest/autorun.php');<strong>SimpleTest :: prefer(new TextReporter());</strong>require_once('../classes/writer.php');class FileTestCase extends UnitTestCase { ...}?></pre> This script will run as is, but of course will output zero passes and zero failures until test methods are added. </p> </div> References and related information... <ul><li> SimpleTest project page on <a href="http://sourceforge.net/projects/simpletest/">SourceForge</a>. </li><li> SimpleTest download page on <a href="http://www.lastcraft.com/simple_test.php">LastCraft</a>. </li><li> <a href="http://simpletest.org/api/">Full API for SimpleTest</a> from the PHPDoc. </li></ul><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><div class="copyright"> Copyright<br>Marcus Baker 2006 </div></body></html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?