<?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"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>PHP in Action &#187; Testing</title>
	<atom:link href="http://blog.agilephp.com/category/testing/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.agilephp.com</link>
	<description>Dagfinn Reiersøl on PHP, agile development, Ruby and other addictive substances</description>
	<lastBuildDate>Mon, 28 Sep 2009 14:35:32 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Don&#8217;t refactor without unit tests</title>
		<link>http://blog.agilephp.com/2009/09/03/dont-refactor-without-unit-tests/</link>
		<comments>http://blog.agilephp.com/2009/09/03/dont-refactor-without-unit-tests/#comments</comments>
		<pubDate>Thu, 03 Sep 2009 13:56:49 +0000</pubDate>
		<dc:creator>dagfinn</dc:creator>
				<category><![CDATA[Clean code]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Refactoring]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[Unit testing]]></category>

		<guid isPermaLink="false">http://blog.agilephp.com/?p=1596</guid>
		<description><![CDATA[



Image by niallkennedy via Flickr



Brandon Savage is writing a series on code improvement using a code example (starting with Peer Review: Taking Code And Making It Better). In other words, it&#8217;s about refactoring, which is practically my favorite subject. Although I don&#8217;t agree with all of it, it&#8217;s mostly good advice. I recommend it.
That said, [...]]]></description>
			<content:encoded><![CDATA[<div class="zemanta-img" style="margin: 1em; display: block;">
<div>
<dl class="wp-caption alignleft" style="width: 250px;">
<dt class="wp-caption-dt"><a href="http://www.flickr.com/photos/35034351734@N01/330227455"><img title="Google &quot;Testing on the Toilet&quot;" src="http://farm1.static.flickr.com/155/330227455_738b83c72e_m.jpg" alt="Google &quot;Testing on the Toilet&quot;" width="240" height="79" /></a></dt>
<dd class="wp-caption-dd zemanta-img-attribution" style="font-size: 0.8em;">Image by <a href="http://www.flickr.com/photos/35034351734@N01/330227455">niallkennedy</a> via Flickr</dd>
</dl>
</div>
</div>
<p>Brandon Savage is writing a series on code improvement using a code example (starting with <a href="http://www.brandonsavage.net/peer-review-taking-code-and-making-it-better/">Peer Review: Taking Code And Making It Better</a>). In other words, it&#8217;s about <a class="zem_slink" title="Code refactoring" rel="wikipedia" href="http://en.wikipedia.org/wiki/Code_refactoring">refactoring</a>, which is practically my favorite subject. Although I don&#8217;t agree with all of it, it&#8217;s mostly good advice. I recommend it.</p>
<p>That said, I do have something important to add. The series is missing the first, most basic rule: Don&#8217;t refactor unless you have good automated test coverage (typically with <a class="zem_slink" title="Unit testing" rel="wikipedia" href="http://en.wikipedia.org/wiki/Unit_testing">unit tests</a>). And if there are no test, write them before you start refactoring. If you don&#8217;t, you&#8217;ll make mistakes and get lost in a frustrating bug search, unless you&#8217;re very stingy and don&#8217;t refactor too much. With test coverage, you have freedom to experiment, to change something and change it back if you don&#8217;t like the result. This, above all, is what makes refactoring such a great learning experience.</p>
<p>Some of those who have commented on the post <a href="http://www.brandonsavage.net/peer-review-looking-into-abstraction/">Peer Review: Looking Into Abstraction</a> (Greg Beaver, Jeff Carouth) mention testing and point out the need to inject objects so that they can be replaced with mock objects for testing. This is a wise move, and practically unavoidable in this case. To support refactoring, unit tests need to be run frequently, without accessing outside services (twitter or email in this case) that take time to process.</p>
<p>This means that in this case, it&#8217;s actually necessary to refactor a bit before proper unit tests can be implemented. This is what Michael Feathers calls the <em>Legacy Code Dilemma</em> in his book <a href="http://www.amazon.com/gp/product/0131177052?ie=UTF8&amp;tag=phinac-20&amp;linkCode=as2&amp;camp=1789&amp;creative=9325&amp;creativeASIN=0131177052">Working Effectively with Legacy Code</a><img class=" lmfwcewjbjvyqgtkyshz lmfwcewjbjvyqgtkyshz lmfwcewjbjvyqgtkyshz lmfwcewjbjvyqgtkyshz lmfwcewjbjvyqgtkyshz lmfwcewjbjvyqgtkyshz lmfwcewjbjvyqgtkyshz lmfwcewjbjvyqgtkyshz lmfwcewjbjvyqgtkyshz lmfwcewjbjvyqgtkyshz lmfwcewjbjvyqgtkyshz lmfwcewjbjvyqgtkyshz lmfwcewjbjvyqgtkyshz lmfwcewjbjvyqgtkyshz lmfwcewjbjvyqgtkyshz lmfwcewjbjvyqgtkyshz lmfwcewjbjvyqgtkyshz lmfwcewjbjvyqgtkyshz lmfwcewjbjvyqgtkyshz lmfwcewjbjvyqgtkyshz lmfwcewjbjvyqgtkyshz lmfwcewjbjvyqgtkyshz wjtwchqpuszatsnifmwm wjtwchqpuszatsnifmwm wjtwchqpuszatsnifmwm wjtwchqpuszatsnifmwm wjtwchqpuszatsnifmwm wjtwchqpuszatsnifmwm wjtwchqpuszatsnifmwm wjtwchqpuszatsnifmwm wjtwchqpuszatsnifmwm wjtwchqpuszatsnifmwm wjtwchqpuszatsnifmwm wjtwchqpuszatsnifmwm wjtwchqpuszatsnifmwm wjtwchqpuszatsnifmwm wjtwchqpuszatsnifmwm wjtwchqpuszatsnifmwm wjtwchqpuszatsnifmwm wjtwchqpuszatsnifmwm wjtwchqpuszatsnifmwm wjtwchqpuszatsnifmwm wjtwchqpuszatsnifmwm wjtwchqpuszatsnifmwm" style="border: medium none  ! important; margin: 0px ! important;" src="http://www.assoc-amazon.com/e/ir?t=phinac-20&amp;l=as2&amp;o=1&amp;a=0131177052" border="0" alt="" width="1" height="1" />. Certainly building this dilemma into new code is not a good idea. Make the code unit-testable before it&#8217;s too late.</p>
<div class="zemanta-pixie" style="margin-top: 10px; height: 15px;"><a class="zemanta-pixie-a" title="Reblog this post [with Zemanta]" href="http://reblog.zemanta.com/zemified/20aff681-c4c1-8462-a15b-12d73157b0de/"><img class="zemanta-pixie-img" style="border: medium none; float: right;" src="http://img.zemanta.com/reblog_a.png?x-id=20aff681-c4c1-8462-a15b-12d73157b0de" alt="Reblog this post [with Zemanta]" /></a><span class="zem-script more-related pretty-attribution"><script src="http://static.zemanta.com/readside/loader.js" type="text/javascript"></script></span></div>
<p class="addtoany_share_save_container">
    <a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?sitename=PHP%20in%20Action&amp;siteurl=http%3A%2F%2Fblog.agilephp.com%2F&amp;linkname=Don%26%238217%3Bt%20refactor%20without%20unit%20tests&amp;linkurl=http%3A%2F%2Fblog.agilephp.com%2F2009%2F09%2F03%2Fdont-refactor-without-unit-tests%2F"><img src="http://blog.agilephp.com/wp-content/plugins/add-to-any/share_save_256_24.png" width="256" height="24" alt="Share/Save/Bookmark"/></a>

	</p>]]></content:encoded>
			<wfw:commentRss>http://blog.agilephp.com/2009/09/03/dont-refactor-without-unit-tests/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>One behavior != one assertion</title>
		<link>http://blog.agilephp.com/2009/02/21/one-behavior-one-assertion/</link>
		<comments>http://blog.agilephp.com/2009/02/21/one-behavior-one-assertion/#comments</comments>
		<pubDate>Sat, 21 Feb 2009 21:07:24 +0000</pubDate>
		<dc:creator>dagfinn</dc:creator>
				<category><![CDATA[Clean code]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[Behavior Driven Development]]></category>
		<category><![CDATA[Unit Tests]]></category>

		<guid isPermaLink="false">http://blog.agilephp.com/?p=1483</guid>
		<description><![CDATA[



Image by libbyrosof via Flickr



The debate on the &#8220;one assertion&#8221; principle continues. Pádraic Brady and I agree on the general principle, but since he is so categorical in his second article, I needed to inspect his reasoning more closely. It&#8217;s clear that our disagreement is more than superficial. The more I think about it, the [...]]]></description>
			<content:encoded><![CDATA[<div class="zemanta-img" style="margin: 1em; display: block;">
<div>
<dl class="wp-caption alignleft" style="width: 250px;">
<dt class="wp-caption-dt"><a href="http://www.flickr.com/photos/87251222@N00/2884366929"><img title="balkan naci islimyeli strait jacket" src="http://farm4.static.flickr.com/3224/2884366929_c8db1c6daa_m.jpg" alt="balkan naci islimyeli strait jacket" width="240" height="196" /></a></dt>
<dd class="wp-caption-dd zemanta-img-attribution" style="font-size: 0.8em;">Image by <a href="http://www.flickr.com/photos/87251222@N00/2884366929">libbyrosof</a> via Flickr</dd>
</dl>
</div>
</div>
<p style="margin-bottom: 0cm;">The debate on the &#8220;one assertion&#8221; principle continues. Pádraic Brady and I agree on the general principle, but <a href="http://blog.astrumfutura.com/archives/388-Unit-Testing-One-Test,-One-Assertion-Why-It-Works.html">since he is so categorical in his second article</a>, I needed to inspect his reasoning more closely. It&#8217;s clear that our disagreement is more than superficial. The more I think about it, the more I disagree.</p>
<h2 style="margin-bottom: 0cm;">The state of the art</h2>
<p style="margin-bottom: 0cm;">Pádraic maintains that one assertion per test is a rule that should always be followed unless there is a specific good reason to break it. I prefer it as a guideline, as does Robert C. Martin in the book <a href="http://www.amazon.com/gp/product/0132350882?ie=UTF8&amp;tag=phinac-20&amp;linkCode=as2&amp;camp=1789&amp;creative=9325&amp;creativeASIN=0132350882">Clean Code</a>. The reference is not intended as an appeal to authority to &#8220;prove&#8221; that I&#8217;m right. I&#8217;m just making the point that I think this reflects the current state of the art, which is not necessarily perfect, of course.</p>
<h2 style="margin-bottom: 0cm;">How small is a behavior?</h2>
<p style="margin-bottom: 0cm;">So what is the basis for wanting to enforce this principle as a strict rule? Pádraic<span style="font-style: normal;"> claims that &#8220;1 behavior == 1 assertion&#8221;.</span></p>
<p style="margin-bottom: 0cm;"><span style="font-style: normal;"><br />
</span></p>
<p style="margin-bottom: 0cm; font-style: normal;"><span style="font-style: normal;">How does that make sense? </span>The starting point is <a class="zem_slink" title="Behavior Driven Development" rel="wikipedia" href="http://en.wikipedia.org/wiki/Behavior_Driven_Development">Behavior-Driven Development</a> (BDD). I agree with the BDD principle that one test should test one behavior—as a guideline. It&#8217;s makes tests more focused, more readable and makes for better error messages when tests fail. Above all, it forces you to <em>think</em><span style="font-style: normal;"> about what is a behavior and what is not. And the idea of one assertion per test drives that thinking process.</span></p>
<p style="margin-bottom: 0cm; font-style: normal;"><span style="font-style: normal;"><br />
</span></p>
<p style="margin-bottom: 0cm; font-style: normal;">From that point of view, &#8220;1 behavior == 1 assertion&#8221; seems totally logical. Unfortunately, it misses the point. It introduces a mechanical equivalence that negates the very thinking process I was just advocating. It makes the conceptual notion of a single, atomic behavior dependent on syntax and on the implementation specifics of a test framework.</p>
<p style="margin-bottom: 0cm; font-style: normal;">
<p style="margin-bottom: 0cm; font-style: normal;">For example, let&#8217;s say we want to test that a product object represents a certain model, a Logitech Harmony One remote control.</p>
<pre class="brush: php">
$this-&gt;assertEquals(&#039;Logitech&#039;, $product-&gt;make);
$this-&gt;assertEquals(&#039;Harmony One&#039;, $product-&gt;model);
</pre>
<p>It&#8217;s two assertions, but we&#8217;re only testing for one thing. We just want to make sure this product is what it should be and not something else. But let&#8217;s think some more about it. There are several ways we could make this into one assertion instead of two. This one, for instance:</p>
<pre class="brush: php">

$this-&gt;assertTrue(
&#039;Logitech&#039; == $product-&gt;make  &amp;amp;&amp;amp;  &#039;Harmony One&#039; == $product-&gt;model);
</pre>
<p>Not very pretty, is it? I think we can agree that this is not an improvement. More importantly, it doesn&#8217;t change the nature of what we are testing. We didn&#8217;t change <em>what</em> we&#8217;re testing, we just changed the way we express the test. A better way to get one assertion would be to make a custom assertion method such as assertLogitechHarmonyOne(). That would be more readable, but it wouldn&#8217;t help our statistics. The test framework would still see two assertions and report this as a 1 to 2 relationship between tests and assertions. And again, what we&#8217;re testing remains the same.</p>
<p>It gets worse. We&#8217;re testing one thing, but are we testing one behavior? No, we&#8217;re just testing the outcome. This is another logical flaw in the &#8220;1 behavior == 1 assertion&#8221; equivalence. A behavior is not tested by assertions, it&#8217;s tested by a complete test method. What happens before the assertions also counts, obviously. You could have multiple behaviors in the test with a single outcome that could be tested by a single assertion.</p>
<p>But I haven&#8217;t mentioned the simplest way to get one assertion per test. Just delete all assertions but one. It&#8217;s cheating, and problably toxic the quality of your tests, but it satisfies the criterion and generates good-looking statistics. It&#8217;s the thing that&#8217;s bound to happen if you try to force this on developers.</p>
<p>Better not use the &#8220;one assertion&#8221; principle as a strait jacket. It works better as exercise equipment to jog your mind.</p>
<div class="zemanta-pixie" style="margin-top: 10px; height: 15px;"><a class="zemanta-pixie-a" title="Zemified by Zemanta" href="http://reblog.zemanta.com/zemified/fd558844-2fd4-411a-aa77-6613d27ea6ba/"><img class="zemanta-pixie-img" style="border: medium none; float: right;" src="http://img.zemanta.com/reblog_a.png?x-id=fd558844-2fd4-411a-aa77-6613d27ea6ba" alt="Reblog this post [with Zemanta]" /></a></div>
<p class="addtoany_share_save_container">
    <a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?sitename=PHP%20in%20Action&amp;siteurl=http%3A%2F%2Fblog.agilephp.com%2F&amp;linkname=One%20behavior%20%21%3D%20one%20assertion&amp;linkurl=http%3A%2F%2Fblog.agilephp.com%2F2009%2F02%2F21%2Fone-behavior-one-assertion%2F"><img src="http://blog.agilephp.com/wp-content/plugins/add-to-any/share_save_256_24.png" width="256" height="24" alt="Share/Save/Bookmark"/></a>

	</p>]]></content:encoded>
			<wfw:commentRss>http://blog.agilephp.com/2009/02/21/one-behavior-one-assertion/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>One assertion per test—always?</title>
		<link>http://blog.agilephp.com/2009/02/15/one-assertion-per-test%e2%80%94always/</link>
		<comments>http://blog.agilephp.com/2009/02/15/one-assertion-per-test%e2%80%94always/#comments</comments>
		<pubDate>Sun, 15 Feb 2009 19:53:02 +0000</pubDate>
		<dc:creator>dagfinn</dc:creator>
				<category><![CDATA[Clean code]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[Unit Tests]]></category>

		<guid isPermaLink="false">http://blog.agilephp.com/?p=1468</guid>
		<description><![CDATA[



Image via Wikipedia



Pádraic Brady pleads for the principle that a unit test method should have only one assertion. His point is perfectly valid; there are several good reasons why one assertion per method is a smart guideline when writing unit tests. But since he drags up the term &#8220;lazy&#8221;, I feel the need for soul-searching [...]]]></description>
			<content:encoded><![CDATA[<div class="zemanta-img" style="margin: 1em; display: block;">
<div>
<dl class="wp-caption alignleft" style="width: 212px;">
<dt class="wp-caption-dt"><a href="http://commons.wikipedia.org/wiki/Image:Louis_the_Pious.jpg"><img title="Louis the Pious doing penance at Attigny in 822" src="http://upload.wikimedia.org/wikipedia/commons/thumb/9/92/Louis_the_Pious.jpg/202px-Louis_the_Pious.jpg" alt="Louis the Pious doing penance at Attigny in 822" width="202" height="186" /></a></dt>
<dd class="wp-caption-dd zemanta-img-attribution" style="font-size: 0.8em;">Image via <a href="http://commons.wikipedia.org/wiki/Image:Louis_the_Pious.jpg">Wikipedia</a></dd>
</dl>
</div>
</div>
<p><span class="fn">Pádraic Brady</span><span class="plugin_comment_author"> <a href="http://blog.astrumfutura.com/index.php?url=archives/387-Unit-Testing-Multiple-Assertions-And-LazyShallow-Testing-Are-Evil.html">pleads for the principle that a unit test method should have only one assertion</a>. His point is perfectly valid; there are several good reasons why one assertion per method is a smart guideline when writing unit tests. But since he drags up the term &#8220;lazy&#8221;, I feel the need for soul-searching and confession.</span></p>
<p>The average for my current unit tests still hovers around three or four assertions per test method. And when I think about it, I see that the reason is that I&#8217;m not always taking the time to practice &#8220;one assertion per test&#8221; as a strict rule. Instead, it&#8217;s part of my problem-solving strategy when I start to get confused by my own tests; when the tests get so complex that I can&#8217;t quickly understand what&#8217;s going on where. <a class="zem_slink" title="Mock object" rel="wikipedia" href="http://en.wikipedia.org/wiki/Mock_object">Mock objects</a>, in particular, contribute to this, since mock expectations specify what should happen later instead of testing what&#8217;s already happened.</p>
<p>Anyway, when I encounter this problem, known unsurprisingly as Complex Test or <a href="http://xunitpatterns.com/Obscure%20Test.html">Obscure test</a>, my first impulse is to make sure the code I&#8217;m testing (known as the <a class="zem_slink" title="System Under Test" rel="wikipedia" href="http://en.wikipedia.org/wiki/System_Under_Test">system under test</a> or SUT) is not too complex. Classes that are too large and methods that are too long are my prime examples of code that&#8217;s hard to test in a simple way. After I check the SUT, I look at the tests, and yes, then one of my goals is to move toward the &#8220;one assertion&#8221; ideal. It really is helpful at that point.</p>
<p>I&#8217;m open to the possibility that I should be more consistent in applying this principle. He may be right that there is a danger of &#8220;lazy/shallow testing&#8221;. In any case, as with all code, bugs and deficiencies are easier to spot the more readable it is.</p>
<div class="zemanta-pixie" style="margin-top: 10px; height: 15px;"><a class="zemanta-pixie-a" title="Zemified by Zemanta" href="http://reblog.zemanta.com/zemified/6e7736b2-1ac9-453f-9394-9b7870d0e941/"><img class="zemanta-pixie-img" style="border: medium none; float: right;" src="http://img.zemanta.com/reblog_a.png?x-id=6e7736b2-1ac9-453f-9394-9b7870d0e941" alt="Reblog this post [with Zemanta]" /></a></div>
<p class="addtoany_share_save_container">
    <a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?sitename=PHP%20in%20Action&amp;siteurl=http%3A%2F%2Fblog.agilephp.com%2F&amp;linkname=One%20assertion%20per%20test%E2%80%94always%3F&amp;linkurl=http%3A%2F%2Fblog.agilephp.com%2F2009%2F02%2F15%2Fone-assertion-per-test%25e2%2580%2594always%2F"><img src="http://blog.agilephp.com/wp-content/plugins/add-to-any/share_save_256_24.png" width="256" height="24" alt="Share/Save/Bookmark"/></a>

	</p>]]></content:encoded>
			<wfw:commentRss>http://blog.agilephp.com/2009/02/15/one-assertion-per-test%e2%80%94always/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>How to test everything</title>
		<link>http://blog.agilephp.com/2008/11/27/how-to-test-everything/</link>
		<comments>http://blog.agilephp.com/2008/11/27/how-to-test-everything/#comments</comments>
		<pubDate>Thu, 27 Nov 2008 23:19:04 +0000</pubDate>
		<dc:creator>dagfinn</dc:creator>
				<category><![CDATA[Application design]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">http://localhost/wordpress/?p=1389</guid>
		<description><![CDATA[They say there&#8217;s no free lunch, but at least there&#8217;s free breakfast. Last week I attended a &#8220;breakfast seminar&#8221; with Robert C. Martin (Uncle Bob). There really was free food.
Anyway, Uncle Bob held an extremely entertaining and useful introduction to the FitNesse testing tool. He got me hooked on it, but I&#8217;m even more fascinated [...]]]></description>
			<content:encoded><![CDATA[<p>They say there&#8217;s no free lunch, but at least there&#8217;s free breakfast. Last week I attended a &#8220;breakfast seminar&#8221; with Robert C. Martin (Uncle Bob). There really was free food.</p>
<p>Anyway, Uncle Bob held an extremely entertaining and useful introduction to the <a href="http://fitnesse.org/">FitNesse</a> testing tool. He got me hooked on it, but I&#8217;m even more fascinated by something else. I asked him, &#8220;what is the nature of the test API [you've been talking about]?&#8221; He answered that question, and also another, more general one: What is a good strategy for complete automated integration and acceptance testing of an application? That was the question I really wanted to ask, only I wasn&#8217;t quite aware that I wanted to ask it. And, impressively, he answered it anyway.</p>
<p>As I understood it, the strategy he outlined was as follows. The diagram is probably only a mild perversion of the one he drew on the whiteboard.</p>
<p><img class="alignnone size-full wp-image-1442" title="totaltest" src="http://blog.agilephp.com/wp-content/uploads/2008/11/totaltest.jpg" alt="totaltest" width="510" height="370" /></p>
<ul>
<li>Test all features through a test API just underneath the user   inteface. These are the main acceptance tests; the ones that are used to   decide whether a feature is <strong>done</strong> or not done yet.</li>
<li>Test the user interface in isolation, running calls to a fake system underneath.</li>
<li>Run just a <em>few</em> tests all the way through the application to   test the plumbing between the user interface and the rest of the   system.</li>
</ul>
<p>The reason why we don&#8217;t want all our acceptance tests to go through the user interface is that the user interface has a tendency to change often, and changes in the user interface tend to breaks lots of tests.</p>
<p>This resonates reasonably well with my experience, although I think testing &#8220;everything&#8221; through the user interface can work sometimes, if the user interface is a very plain one.</p>
<p>In addition, we want unit tests for each small piece of the application. The tests outlined above are just catch the few bugs that are not detected by the unit tests.</p>
<p class="addtoany_share_save_container">
    <a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?sitename=PHP%20in%20Action&amp;siteurl=http%3A%2F%2Fblog.agilephp.com%2F&amp;linkname=How%20to%20test%20everything&amp;linkurl=http%3A%2F%2Fblog.agilephp.com%2F2008%2F11%2F27%2Fhow-to-test-everything%2F"><img src="http://blog.agilephp.com/wp-content/plugins/add-to-any/share_save_256_24.png" width="256" height="24" alt="Share/Save/Bookmark"/></a>

	</p>]]></content:encoded>
			<wfw:commentRss>http://blog.agilephp.com/2008/11/27/how-to-test-everything/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Zend_Test</title>
		<link>http://blog.agilephp.com/2008/11/18/zend_test/</link>
		<comments>http://blog.agilephp.com/2008/11/18/zend_test/#comments</comments>
		<pubDate>Tue, 18 Nov 2008 23:45:54 +0000</pubDate>
		<dc:creator>dagfinn</dc:creator>
				<category><![CDATA[Frameworks]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">http://localhost/wordpress/?p=1392</guid>
		<description><![CDATA[ I admit I don&#8217;t follow Zend Framework very closely, since I haven&#8217;t been using it for any serious work. But I did write a piece about testing a Zend Framework action controller with View Helpers. 
 This might need updating, since the testing capabilities of the Zend Framework have grown substantially since then. In [...]]]></description>
			<content:encoded><![CDATA[<p> I admit I don&#8217;t follow Zend Framework very closely, since I haven&#8217;t been using it for any serious work. But I did write a piece about <a href="http://www.reiersol.com/blog/1_php_in_action/archive/70_testing_a_zend_framework_action_controller_with_view_helpers.html">testing a Zend Framework action controller with View Helpers</a>. </p>
<p> This might need updating, since the testing capabilities of the Zend Framework have grown substantially since then. In particular, there is now a component called <a href="http://framework.zend.com/manual/en/zend.test.html">Zend_Test</a>. I haven&#8217;t had time to study it closely yet, but I hope to do so soon. </p>
<p class="addtoany_share_save_container">
    <a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?sitename=PHP%20in%20Action&amp;siteurl=http%3A%2F%2Fblog.agilephp.com%2F&amp;linkname=Zend_Test&amp;linkurl=http%3A%2F%2Fblog.agilephp.com%2F2008%2F11%2F18%2Fzend_test%2F"><img src="http://blog.agilephp.com/wp-content/plugins/add-to-any/share_save_256_24.png" width="256" height="24" alt="Share/Save/Bookmark"/></a>

	</p>]]></content:encoded>
			<wfw:commentRss>http://blog.agilephp.com/2008/11/18/zend_test/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>More beautiful code</title>
		<link>http://blog.agilephp.com/2008/11/08/more-beautiful-code/</link>
		<comments>http://blog.agilephp.com/2008/11/08/more-beautiful-code/#comments</comments>
		<pubDate>Sat, 08 Nov 2008 05:34:42 +0000</pubDate>
		<dc:creator>dagfinn</dc:creator>
				<category><![CDATA[Application design]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">http://localhost/wordpress/?p=1395</guid>
		<description><![CDATA[ I got some interesting comments to my previous post on &#8220;beautiful code&#8221;. Some were pretty strong disagreements. 
 So am I wrong? Did I get carried away? Did my critical faculty go on vacation somewhere nice and sunny? I admit that sometimes I deliberately look at the positive and ignore the negative. (And sometimes [...]]]></description>
			<content:encoded><![CDATA[<p> I got some interesting comments to my previous post on <a href="http://www.reiersol.com/blog/1_php_in_action/archive/168_beautiful_code.html">&#8220;beautiful code&#8221;</a>. Some were pretty strong disagreements. </p>
<p> So am I wrong? Did I get carried away? Did my critical faculty go on vacation somewhere nice and sunny? I admit that sometimes I deliberately look at the positive and ignore the negative. (And sometimes I do the opposite; It&#8217;s a good exercise if you&#8217;re careful.) </p>
<p> I wasn&#8217;t drunk, anyway. But let me take a closer look at the particular line of code I was praising: </p>
<pre class="brush: php">
$this-&gt;assertThat($form-&gt;hasSelect(withName(&#039;statusConfirm&#039;))-&gt;hasValues(),
    array(&#039;Yes&#039;,&#039;No&#039;));
</pre>
<p> My main point is that it&#8217;s close to plain English. Not everyone agrees that that&#8217;s a good thing, but I argue that we&#8217;re built (genetically wired, in fact) to understand natural languages, not program code. Therefore code should be easier to understand when it approximates natural language and expression. And we&#8217;re trying to create or approximate a Domain Specific Language (DSL), which should express exactly what&#8217;s required for the domain and not the demands of the technical implementation. </p>
<p> So for this experiment, let&#8217;s translate this one into a (plain English sentence: </p>
<p> Assert that Form (this particular one) has a select menu with the name &#8220;statusConfirm&#8221; and values &#8220;yes&#8221; and &#8220;no&#8221; </p>
<p> Translating back into code, it might look more like this: </p>
<pre class="brush: php">
$this-&gt;assertThat($form)-&gt;hasSelect()-&gt;withName(&#039;statusConfirm&#039;)-&gt;andValues(&#039;yes&#039;,&#039;no&#039;);
</pre>
<p> To me, this is even more natural than the other one. I think we&#8217;ve gotten rid of some syntax that has to do with implementation details rather than making the API simple to use. </p>
<p> It also seems clear to me how this could be implemented. All of the method calls could be to an assertion object that would take all these various inputs and always return itself at the end of the method so you can chain the calls in what&#8217;s known as a fluent interface. </p>
<p class="addtoany_share_save_container">
    <a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?sitename=PHP%20in%20Action&amp;siteurl=http%3A%2F%2Fblog.agilephp.com%2F&amp;linkname=More%20beautiful%20code&amp;linkurl=http%3A%2F%2Fblog.agilephp.com%2F2008%2F11%2F08%2Fmore-beautiful-code%2F"><img src="http://blog.agilephp.com/wp-content/plugins/add-to-any/share_save_256_24.png" width="256" height="24" alt="Share/Save/Bookmark"/></a>

	</p>]]></content:encoded>
			<wfw:commentRss>http://blog.agilephp.com/2008/11/08/more-beautiful-code/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Beautiful code</title>
		<link>http://blog.agilephp.com/2008/11/03/beautiful-code/</link>
		<comments>http://blog.agilephp.com/2008/11/03/beautiful-code/#comments</comments>
		<pubDate>Mon, 03 Nov 2008 13:36:56 +0000</pubDate>
		<dc:creator>dagfinn</dc:creator>
				<category><![CDATA[Application design]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Refactoring]]></category>
		<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">http://localhost/wordpress/?p=1398</guid>
		<description><![CDATA[     Max Horwath has published his slides on Making       Selenium Test Writing easier using a DSL onlinefrom IPC 2008. Let me     quote the whole short description:     
 Implementing automated tests by using Seleniums API methods has several [...]]]></description>
			<content:encoded><![CDATA[<p>     Max Horwath has published his slides on <a href="http://www.maxhorvath.com/2008/10/slide-for-ipc-session-on-making-selenium-test-writing-easier-using-a-dsl-online.html">Making       Selenium Test Writing easier using a DSL online</a>from <a href="http://it-republik.de/php/phpconference/">IPC 2008</a>. Let me     quote the whole short description:     </p>
<blockquote><p> Implementing automated tests by using Seleniums API methods has several drawbacks. Selenium is great for what it does, providing a generic framework for testing a generic application. Using the Testing_SeleniumDSL framework, I will show you how to create your own Domain Specific Language (DSL), which would allow you to write tests in the language of your business rather than in Seleniums language.  </p></blockquote>
<p> I&#8217;m quite impressed by the examples he presents, such as: </p>
<pre class="brush: php">
$this-&gt;assertThat($form-&gt;hasSelect(withName(&#039;statusConfirm&#039;))-&gt;hasValues(),
    array(&#039;Yes&#039;,&#039;No&#039;));
</pre>
<p> This is truly expressive, readable code. I&#8217;ve refactored web test code in this direction many times, but I admit I  never got quite this far. </p>
<p>  The DSL is planned as an open source release. It would be interesting to try something  similar for the SimpleTest web tester, which is my favorite for testing web interfaces without too much JavaScript. (Based on <a href="http://www.reiersol.com/blog/1_php_in_action/archive/25_paparrazzi_testing.html">the paparrazzi principle</a>.) </p>
<p class="addtoany_share_save_container">
    <a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?sitename=PHP%20in%20Action&amp;siteurl=http%3A%2F%2Fblog.agilephp.com%2F&amp;linkname=Beautiful%20code&amp;linkurl=http%3A%2F%2Fblog.agilephp.com%2F2008%2F11%2F03%2Fbeautiful-code%2F"><img src="http://blog.agilephp.com/wp-content/plugins/add-to-any/share_save_256_24.png" width="256" height="24" alt="Share/Save/Bookmark"/></a>

	</p>]]></content:encoded>
			<wfw:commentRss>http://blog.agilephp.com/2008/11/03/beautiful-code/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Many test methods versus custom assertions</title>
		<link>http://blog.agilephp.com/2008/09/16/many-test-methods-versus-custom-assertions/</link>
		<comments>http://blog.agilephp.com/2008/09/16/many-test-methods-versus-custom-assertions/#comments</comments>
		<pubDate>Tue, 16 Sep 2008 22:27:35 +0000</pubDate>
		<dc:creator>dagfinn</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">http://localhost/wordpress/?p=1415</guid>
		<description><![CDATA[ This is something I posted to the Sitepoint PHP Application Design Forum with a little bit of added background. 
 The background is the idea that unit test methods, for the sake of readability, should test only one single behavior. This may mean several tests for one method under test, since one method may [...]]]></description>
			<content:encoded><![CDATA[<p> This is something I posted to the Sitepoint PHP Application Design Forum with a little bit of added background. </p>
<p> The background is the idea that unit test methods, for the sake of readability, should test only one single behavior. This may mean several tests for one method under test, since one method may have several behaviors. One version of this is the notion of <a href="http://blog.jayfields.com/2007/06/testing-one-assertion-per-test.html">one assertion per test</a>. </p>
<p> Web tests are not quite the same thing as unit test. On the Sitepoint forum, in a discussion on web testing, I said: </p>
<p>
<blockquote> It&#8217;s also very slow to have too many separate test methods. So instead, to make it readable, I tend to use custom assertions. </p></blockquote>
<p>  Marcus Baker asked me for examples. I tried to illustrate the principle with an example that may be a something of a caricature. </p>
<p>  If performance and speed were not an issue, I might do something like this, keeping each test method short and sweet: PHP Code: </p>
<p>
<pre class="brush: php">
class ArticleListTest extends WebTestCase {

    function setUp() {
        // Some code to log in and go to the article list page
        //....
    }

    function testHttpStatusOk() {
        //...
    }

    function testHasCorrectTitle() {
        //...
    }

    function testHasNoDebugInfoAccidentallyLeftBehind() {
        //...
    }
}
</pre>
</p>
<p>  Unfortunately, it would be nauseatingly slow, since you have to traverse some web pages for every single test method. Instead, I can try to get similar small chunking by using custom assertions: PHP Code: </p>
<p>
<pre class="brush: php">
class VariousPageTests extends WebTestCase {

    function testLoginAndArticleList() {
        // Some code to log in
        //....

        $this-&gt;assertStartPage();

        // Some code to go to article list page
        //....

        $this-&gt;assertArticleListPage();
    }

    function assertArticleListPage() {
        $this-&gt;assertHttpStatusOk();
        $this-&gt;assertHasCorrectTitle();
        $this-&gt;assertHasNoDebugInfoAccidentallyLeftBehind();
    }

    function assertStartPage() {
        //
    }
}
</pre>
</p>
<p> This way, the code has names for approximately the same details as in the first example.</p>
<p class="addtoany_share_save_container">
    <a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?sitename=PHP%20in%20Action&amp;siteurl=http%3A%2F%2Fblog.agilephp.com%2F&amp;linkname=Many%20test%20methods%20versus%20custom%20assertions&amp;linkurl=http%3A%2F%2Fblog.agilephp.com%2F2008%2F09%2F16%2Fmany-test-methods-versus-custom-assertions%2F"><img src="http://blog.agilephp.com/wp-content/plugins/add-to-any/share_save_256_24.png" width="256" height="24" alt="Share/Save/Bookmark"/></a>

	</p>]]></content:encoded>
			<wfw:commentRss>http://blog.agilephp.com/2008/09/16/many-test-methods-versus-custom-assertions/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Testing a Zend Framework action controller with View Helpers</title>
		<link>http://blog.agilephp.com/2008/06/10/testing-a-zend-framework-action-controller-with-view-helpers/</link>
		<comments>http://blog.agilephp.com/2008/06/10/testing-a-zend-framework-action-controller-with-view-helpers/#comments</comments>
		<pubDate>Tue, 10 Jun 2008 22:40:23 +0000</pubDate>
		<dc:creator>dagfinn</dc:creator>
				<category><![CDATA[Application design]]></category>
		<category><![CDATA[Frameworks]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">http://localhost/wordpress/?p=1372</guid>
		<description><![CDATA[ I came across a Zend Framework (ZF) example I wanted to refactor. You really have to have unit test coverage to refactor effectively, and since there were no tests, I started trying to find out how to test it. There didn&#8217;t seem to be a wealth of information available on the web, so I&#8217;ve [...]]]></description>
			<content:encoded><![CDATA[<p> I came across a Zend Framework (ZF) example I wanted to refactor. You really have to have unit test coverage to refactor effectively, and since there were no tests, I started trying to find out how to test it. There didn&#8217;t seem to be a wealth of information available on the web, so I&#8217;ve tried to figure it out by myself. </p>
<p> Several challenges presented themselves. It might seem as if the ZF controller system is not optimally designed for testing action controllers, but there are always ways to get around obstacles. </p>
<p> A ZF action controller (which is really just a group of actions collected in a class) always extends Zend_Controller_Action. (For an introduction, see <a href="http://framework.zend.com/wiki/display/ZFDEV/Official+ZF+QuickStart">the official QuickStart</a>). </p>
<p> The one I was playing with was an authentication controller: </p>
<pre class="brush: php">
class AuthController extends Zend_Controller_Action...
</pre>
<p> That&#8217;s all we need to know about the specific class under test for now. </p>
<p> I always like to set up the basic objects for the test as instance variables in the setUp() method, so that I can write many small test methods exercising them in various ways. To even get started testing an action controller, we need to instantiate it outside its usual environment, the Zend Front Controller. It requires a  request object and a response object: </p>
<pre class="brush: php">
class AuthControllerTest extends UnitTestCase {

  function setUp() {
    $this-&gt;request = new Zend_Controller_Request_Http();
    $this-&gt;response = new Zend_Controller_Response_Cli();
    $this-&gt;controller = new AuthController($this-&gt;request,$this-&gt;response);
  }
</pre>
<p> (I&#8217;m using SimpleTest here, but the same techniques should apply to PHPUnit with different syntax.) </p>
<p> The challenge increases increase when we want to use View Helpers, such as the flash messenger. When programming the action controller, it looks like this: </p>
<pre class="brush: php">
$flashMessenger = $this-&gt;_helper-&gt;FlashMessenger;
</pre>
<p> To test this meaningfully, we need to replace the flash messenger with a mock object. That means also having to replace $this->_helper, which is a &#8220;helper broker&#8221; object. </p>
<p> First, we generate the mock classes: </p>
<pre class="brush: php">
Mock::generate(&#039;Zend_Controller_Action_HelperBroker&#039;,&#039;MockHelperBroker&#039;);
Mock::generate(&#039;Zend_Controller_Action_Helper_FlashMessenger&#039;,&#039;MockFlashMessenger&#039;);
</pre>
<p> Unfortunately, the helper broker is a protected instance variable, with no apparent way to change it. But it&#8217;s easy to fix by adding a setter method to the action controller class: </p>
<pre class="brush: php">
class AuthController extends Zend_Controller_Action...

  public function setHelperBroker($helperBroker) {
    $this-&gt;_helper = $helperBroker;
  }
</pre>
<p> This allows us to set up the mock helper broker and let it return the flash messenger: </p>
<pre class="brush: php">
$this-&gt;helperBroker = new MockHelperBroker;
$this-&gt;controller-&gt;setHelperBroker($this-&gt;helperBroker);

$this-&gt;flashMessenger = new MockFlashMessenger;
$this-&gt;helperBroker-&gt;setReturnValue(
  &#039;__get&#039;,$this-&gt;flashMessenger,array(&#039;FlashMessenger&#039;));
</pre>
<p> Now we can use the mock objects to test an action which is supposed to send a flash message:
<pre class="brush: php">
function testIdentifyActionSendsFlashMessage() {
    $this-&gt;flashMessenger-&gt;expectOnce(
        &#039;addMessage&#039;,
        array(&#039;Please provide a username and password.&#039;));
    $this-&gt;controller-&gt;identifyAction();
}
</pre>
<p> That&#8217;s as far as I&#8217;m going with this now. There may be more later. </p>
<p> </body></p>
<p class="addtoany_share_save_container">
    <a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?sitename=PHP%20in%20Action&amp;siteurl=http%3A%2F%2Fblog.agilephp.com%2F&amp;linkname=Testing%20a%20Zend%20Framework%20action%20controller%20with%20View%20Helpers&amp;linkurl=http%3A%2F%2Fblog.agilephp.com%2F2008%2F06%2F10%2Ftesting-a-zend-framework-action-controller-with-view-helpers%2F"><img src="http://blog.agilephp.com/wp-content/plugins/add-to-any/share_save_256_24.png" width="256" height="24" alt="Share/Save/Bookmark"/></a>

	</p>]]></content:encoded>
			<wfw:commentRss>http://blog.agilephp.com/2008/06/10/testing-a-zend-framework-action-controller-with-view-helpers/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>The psychology of test-driven development</title>
		<link>http://blog.agilephp.com/2008/06/02/the-psychology-of-test-driven-development/</link>
		<comments>http://blog.agilephp.com/2008/06/02/the-psychology-of-test-driven-development/#comments</comments>
		<pubDate>Mon, 02 Jun 2008 21:46:48 +0000</pubDate>
		<dc:creator>dagfinn</dc:creator>
				<category><![CDATA[Application design]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">http://localhost/wordpress/?p=1369</guid>
		<description><![CDATA[Silvan Mühlemann has written an excellent piece titled Unit testing makes coding more fun.
 I keep saying that sort of thing, too. I also say it raises your IQ. But when I attended a presentation by Uncle Bob (Robert C. Martin) last summer, I was mildly shocked to see that he described TDD as tedious [...]]]></description>
			<content:encoded><![CDATA[<p>Silvan Mühlemann has written an excellent piece titled <a href="http://techblog.tilllate.com/2008/06/01/unit-testing-makes-coding-more-fun/">Unit testing makes coding more fun</a>.
<p> I keep saying that sort of thing, too. I also say it <a href="http://www.reiersol.com/blog/1_php_in_action/archive/13_fake_it_and_raise_your_iq.html">raises your IQ</a>. But when I attended a presentation by Uncle Bob (Robert C. Martin) last summer, I was mildly shocked to see that he described TDD as tedious (but a requirement for professionalism). I confronted him politely about it, emphasizing that above all I find TDD relaxing.  Why? Because bugs bug me. I get stressed when I have to search for a bug and even more so when it takes much longer than expected (I&#8217;m sure that never happens to you). With TDD, bugs are always small and easy to locate. </p>
<p> Uncle Bob seemed to agree with me as far as his own experience was concerned, but he believed that many developers found the non-TDD process exciting rather than stressful. </p>
<p> Sounds weird to me. But I know from long experience that sometimes people are different from me in ways that make reality seem stranger than fiction. </p>
<p class="addtoany_share_save_container">
    <a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?sitename=PHP%20in%20Action&amp;siteurl=http%3A%2F%2Fblog.agilephp.com%2F&amp;linkname=The%20psychology%20of%20test-driven%20development&amp;linkurl=http%3A%2F%2Fblog.agilephp.com%2F2008%2F06%2F02%2Fthe-psychology-of-test-driven-development%2F"><img src="http://blog.agilephp.com/wp-content/plugins/add-to-any/share_save_256_24.png" width="256" height="24" alt="Share/Save/Bookmark"/></a>

	</p>]]></content:encoded>
			<wfw:commentRss>http://blog.agilephp.com/2008/06/02/the-psychology-of-test-driven-development/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
