<?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; Behavior Driven Development</title>
	<atom:link href="http://blog.agilephp.com/tag/behavior-driven-development/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>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>
	</channel>
</rss>
