<?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; Programming</title>
	<atom:link href="http://parthpatil.com/category/programming/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>
		<item>
		<title>Using Perl Expect To Automate SFTP Access</title>
		<link>http://parthpatil.com/2008/05/02/using-perl-expect-to-automate-sftp-access/</link>
		<comments>http://parthpatil.com/2008/05/02/using-perl-expect-to-automate-sftp-access/#comments</comments>
		<pubDate>Sat, 03 May 2008 01:00:49 +0000</pubDate>
		<dc:creator>Parth</dc:creator>
		
		<category><![CDATA[Programming]]></category>

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

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

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

		<guid isPermaLink="false">http://parthpatil.com/2008/04/30/using-perl-expect-to-automate-sftp-access-2/</guid>
		<description><![CDATA[Recently I had to do a task where I had to automate the downloading of files from a sftp server. Automating plain ftp download is much easier than automating sftp because there are prompts from the remote server that have to be handled. For automating such interactive applications there is a wonderful tool called Expect. [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I had to do a task where I had to automate the downloading of files from a sftp server. Automating plain ftp download is much easier than automating sftp because there are prompts from the remote server that have to be handled. For automating such interactive applications there is a wonderful tool called <a href="http://expect.nist.gov/" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://expect.nist.gov/');">Expect</a>.  The automation scripts for Expect are written in the Tcl language. There was an existing Tcl Expect script to automate downloading of files from sftp. I found that I did not have Tcl Expect installed and I had heard that Perl Expect is very similar to TCL Expect. So I decided to give it a shot. I ported the original Expect script to Perl version and added few features.<br />
Following is the script and I have provided some explanation below the code.</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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
</pre></td><td class="code"><pre class="perl"><span style="color: #000000; font-weight: bold;">use</span> strict;
<span style="color: #000000; font-weight: bold;">use</span> Expect;
&nbsp;
<span style="color: #808080; font-style: italic;"># Uncomment the following line if you want to see what expect is doing </span>
<span style="color: #808080; font-style: italic;">#$Expect::Exp_Internal = 1;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Uncomment the following line if you don't want to see any output from the script</span>
<span style="color: #808080; font-style: italic;">#$Expect::Log_Stdout = 0;</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># set your username/password here</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$sftpUsername</span> = <span style="color: #ff0000;">&quot;username&quot;</span> ;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$sftpPassword</span> = <span style="color: #ff0000;">&quot;password&quot;</span>; 
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$sftpServer</span> = <span style="color: #ff0000;">&quot;yourserver&quot;</span>;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$fileToFetch</span> = <span style="color: #ff0000;">&quot;myfile&quot;</span>;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$timeout</span> = <span style="color: #cc66cc;">10</span>; 
&nbsp;
<span style="color: #808080; font-style: italic;"># If sftp is not in your path replace with absolute path of sftp program</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$command</span> = <span style="color: #ff0000;">'sftp'</span>;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$params</span> = <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;$sftpUsername<span style="color: #000099; font-weight: bold;">\@</span>$sftpServer&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;"># Create the Expect object</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$exp</span> = Expect-<span style="color: #66cc66;">&gt;</span><span style="color: #006600;">spawn</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$command</span>, <span style="color: #0000ff;">$params</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">or</span> <span style="color: #000066;">die</span> <span style="color: #ff0000;">&quot;Cannot spawn sftp command <span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;"># If this is the first time you are running this , expect will send &quot;yes&quot; to add the key</span>
<span style="color: #808080; font-style: italic;"># for the sftp server to the ~/.ssh/known_hosts file else</span>
<span style="color: #808080; font-style: italic;"># wait for &quot;Password Authentication&quot; string to show up</span>
<span style="color: #0000ff;">$exp</span>-<span style="color: #66cc66;">&gt;</span><span style="color: #006600;">expect</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$timeout</span>,
	<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;Password Authentication&quot;</span><span style="color: #66cc66;">&#93;</span>,
	<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;Are you sure you want to continue connecting&quot;</span>, <span style="color: #000000; font-weight: bold;">sub</span> <span style="color: #66cc66;">&#123;</span><span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$self</span> = <span style="color: #000066;">shift</span>; <span style="color: #0000ff;">$self</span>-<span style="color: #66cc66;">&gt;</span><span style="color: #006600;">send</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;yes<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #66cc66;">&#41;</span>;<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#93;</span>
	<span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;"># Wait for Password prompt to show up </span>
<span style="color: #0000ff;">$exp</span>-<span style="color: #66cc66;">&gt;</span><span style="color: #006600;">expect</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$timeout</span>, <span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;Password:&quot;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;"># Sent the sftp password </span>
<span style="color: #0000ff;">$exp</span>-<span style="color: #66cc66;">&gt;</span><span style="color: #006600;">send</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;$sftpPassword<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;"># Wait for sftp prompt</span>
<span style="color: #0000ff;">$exp</span>-<span style="color: #66cc66;">&gt;</span><span style="color: #006600;">expect</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$timeout</span>, <span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;sftp&gt;&quot;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;"># Get yesterday's report file/s and put it in reportsPath directory on the local machine</span>
<span style="color: #0000ff;">$exp</span>-<span style="color: #66cc66;">&gt;</span><span style="color: #006600;">send</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;get $fileToFetch<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;"># Wait for sftp prompt</span>
<span style="color: #0000ff;">$exp</span>-<span style="color: #66cc66;">&gt;</span><span style="color: #006600;">expect</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$timeout</span>, <span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;sftp&gt;&quot;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;"># Close ftp session</span>
<span style="color: #0000ff;">$exp</span>-<span style="color: #66cc66;">&gt;</span><span style="color: #006600;">send</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;bye<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #808080; font-style: italic;"># Destroy the expect object</span>
<span style="color: #0000ff;">$exp</span>-<span style="color: #66cc66;">&gt;</span><span style="color: #006600;">soft_close</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</pre></td></tr></table></div>

<p>You need to install Expect from cpan if you don&#8217;t have it installed.  To check if the Expect is installed you could check it in your terminal by executing this &#8220;perl -MExpect -e1&#8243;.<br />
On line 22 I am creating an Expect object using using the spawn function. spawn takes the command to execute as the first argument and the parameters to the command as an array of arguments, hence  $params is an array containing single element which is &#8220;username@sftpserver&#8221;.<br />
Now when the script is run for the first time, there is a prompt to save the key of the sftp server in the ~/.ssh/know_hosts file and if the entry for the remote sftp server is already present in the known_hosts file a &#8220;Password Authentication&#8221; prompt will be sent by the sftp server. Both these situations have to be handled in the Expect script. Fortunately its very easy to to write handlers for these two possibilities in a single expect function call. The expect function can take any number of possible strings to expect from the remote server at a given point. You can see this on line 28 &amp; 29, each possible string is provided as an array reference. And if there is an action that you want to perform corresponding to a particular string you can do this by providing a subroutine to handle this. You could either pass a reference to a subroutine or provide an anonymous subroutine. On line 29 I have provided an anonymous subroutine to send &#8220;yes&#8221; when prompted for accepting the key from the sftp server. Its also possible to provide the regular expression instead of literal string matches to the expect function.<br />
The rest of the script is self explanatory.</p>
<p>Read the Expect module documentation on cpan for more information :<br />
<a title="Expect Documentation" href="http://search.cpan.org/~rgiersig/Expect-1.21/Expect.pod" onclick="javascript:pageTracker._trackPageview('/outbound/article/http://search.cpan.org/~rgiersig/Expect-1.21/Expect.pod');" target="_blank">http://search.cpan.org/~rgiersig/Expect-1.21/Expect.pod</a></p>
]]></content:encoded>
			<wfw:commentRss>http://parthpatil.com/2008/05/02/using-perl-expect-to-automate-sftp-access/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>

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