<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>
<channel>
	<title>Comments on: pTest: PHP Unit Tester in 9 Lines Of Code</title>
	<atom:link href="http://www.sitepoint.com/blogs/2007/08/13/ptest-php-unit-tester-in-9-lines-of-code/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.sitepoint.com/blogs/2007/08/13/ptest-php-unit-tester-in-9-lines-of-code/</link>
	<description>News, opinion, and fresh thinking for web developers and designers. The official podcast of sitepoint.com.</description>
	<pubDate>Fri, 21 Nov 2008 08:18:25 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5</generator>
		<item>
		<title>By: Christopher Thompson</title>
		<link>http://www.sitepoint.com/blogs/2007/08/13/ptest-php-unit-tester-in-9-lines-of-code/#comment-334931</link>
		<dc:creator>Christopher Thompson</dc:creator>
		<pubDate>Tue, 14 Aug 2007 00:15:49 +0000</pubDate>
		<guid isPermaLink="false">http://www.sitepoint.com/blogs/2007/08/13/ptest-php-unit-tester-in-9-lines-of-code/#comment-334931</guid>
		<description>I wrote a small unit tester as part of a forum discussion a while back that I called TinyTest. Although it is 50 lines, not 9, it provides assertTrue(), assertFalse(), pass() and fail() plus standard green/red output of the results. The original ideas was to have a unit tester small enough that you could supply it with posted code and tests. 

&lt;pre&gt;&lt;code class="php"&gt;
class UnitTester {
        var $_passes = 0;
        var $_fails = 0;
        var $_test_stack = array();
        var $_trace_pos = 0;
        var $output = '';
       
        function assertTrue($result, $pos=0) {
                if ($result) {
                        $this-&#62;pass();
                } else {
                        $this-&#62;_trace_pos = $pos + 1;
                        $this-&#62;fail();
                }
        }

        function assertFalse($result, $pos=0) {
                if ($result) {
                        $this-&#62;_trace_pos = $pos + 1;
                        $this-&#62;fail();
                } else {
                        $this-&#62;pass();
                }
        }

        function pass() {
                ++$this-&#62;_passes;
        }

        function fail() {
                ++$this-&#62;_fails;
                $test_data = debug_backtrace();
                $this-&#62;_test_stack[] = $test_data[$this-&#62;_trace_pos];
                $this-&#62;_trace_pos = 0;
        }

        var $passed_heading_style = 'color: white; background-color: green; margin-top: 2; padding: 4 4 4 4;';
        var $failed_heading_style = 'color: white; background-color: red; margin-top: 2; padding: 4 4 4 4;';
        var $failed_test_style = 'color: white; background-color: red; margin-top: 2; padding-left: 4;';

        function __destruct() {
                if ($this-&#62;_fails &#62; 0) {
                        $output = '&#60;div style="' . $this-&#62;failed_heading_style . '"&#62;Failed ' . $this-&#62;_fails . ' of ' . ($this-&#62;_fails + $this-&#62;_passes) . ' tests. &#60;/div&#62;';
                        $n = 1;
                        foreach ($this-&#62;_test_stack as $this_data) {
                                $message = $n++ . ". Failed: {$this_data['function']} on line {$this_data['line']} of file {$this_data['file']}. ";
                                $output .= '&#60;div style="' . $this-&#62;failed_test_style . '"&#62;' . $message . '&#60;/div&#62;';
                        }
                } else {
                        $output = '&#60;div style="' . $this-&#62;passed_heading_style . '"&#62;Passed ' .$this-&#62;_passes . ' tests.&#60;/div&#62;';
                }
            echo $output;
        }

}
&lt;/code&gt;&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>I wrote a small unit tester as part of a forum discussion a while back that I called TinyTest. Although it is 50 lines, not 9, it provides assertTrue(), assertFalse(), pass() and fail() plus standard green/red output of the results. The original ideas was to have a unit tester small enough that you could supply it with posted code and tests. </p>
<pre><code class="php">
class UnitTester {
        var $_passes = 0;
        var $_fails = 0;
        var $_test_stack = array();
        var $_trace_pos = 0;
        var $output = '';
       
        function assertTrue($result, $pos=0) {
                if ($result) {
                        $this-&gt;pass();
                } else {
                        $this-&gt;_trace_pos = $pos + 1;
                        $this-&gt;fail();
                }
        }

        function assertFalse($result, $pos=0) {
                if ($result) {
                        $this-&gt;_trace_pos = $pos + 1;
                        $this-&gt;fail();
                } else {
                        $this-&gt;pass();
                }
        }

        function pass() {
                ++$this-&gt;_passes;
        }

        function fail() {
                ++$this-&gt;_fails;
                $test_data = debug_backtrace();
                $this-&gt;_test_stack[] = $test_data[$this-&gt;_trace_pos];
                $this-&gt;_trace_pos = 0;
        }

        var $passed_heading_style = 'color: white; background-color: green; margin-top: 2; padding: 4 4 4 4;';
        var $failed_heading_style = 'color: white; background-color: red; margin-top: 2; padding: 4 4 4 4;';
        var $failed_test_style = 'color: white; background-color: red; margin-top: 2; padding-left: 4;';

        function __destruct() {
                if ($this-&gt;_fails &gt; 0) {
                        $output = '&lt;div style="' . $this-&gt;failed_heading_style . '"&gt;Failed ' . $this-&gt;_fails . ' of ' . ($this-&gt;_fails + $this-&gt;_passes) . ' tests. &lt;/div&gt;';
                        $n = 1;
                        foreach ($this-&gt;_test_stack as $this_data) {
                                $message = $n++ . ". Failed: {$this_data['function']} on line {$this_data['line']} of file {$this_data['file']}. ";
                                $output .= '&lt;div style="' . $this-&gt;failed_test_style . '"&gt;' . $message . '&lt;/div&gt;';
                        }
                } else {
                        $output = '&lt;div style="' . $this-&gt;passed_heading_style . '"&gt;Passed ' .$this-&gt;_passes . ' tests.&lt;/div&gt;';
                }
            echo $output;
        }

}
</code></pre>]]></content:encoded>
	</item>
	<item>
		<title>By: shiflett</title>
		<link>http://www.sitepoint.com/blogs/2007/08/13/ptest-php-unit-tester-in-9-lines-of-code/#comment-334834</link>
		<dc:creator>shiflett</dc:creator>
		<pubDate>Mon, 13 Aug 2007 18:11:05 +0000</pubDate>
		<guid isPermaLink="false">http://www.sitepoint.com/blogs/2007/08/13/ptest-php-unit-tester-in-9-lines-of-code/#comment-334834</guid>
		<description>You might want to check out TAP, the Test Anything Protocol:

http://testanything.org/

If you're going to write a simple library like this, making it TAP-compliant means your output is going to be consumable by a number of  mature testing tools.

There's a PHP library that comes bundled with Apache-Test that outputs TAP and is almost as simple as yours:

http://shiflett.org/code/test-more.php</description>
		<content:encoded><![CDATA[<p>You might want to check out TAP, the Test Anything Protocol:</p>
<p><a href="http://testanything.org/" rel="nofollow">http://testanything.org/</a></p>
<p>If you&#8217;re going to write a simple library like this, making it TAP-compliant means your output is going to be consumable by a number of  mature testing tools.</p>
<p>There&#8217;s a PHP library that comes bundled with Apache-Test that outputs TAP and is almost as simple as yours:</p>
<p><a href="http://shiflett.org/code/test-more.php" rel="nofollow">http://shiflett.org/code/test-more.php</a></p>]]></content:encoded>
	</item>
	<item>
		<title>By: deathshadow</title>
		<link>http://www.sitepoint.com/blogs/2007/08/13/ptest-php-unit-tester-in-9-lines-of-code/#comment-334599</link>
		<dc:creator>deathshadow</dc:creator>
		<pubDate>Mon, 13 Aug 2007 07:23:19 +0000</pubDate>
		<guid isPermaLink="false">http://www.sitepoint.com/blogs/2007/08/13/ptest-php-unit-tester-in-9-lines-of-code/#comment-334599</guid>
		<description>Good lord, people write whole units for this stuff? You know, after three decades of doing this ****, I am still amazed people ever need to waste time on this level of internal reporting.

Unit tester - took me a while just to figure out you meant result tracking... is that the new name for a four decade old method of code auditing?</description>
		<content:encoded><![CDATA[<p>Good lord, people write whole units for this stuff? You know, after three decades of doing this ****, I am still amazed people ever need to waste time on this level of internal reporting.</p>
<p>Unit tester - took me a while just to figure out you meant result tracking&#8230; is that the new name for a four decade old method of code auditing?</p>]]></content:encoded>
	</item>
	<item>
		<title>By: z0s0</title>
		<link>http://www.sitepoint.com/blogs/2007/08/13/ptest-php-unit-tester-in-9-lines-of-code/#comment-334566</link>
		<dc:creator>z0s0</dc:creator>
		<pubDate>Mon, 13 Aug 2007 06:04:39 +0000</pubDate>
		<guid isPermaLink="false">http://www.sitepoint.com/blogs/2007/08/13/ptest-php-unit-tester-in-9-lines-of-code/#comment-334566</guid>
		<description>Yeah... reckon it must be one of PHP's least used builtins.</description>
		<content:encoded><![CDATA[<p>Yeah&#8230; reckon it must be one of PHP&#8217;s least used builtins.</p>]]></content:encoded>
	</item>
	<item>
		<title>By: Paul Annesley</title>
		<link>http://www.sitepoint.com/blogs/2007/08/13/ptest-php-unit-tester-in-9-lines-of-code/#comment-334477</link>
		<dc:creator>Paul Annesley</dc:creator>
		<pubDate>Mon, 13 Aug 2007 04:02:07 +0000</pubDate>
		<guid isPermaLink="false">http://www.sitepoint.com/blogs/2007/08/13/ptest-php-unit-tester-in-9-lines-of-code/#comment-334477</guid>
		<description>Good point.. although configuring it to result in output similar to assertTrue() (without making any assumptions about PHP's global configuration) probably uses more lines of code ;)

With a bit of work it can certainly be set up nicely - with added benefits like line number reporting, and output of the evaled assertion code.

But I think the simplicity and self contained nature of pTest probably makes it less daunting to beginners to PHP and unit testing.</description>
		<content:encoded><![CDATA[<p>Good point.. although configuring it to result in output similar to assertTrue() (without making any assumptions about PHP&#8217;s global configuration) probably uses more lines of code ;)</p>
<p>With a bit of work it can certainly be set up nicely - with added benefits like line number reporting, and output of the evaled assertion code.</p>
<p>But I think the simplicity and self contained nature of pTest probably makes it less daunting to beginners to PHP and unit testing.</p>]]></content:encoded>
	</item>
	<item>
		<title>By: z0s0</title>
		<link>http://www.sitepoint.com/blogs/2007/08/13/ptest-php-unit-tester-in-9-lines-of-code/#comment-334458</link>
		<dc:creator>z0s0</dc:creator>
		<pubDate>Mon, 13 Aug 2007 03:47:31 +0000</pubDate>
		<guid isPermaLink="false">http://www.sitepoint.com/blogs/2007/08/13/ptest-php-unit-tester-in-9-lines-of-code/#comment-334458</guid>
		<description>Interestingly, PHP actually has an assertion tester built-in:

http://php.net/assert</description>
		<content:encoded><![CDATA[<p>Interestingly, PHP actually has an assertion tester built-in:</p>
<p><a href="http://php.net/assert" rel="nofollow">http://php.net/assert</a></p>]]></content:encoded>
	</item>
</channel>
</rss>
