<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Object Mentor Blog: 100% Code Coverage?</title>
    <link>http://blog.objectmentor.com/articles/2007/05/16/100-code-coverage</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description></description>
    <item>
      <title>100% Code Coverage?</title>
      <description>&lt;p&gt;Should you strive for 100% code coverage from your unit tests? It&amp;#8217;s probably not mandatory and if your goal is 100% coverage, you&amp;#8217;ll focus on that goal and not focus on writing the best tests for the &lt;i&gt;behavior&lt;/i&gt; of your code.&lt;/p&gt;


	&lt;p&gt;That said, here are some thoughts on why I still like to get close to 100%.&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;I&amp;#8217;m anal retentive. I don&amp;#8217;t like those little red bits in my coverage report. (Okay, that&amp;#8217;s not a good reason&amp;#8230;)&lt;/li&gt;
		&lt;li&gt;Every time I run the coverage report, I have to inspect all the uninteresting cases to find the interesting cases I should cover.&lt;/li&gt;
		&lt;li&gt;The tests are the specification and documentation of the code, so if something nontrivial but unexpected happens, there should still be a test to &amp;#8220;document&amp;#8221; the behavior, even if the test is hard to write.&lt;/li&gt;
		&lt;li&gt;Maybe those places without coverage are telling me to fix the design.&lt;/li&gt;
	&lt;/ul&gt;


	&lt;p&gt;I was thinking about this last point the other day when considering a bit of Java code that does a downcast (assume that&amp;#8217;s a good idea, for the sake of argument&amp;#8230;), wrapped in a try/catch block for the potential &lt;code&gt;ClassCastException&lt;/code&gt;:&lt;/p&gt;


&lt;pre&gt;
public void handleEvent (Event event) throws ApplicationException {
  try {
    SpecialEvent specialEvent = (SpecialEvent) event;
    doSomethingSpecial (specialEvent);
  } catch (ClassCastException cce) { 
    throw new ApplicationException(cce);
  }
}
&lt;/pre&gt;

	&lt;p&gt;To get 100% coverage, you would have to write a test that inputs an object of a different subtype of &lt;code&gt;Event&lt;/code&gt; to trigger coverage of the catch block. As we all know, these sorts of error-handling code blocks are typically the hardest to cover and ones we&amp;#8217;re most likely to ignore. (When was the last time you saw a &lt;code&gt;ClassCastException&lt;/code&gt; anyway?)&lt;/p&gt;


	&lt;p&gt;So my thought was this, we want 100% of the production code to be developed with &lt;span class="caps"&gt;TDD&lt;/span&gt;, so what if we made 100% coverage a similar goal? How would that change our designs? We might decide that since we have to write a test to cover this error-handling scenario, maybe we should rethink the scenario itself. Is it necessary? Could we eliminate the catch block with a better overall design, in this case, making sure that we test all &lt;i&gt;callers&lt;/i&gt; and ensure that they obey the method&amp;#8217;s &amp;#8216;contract&amp;#8217;? Should we just let the &lt;code&gt;ClassCastException&lt;/code&gt; fly out of the function and let a higher-level catch block handle it? After all, catching and rethrowing a different exception is slightly smelly and the code would be cleaner without the try/catch block. (For completeness, a good use of exception wrapping is to avoid namespace pollution. We might not want application layer A to know anything about layer C&amp;#8217;s exception types, so we wrap a C exception in an A exception, which gets passed through layer B&amp;#8230;)&lt;/p&gt;


	&lt;p&gt;100% coverage is often impossible or impractical, because of language or tool oddities. Still, if you give in early, you&amp;#8217;re overlooking some potential benefits.&lt;/p&gt;</description>
      <pubDate>Wed, 16 May 2007 22:50:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:1042dbcd-42aa-4dd1-a2de-3c371b910ba7</guid>
      <author>Dean Wampler</author>
      <link>http://blog.objectmentor.com/articles/2007/05/16/100-code-coverage</link>
      <category>Dean's Deprecations</category>
      <category>TDD</category>
      <category>code</category>
      <category>coverage</category>
      <trackback:ping>http://blog.objectmentor.com/articles/trackback/7523</trackback:ping>
    </item>
    <item>
      <title>"100% Code Coverage?" by Jason Bock</title>
      <description>&lt;p&gt;I agree &amp;#8211; I voiced the same opinions:&lt;/p&gt;


	&lt;p&gt;&lt;a href="http://www.jasonbock.net/JB/Default.aspx?blog=entry.8b942f550d504b0198837636db915a79" rel="nofollow"&gt;http://www.jasonbock.net/JB/Default.aspx?blog=entry.8b942f550d504b0198837636db915a79&lt;/a&gt;&lt;/p&gt;


	&lt;p&gt;I&amp;#8217;d say shooting for 90% coverage is feasible, but trying to hit 100% can become a futile exercise.&lt;/p&gt;</description>
      <pubDate>Mon, 17 Dec 2007 08:50:46 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:97fa53ec-2f15-4b57-a263-5e0edecd00e3</guid>
      <link>http://blog.objectmentor.com/articles/2007/05/16/100-code-coverage#comment-1245</link>
    </item>
  </channel>
</rss>
