<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>

<channel>
	<title>Parth Patil's Blog &#187; Testing</title>
	<atom:link href="http://parthpatil.com/category/testing/feed/" rel="self" type="application/rss+xml" />
	<link>http://parthpatil.com</link>
	<description>Progromming , Tutorials, Howtos, Hacks, Tools , Reviews etc ...</description>
	<pubDate>Thu, 15 May 2008 08:32:23 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5.1</generator>
	<language>en</language>
			<item>
		<title>Formatting PHPUnit Test Results As HTML Table</title>
		<link>http://parthpatil.com/2008/05/14/formatting-phpunit-test-results-as-html-table/</link>
		<comments>http://parthpatil.com/2008/05/14/formatting-phpunit-test-results-as-html-table/#comments</comments>
		<pubDate>Wed, 14 May 2008 07:17:55 +0000</pubDate>
		<dc:creator>Parth</dc:creator>
		
		<category><![CDATA[PHP]]></category>

		<category><![CDATA[Programming]]></category>

		<category><![CDATA[Testing]]></category>

		<category><![CDATA[phpunit]]></category>

		<guid isPermaLink="false">http://parthpatil.com/?p=9</guid>
		<description><![CDATA[PHPUnit is an excellent Unit Test framework for PHP based on XUnit. If you haven&#8217;t used PHPUnit or unit testing you should definitely read about it at http://phpunit.de. In this tutorial I explain how you can custom format the test results from PHPUnit. The tutorial assumes familiarity with PHPUnit.
I am a big fan of PHPUnit [...]]]></description>
			<content:encoded><![CDATA[<p>PHPUnit is an excellent Unit Test framework for PHP based on XUnit. If you haven&#8217;t used PHPUnit or unit testing you should definitely read about it at <a href="http://phpunit.de" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://phpunit.de');">http://phpunit.de</a>. In this tutorial I explain how you can custom format the test results from PHPUnit. The tutorial assumes familiarity with PHPUnit.<br />
I am a big fan of PHPUnit and use it very often during PHP development. I wanted to view the results of tests run on remote servers in my web browser. The default test result output for phpunit is in the TAP format and its not convenient to parse this output. XML is more suitable for parsing and we will see how to get our test results in xml format.<br />
We will consider the following example Unit Test for this tutorial</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
</pre></td><td class="code"><pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
    <span style="color: #b1b100;">require_once</span> <span style="color: #ff0000;">'PHPUnit/Framework.php'</span><span style="color: #66cc66;">;</span>
    <span style="color: #000000; font-weight: bold;">class</span> ArrayTest <span style="color: #000000; font-weight: bold;">extends</span> PHPUnit_Framework_TestCase
    <span style="color: #66cc66;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> testNewArrayIsEmpty<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#123;</span>
            <span style="color: #808080; font-style: italic;">// Create the Array fixture.    </span>
            <span style="color: #ff0000">$my_array</span> <span style="color: #66cc66;">=</span> <span style="color: #000066;">array</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
&nbsp;
            <span style="color: #808080; font-style: italic;">// Assert that the size of the Array fixture is 0.</span>
            <span style="color: #ff0000">$this</span><span style="color: #66cc66;">-&gt;</span><span style="color: #006600;">assertEquals</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">,</span> <span style="color: #000066;">sizeof</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000">$my_array</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
        <span style="color: #66cc66;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> testArrayContainsAnElement<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#123;</span>
            <span style="color: #808080; font-style: italic;">// Create the Array fixture. </span>
            <span style="color: #ff0000">$my_array</span> <span style="color: #66cc66;">=</span> <span style="color: #000066;">array</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
&nbsp;
            <span style="color: #808080; font-style: italic;">// Add an element to the Array fixture.</span>
            <span style="color: #ff0000">$my_array</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'Element'</span><span style="color: #66cc66;">;</span>
&nbsp;
            <span style="color: #808080; font-style: italic;">// Make this test fail on purpose </span>
            <span style="color: #ff0000">$this</span><span style="color: #66cc66;">-&gt;</span><span style="color: #006600;">assertEquals</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">,</span> <span style="color: #000066;">sizeof</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000">$my_array</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
        <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>The test is run on the command line as follows</p>

<div class="wp_syntax"><div class="code"><pre class="bash">phpunit ArrayTest</pre></div></div>

<p>The output from running the above test on command line looks as follows</p>
<p>
<img src="http://parthpatil.com/wp-content/uploads/2008/05/terminalscreensnapz001.jpg" alt="" title="default phpunit output" width="500" height="225" class="alignnone size-full wp-image-13" />
</p>
<p>As I mentioned before, for formating the results of the tests into HTML table its convenient to have the results in the form of xml. There are two ways to get the test results in xml</p>
<p>
<b>1.</b> Specifying an option to PHPUnit on the command line.</p>

<div class="wp_syntax"><div class="code"><pre class="bash">phpunit --log-xml report.xml ArrayTest</pre></div></div>

</p>
<p>
<b>2.</b> By doing what the PHPUnit does internally to get the output in xml format.</p>
<p>
For the first option you will have to execute the phpunit command from inside the PHP script, the xml output is written to a file which has to be again read by your PHP script. The second option does not require you to run the phpunit command from inside PHP script and gives you lot more flexibility than the first one. It will also give you an idea of how to use custom formatters for PHPUnit results.<br />
I couldn&#8217;t find much documentation on how to use/write custom formatters for PHPUnit test results. After tracing PHPUnit executable(the &#8220;phpunit&#8221; command itself) using xdebug I found out how PHPUnit outputs test in xml format when you specify &#8220;&#8211;log-xml&#8221; option on the command line. PHPUnit uses something called TestListener for custom formatting of test results. The TestListener follows the observer pattern. The TestListener subscribes to &#8220;test failure&#8221; and &#8220;test success&#8221; events of the PHPUnit_Framework_TestResult class.</p>
<p>I have created a Test Runner that uses the xml TestListener. The test runner looks as follows.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
</pre></td><td class="code"><pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #b1b100;">require_once</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'PHPUnit/Framework/TestSuite.php'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
<span style="color: #b1b100;">require_once</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'PHPUnit/Framework/TestResult.php'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
<span style="color: #b1b100;">require_once</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'PHPUnit/Util/Log/XML.php'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
<span style="color: #b1b100;">require_once</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'ArrayTest.php'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> MyTestRunner
<span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066;">static</span> <span style="color: #000000; font-weight: bold;">function</span> run<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#123;</span>
        <span style="color: #808080; font-style: italic;">// Create the test suite instance</span>
        <span style="color: #ff0000">$suite</span> <span style="color: #66cc66;">=</span> <span style="color: #000000; font-weight: bold;">new</span> PHPUnit_Framework_TestSuite<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
        <span style="color: #ff0000">$suite</span><span style="color: #66cc66;">-&gt;</span><span style="color: #006600;">setName</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'MyTestRunner'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;">// Add files to the TestSuite</span>
        <span style="color: #ff0000">$suite</span><span style="color: #66cc66;">-&gt;</span><span style="color: #006600;">addTestSuite</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'ArrayTest'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;">// Create a xml listener object </span>
        <span style="color: #ff0000">$listener</span> <span style="color: #66cc66;">=</span> <span style="color: #000000; font-weight: bold;">new</span> PHPUnit_Util_Log_XML<span style="color: #66cc66;">;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;">// Create TestResult object and pass the xml listener to it</span>
        <span style="color: #ff0000">$testResult</span> <span style="color: #66cc66;">=</span> <span style="color: #000000; font-weight: bold;">new</span> PHPUnit_Framework_TestResult<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
        <span style="color: #ff0000">$testResult</span><span style="color: #66cc66;">-&gt;</span><span style="color: #006600;">addListener</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000">$listener</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;">// Run the TestSuite</span>
        <span style="color: #ff0000">$result</span> <span style="color: #66cc66;">=</span> <span style="color: #ff0000">$suite</span><span style="color: #66cc66;">-&gt;</span><span style="color: #006600;">run</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000">$testResult</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;">// Get the results from the listener</span>
        <span style="color: #ff0000">$xml_result</span> <span style="color: #66cc66;">=</span> <span style="color: #ff0000">$listener</span><span style="color: #66cc66;">-&gt;</span><span style="color: #006600;">getXML</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #ff0000">$xml_result</span><span style="color: #66cc66;">;</span>
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>I have simplified the code for the The Test Runner for purpose of this tutorial. An actual Test Runner would have some mechanism to get the list of tests to run and a loop to add all those tests to the TestSuite.</p>
<p>The Test runner code is pretty straight forward. I first create a TestSuite . Then I add the ArrayTest to the TestSuite. After that I instantiate an xml TestListener from the PHPUnit_Util_Log_XML class that comes bundled with PHPUnit. Then I instantiate a PHPUnit_Framework_TestResult object and add the TestListener object to the TestResult object. Then I run the TestSuite and get the test result in xml format from the listener object.<br />
This is how the xml looks like</p>

<div class="wp_syntax"><div class="code"><pre class="xml"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="font-weight: bold; color: black;">?&gt;</span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;testsuites<span style="font-weight: bold; color: black;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;testsuite</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;MyTestRunner&quot;</span> <span style="color: #000066;">file</span>=<span style="color: #ff0000;">&quot;/home/parth/test/MyTestRunner.php&quot;</span> <span style="color: #000066;">tests</span>=<span style="color: #ff0000;">&quot;2&quot;</span> <span style="color: #000066;">failures</span>=<span style="color: #ff0000;">&quot;1&quot;</span> <span style="color: #000066;">errors</span>=<span style="color: #ff0000;">&quot;0&quot;</span> <span style="color: #000066;">time</span>=<span style="color: #ff0000;">&quot;0.002406&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span>
    <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;testsuite</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;ArrayTest&quot;</span> <span style="color: #000066;">file</span>=<span style="color: #ff0000;">&quot;/home/parth/test/ArrayTest.php&quot;</span> <span style="color: #000066;">tests</span>=<span style="color: #ff0000;">&quot;2&quot;</span> <span style="color: #000066;">failures</span>=<span style="color: #ff0000;">&quot;1&quot;</span> <span style="color: #000066;">errors</span>=<span style="color: #ff0000;">&quot;0&quot;</span> <span style="color: #000066;">time</span>=<span style="color: #ff0000;">&quot;0.002406&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span>
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;testcase</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;testNewArrayIsEmpty&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;ArrayTest&quot;</span> <span style="color: #000066;">file</span>=<span style="color: #ff0000;">&quot;/home/parth/test/ArrayTest.php&quot;</span> <span style="color: #000066;">line</span>=<span style="color: #ff0000;">&quot;5&quot;</span> <span style="color: #000066;">time</span>=<span style="color: #ff0000;">&quot;0.000514&quot;</span><span style="font-weight: bold; color: black;">/&gt;</span></span>
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;testcase</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;testArrayContainsAnElement&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;ArrayTest&quot;</span> <span style="color: #000066;">file</span>=<span style="color: #ff0000;">&quot;/home/parth/test/ArrayTest.php&quot;</span> <span style="color: #000066;">line</span>=<span style="color: #ff0000;">&quot;14&quot;</span> <span style="color: #000066;">time</span>=<span style="color: #ff0000;">&quot;0.001892&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span>
        <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;failure</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;PHPUnit_Framework_ExpectationFailedException&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span><span style="color: #339933;">&lt;![CDATA[testArrayContainsAnElement(ArrayTest)</span>
<span style="color: #339933;">Failed asserting that &lt;integer:1&gt; matches expected value &lt;integer:2&gt;.</span>
&nbsp;
<span style="color: #339933;">/home/parth/test/ArrayTest.php:23</span>
<span style="color: #339933;">/home/parth/test/MyTestRunner.php:26</span>
<span style="color: #339933;">/home/parth/test/MyTestRunner.php:34</span>
<span style="color: #339933;">]]&gt;</span><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/failure<span style="font-weight: bold; color: black;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/testcase<span style="font-weight: bold; color: black;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/testsuite<span style="font-weight: bold; color: black;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/testsuite<span style="font-weight: bold; color: black;">&gt;</span></span></span>
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/testsuites<span style="font-weight: bold; color: black;">&gt;</span></span></span></pre></div></div>

<p>As expected the xml shows the same results as before(when run on the command line). The xml is self descriptive and has lot of metadata for the test results. Next step is to parse the above xml into an associative array. Following is how I parsed the above result xml using SimpleXML.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
</pre></td><td class="code"><pre class="php"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
   <span style="color: #ff0000">$test_xml</span> <span style="color: #66cc66;">=</span> MyTestRunner<span style="color: #66cc66;">::</span><span style="color: #006600;">run</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
   <span style="color: #ff0000">$simple</span> <span style="color: #66cc66;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SimpleXMLElement<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000">$test_xml</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
&nbsp;
   <span style="color: #ff0000">$test_results</span> <span style="color: #66cc66;">=</span> <span style="color: #000066;">array</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
&nbsp;
   <span style="color: #b1b100;">foreach</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000">$simple</span><span style="color: #66cc66;">-&gt;</span><span style="color: #66cc66;">&#123;</span><span style="color: #ff0000;">'testsuite'</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">-&gt;</span><span style="color: #66cc66;">&#123;</span><span style="color: #ff0000;">'testsuite'</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">-&gt;</span><span style="color: #006600;">testcase</span> <span style="color: #b1b100;">as</span> <span style="color: #ff0000">$testcase</span><span style="color: #66cc66;">&#41;</span>
   <span style="color: #66cc66;">&#123;</span>
       <span style="color: #ff0000">$result</span> <span style="color: #66cc66;">=</span> <span style="color: #000066;">array</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">;</span>
       <span style="color: #808080; font-style: italic;">// Don't froget to cast SimpleXMLElement to string!</span>
       <span style="color: #ff0000">$result</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'name'</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#40;</span>string<span style="color: #66cc66;">&#41;</span><span style="color: #ff0000">$testcase</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'name'</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">;</span>
       <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #000066;">isset</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000">$testcase</span><span style="color: #66cc66;">-&gt;</span><span style="color: #66cc66;">&#123;</span><span style="color: #ff0000;">'failure'</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
       <span style="color: #66cc66;">&#123;</span>
           <span style="color: #ff0000">$result</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'result'</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'Fail'</span><span style="color: #66cc66;">;</span>
           <span style="color: #ff0000">$result</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'message'</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#40;</span>string<span style="color: #66cc66;">&#41;</span><span style="color: #ff0000">$testcase</span><span style="color: #66cc66;">-&gt;</span><span style="color: #66cc66;">&#123;</span><span style="color: #ff0000;">'failure'</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">;</span>
       <span style="color: #66cc66;">&#125;</span>
       <span style="color: #b1b100;">else</span>
       <span style="color: #66cc66;">&#123;</span>
            <span style="color: #ff0000">$result</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'result'</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'Pass'</span><span style="color: #66cc66;">;</span>
            <span style="color: #ff0000">$result</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'message'</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">''</span><span style="color: #66cc66;">;</span>
       <span style="color: #66cc66;">&#125;</span>
       <span style="color: #ff0000">$test_results</span><span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #ff0000">$result</span><span style="color: #66cc66;">;</span>     
   <span style="color: #66cc66;">&#125;</span>
   <span style="color: #ff0000">$test_results</span><span style="color: #66cc66;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>And finally I rendered the result in the form of a html table</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code"><pre class="php"><span style="color: #66cc66;">&lt;</span>table cellspacing<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;0&quot;</span> <span style="color: #000000; font-weight: bold;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;test_results&quot;</span><span style="color: #66cc66;">&gt;</span>
  <span style="color: #66cc66;">&lt;</span>thead<span style="color: #66cc66;">&gt;</span>
    <span style="color: #66cc66;">&lt;</span>tr<span style="color: #66cc66;">&gt;&lt;</span>th<span style="color: #66cc66;">&gt;</span>Test Name<span style="color: #66cc66;">&lt;/</span>th<span style="color: #66cc66;">&gt;&lt;</span>th<span style="color: #66cc66;">&gt;</span>Result<span style="color: #66cc66;">&lt;/</span>th<span style="color: #66cc66;">&gt;&lt;</span>th<span style="color: #66cc66;">&gt;</span>Message<span style="color: #66cc66;">&lt;/</span>th<span style="color: #66cc66;">&gt;&lt;/</span>tr<span style="color: #66cc66;">&gt;</span>
  <span style="color: #66cc66;">&lt;/</span>thead<span style="color: #66cc66;">&gt;</span>
  <span style="color: #66cc66;">&lt;</span>tbody<span style="color: #66cc66;">&gt;</span>
  <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">foreach</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000">$test_results</span> <span style="color: #b1b100;">as</span> <span style="color: #ff0000">$test_result</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
  <span style="color: #66cc66;">&lt;</span>tr<span style="color: #66cc66;">&gt;</span>
      <span style="color: #66cc66;">&lt;</span>td<span style="color: #66cc66;">&gt;&lt;</span>?php <span style="color: #000066;">echo</span> <span style="color: #ff0000">$test_result</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'name'</span><span style="color: #66cc66;">&#93;</span> ?<span style="color: #66cc66;">&gt;&lt;/</span>td<span style="color: #66cc66;">&gt;</span>
      <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000">$test_result</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'result'</span><span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">==</span> <span style="color: #ff0000;">'Fail'</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
      <span style="color: #66cc66;">&lt;</span>td <span style="color: #000000; font-weight: bold;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;test_fail&quot;</span><span style="color: #66cc66;">/&gt;</span>
      <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">else</span><span style="color: #66cc66;">:</span> <span style="color: #000000; font-weight: bold;">?&gt;</span> 
      <span style="color: #66cc66;">&lt;</span>td <span style="color: #000000; font-weight: bold;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;test_pass&quot;</span><span style="color: #66cc66;">/&gt;</span>
      <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endif</span><span style="color: #66cc66;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
      <span style="color: #66cc66;">&lt;</span>td<span style="color: #66cc66;">&gt;&lt;</span>?php <span style="color: #000066;">echo</span> <span style="color: #ff0000">$test_result</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'message'</span><span style="color: #66cc66;">&#93;</span> ?<span style="color: #66cc66;">&gt;&lt;/</span>td<span style="color: #66cc66;">&gt;</span>
  <span style="color: #66cc66;">&lt;/</span>tr<span style="color: #66cc66;">&gt;</span>
  <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">endforeach</span><span style="color: #66cc66;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
  <span style="color: #66cc66;">&lt;/</span>tbody<span style="color: #66cc66;">&gt;</span>
<span style="color: #66cc66;">&lt;/</span>table<span style="color: #66cc66;">&gt;</span></pre></td></tr></table></div>

<p>Here is how the results look in html table. I borrowed the css from Symfony&#8217;s admin generator module.<br />
<img src="http://parthpatil.com/wp-content/uploads/2008/05/firefoxscreensnapz001.jpg" alt="" title="test result in html table" width="500" height="163" class="alignnone size-full wp-image-12" /></p>
<p>I hope this tutorial has been helpful. Thanks for reading!</p>
]]></content:encoded>
			<wfw:commentRss>http://parthpatil.com/2008/05/14/formatting-phpunit-test-results-as-html-table/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 1.181 seconds -->
