reporter_documentation.html.svn-base
来自「PHP 知识管理系统(基于树结构的知识管理系统), 英文原版的PHP源码。」· SVN-BASE 代码 · 共 520 行 · 第 1/2 页
SVN-BASE
520 行
<p> The reason for this is to allow for more sophistcated GUI displays that allow the selection of individual test cases. In order to build a list of possible tests they need a report on the test structure for drawing, say a tree view of the test suite. With a reporter set to dry run that just sends drawing events this is easily accomplished. </p> <p><a class="target" name="other"><h2>Extending the reporter</h2></a></p> <p> Rather than simply modifying the existing display, you might want to produce a whole new HTML look, or even generate text or XML. Rather than override every method in <span class="new_code">HtmlReporter</span> we can take one step up the class hiearchy to <span class="new_code">SimpleReporter</span> in the <em>simple_test.php</em> source file. </p> <p> A do nothing display, a blank canvas for your own creation, would be...<pre><strong>require_once('simpletest/simple_test.php');</strong>class MyDisplay extends SimpleReporter {<strong> </strong> function paintHeader($test_name) { } function paintFooter($test_name) { } function paintStart($test_name, $size) {<strong> parent::paintStart($test_name, $size);</strong> } function paintEnd($test_name, $size) {<strong> parent::paintEnd($test_name, $size);</strong> } function paintPass($message) {<strong> parent::paintPass($message);</strong> } function paintFail($message) {<strong> parent::paintFail($message);</strong> }}</pre> No output would come from this class until you add it. </p> <p><a class="target" name="cli"><h2>The command line reporter</h2></a></p> <p> SimpleTest also ships with a minimal command line reporter. The interface mimics JUnit to some extent, but paints the failure messages as they arrive. To use the command line reporter simply substitute it for the HTML version...<pre><?phprequire_once('simpletest/unit_tester.php');require_once('simpletest/reporter.php');$test = &new TestSuite('File test');$test->addTestFile('tests/file_test.php');$test->run(<strong>new TextReporter()</strong>);?></pre> Then invoke the test suite from the command line...<pre class="shell">php file_test.php</pre> You will need the command line version of PHP installed of course. A passing test suite looks like this...<pre class="shell">File testOKTest cases run: 1/1, Failures: 0, Exceptions: 0</pre> A failure triggers a display like this...<pre class="shell">File test1) True assertion failed. in createnewfileFAILURES!!!Test cases run: 1/1, Failures: 1, Exceptions: 0</pre> </p> <p> One of the main reasons for using a command line driven test suite is of using the tester as part of some automated process. To function properly in shell scripts the test script should return a non-zero exit code on failure. If a test suite fails the value <span class="new_code">false</span> is returned from the <span class="new_code">SimpleTest::run()</span> method. We can use that result to exit the script with the desired return code...<pre><?phprequire_once('simpletest/unit_tester.php');require_once('simpletest/reporter.php');$test = &new TestSuite('File test');$test->addTestFile('tests/file_test.php');<strong>exit ($test->run(new TextReporter()) ? 0 : 1);</strong>?></pre> Of course we don't really want to create two test scripts, a command line one and a web browser one, for each test suite. The command line reporter includes a method to sniff out the run time environment...<pre><?phprequire_once('simpletest/unit_tester.php');require_once('simpletest/reporter.php');$test = &new TestSuite('File test');$test->addTestFile('tests/file_test.php');<strong>if (TextReporter::inCli()) {</strong> exit ($test->run(new TextReporter()) ? 0 : 1);<strong>}</strong>$test->run(new HtmlReporter());?></pre> This is the form used within SimpleTest itself. </p> <p><a class="target" name="xml"><h2>Remote testing</h2></a></p> <p> SimpleTest ships with an <span class="new_code">XmlReporter</span> class used for internal communication. When run the output looks like...<pre class="shell"><?xml version="1.0"?><run> <group size="4"> <name>Remote tests</name> <group size="4"> <name>Visual test with 48 passes, 48 fails and 4 exceptions</name> <case> <name>testofunittestcaseoutput</name> <test> <name>testofresults</name> <pass>This assertion passed</pass> <fail>This assertion failed</fail> </test> <test> ... </test> </case> </group> </group></run></pre> You can make use of this format with the parser supplied as part of SimpleTest itself. This is called <span class="new_code">SimpleTestXmlParser</span> and resides in <em>xml.php</em> within the SimpleTest package...<pre><?phprequire_once('simpletest/xml.php'); ...$parser = &new SimpleTestXmlParser(new HtmlReporter());$parser->parse($test_output);?></pre> The <span class="new_code">$test_output</span> should be the XML format from the XML reporter, and could come from say a command line run of a test case. The parser sends events to the reporter just like any other test run. There are some odd occasions where this is actually useful. </p> <p> A problem with large test suites is thet they can exhaust the default 8Mb memory limit on a PHP process. By having the test groups output in XML and run in separate processes, the output can be reparsed to aggregate the results into a much smaller footprint top level test. </p> <p> Because the XML output can come from anywhere, this opens up the possibility of aggregating test runs from remote servers. A test case already exists to do this within the SimpleTest framework, but it is currently experimental...<pre><?php<strong>require_once('../remote.php');</strong>require_once('../reporter.php'); $test_url = ...;$dry_url = ...; $test = &new TestSuite('Remote tests');$test->addTestCase(<strong>new RemoteTestCase($test_url, $dry_url)</strong>);$test->run(new HtmlReporter());?></pre> The <span class="new_code">RemoteTestCase</span> takes the actual location of the test runner, basically a web page in XML format. It also takes the URL of a reporter set to do a dry run. This is so that progress can be reported upward correctly. The <span class="new_code">RemoteTestCase</span> can be added to test suites just like any other group test. </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> The <a href="http://simpletest.org/api/">developer's API for SimpleTest</a> gives full detail on the classes and assertions available. </li></ul><div class="menu_back"><div class="menu"><a href="index.html">SimpleTest</a> | <a href="overview.html">Overview</a> | <a href="unit_test_documentation.html">Unit tester</a> | <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> | <span class="chosen">Reporting</span> | <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 + -
显示快捷键?