<?xml version='1.0' encoding='UTF-8'?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/'><id>tag:blogger.com,1999:blog-30447482</id><updated>2007-07-23T21:20:33.335-04:00</updated><title type='text'>Test Often.</title><link rel='alternate' type='text/html' href='http://www.testoften.com/'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/30447482/posts/default'/><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://www.testoften.com/atom.xml'/><author><name>Codign Software</name></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>13</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-30447482.post-4150232366303271879</id><published>2007-07-23T20:32:00.000-04:00</published><updated>2007-07-23T21:20:33.363-04:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='unit testing'/><category scheme='http://www.blogger.com/atom/ns#' term='metrics'/><category scheme='http://www.blogger.com/atom/ns#' term='testability'/><title type='text'>7 Metrics to Improve Your Unit Testing</title><content type='html'>Unit testing is tough, regardless of manual or automated, test-first or test-after. With any of the approaches, there are certain metrics to pay attention to while you code. Staying within acceptable levels means your code will be much more testable, whether you unit test or not!&lt;br /&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;&lt;span style="font-weight: bold;"&gt;Parameters&lt;/span&gt; - Each parameter added to a method's signature increases the number of possible ways that method can be invoked, eventually creating an unmanageable combinatorial problem. By limiting the number of parameters to three or fewer, you can easily increase the testability of your code.&lt;/li&gt;&lt;/ul&gt;&lt;ul&gt;&lt;li&gt;&lt;span style="font-weight: bold;"&gt;Exceptions&lt;/span&gt; - Each thrown exception represents a distinct error condition that must be tested, and error conditions are very hard to test. Keeping the number of thrown exceptions to three or less greatly simplifies your testing effort.&lt;/li&gt;&lt;/ul&gt;&lt;ul&gt;&lt;li&gt;&lt;span style="font-weight: bold;"&gt;Cyclomatic Complexity&lt;/span&gt; - Cyclomatic Complexity (CC) is a measure of the decision logic in a method.  The more decisions your method has, the more ways it will behave. Keeping the number of CC paths to 10 or less greatly reduces the risk of defects as well as increases the overall testability of your code.&lt;/li&gt;&lt;/ul&gt;&lt;ul&gt;&lt;li&gt;&lt;span style="font-weight: bold;"&gt;Lines of Code&lt;/span&gt; - One of the oldest and simplest metrics used to measure a method's testability. A shorter method is easier to understand and test.&lt;/li&gt;&lt;/ul&gt;&lt;ul&gt;&lt;li&gt;&lt;span style="font-weight: bold;"&gt;Paths&lt;/span&gt; - This metric looks at distinct data paths. Data paths are paths that link a data element's definition to its use. In other words, if you define a data element, you should use it, and if you use it, you should test it. Data paths often overlap with Cyclomatic Complexity paths.&lt;/li&gt;&lt;/ul&gt;&lt;ul&gt;&lt;li&gt;&lt;span style="font-weight: bold;"&gt;Static Invocations &lt;/span&gt;- Methods under test invoke methods from external classes, and these external classes can usually be mocked or dummied up to behave exactly as required for an individual unit test. Static invocations of external methods often have an unchangeable behavior that thwarts even the best testing effort. Avoid this unnecessary coupling to external classes, and keep your external static invocations to a minimum.&lt;/li&gt;&lt;/ul&gt;&lt;ul&gt;&lt;li&gt;&lt;span style="font-weight: bold;"&gt;Anonymous Classes&lt;/span&gt; - Anonymous classes make unit testing difficult because they exist only within the method under test, and cannot themselves be unit tested. Although anonymous classes are convenient (especially for GUI programming), their lack of testability and mockability can hinder your testing effort.&lt;/li&gt;&lt;/ul&gt;</content><link rel='alternate' type='text/html' href='http://www.testoften.com/2007/07/7-metrics-to-improve-your-unit-testing.html' title='7 Metrics to Improve Your Unit Testing'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=30447482&amp;postID=4150232366303271879' title='0 Comments'/><link rel='replies' type='application/atom+xml' href='http://www.testoften.com/atom.xml' title='Post Comments'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/30447482/posts/default/4150232366303271879'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/30447482/posts/default/4150232366303271879'/><author><name>Codign Software</name></author></entry><entry><id>tag:blogger.com,1999:blog-30447482.post-549184939981314275</id><published>2007-05-02T21:48:00.000-04:00</published><updated>2007-05-03T08:28:54.691-04:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Define Use'/><category scheme='http://www.blogger.com/atom/ns#' term='unit testing'/><category scheme='http://www.blogger.com/atom/ns#' term='cyclomatic complexity'/><title type='text'>Define-Use (DU) Paths and Unit Testing</title><content type='html'>Yesterday, I &lt;a href="http://www.testoften.com/2007/04/cyclomatic-complexity-and-unit-testing.html"&gt;blogged &lt;/a&gt;about the use of cyclomatic complexity (CC) and unit testing.  Cyclomatic complexity is one way of looking at the paths. Specifically, CC measures the &lt;a href="http://en.wikipedia.org/wiki/Control_flow"&gt;control flow&lt;/a&gt; within a method.&lt;br /&gt;&lt;br /&gt;Another way of looking at paths is to analyze a methods' &lt;a href="http://en.wikipedia.org/wiki/Data_flow"&gt;data flow&lt;/a&gt;. A well researched metric for this analysis is called 'Define Use', or DU. A simplistic definition of DU is that if a variable is defined, it should be used, and if it is used, it should be tested.&lt;br /&gt;&lt;br /&gt;Specifically, a DU path for a variable is a path from the defining node to the usage node, thus the "flow of data". DU paths are very good at identifying problems because the error usually occurs between the definition and usage. &lt;br /&gt;&lt;br /&gt;CC, on the other hand, only looks at control flow.  A DU path has a better chance of being executed compared to a CC path, but there are also other factors that play into that as well. Which one is better for unit testing?  My personal opinion is a hybrid approach, one that looks at the linear combination of DU and CC paths. &lt;br /&gt;&lt;br /&gt;Take a look at the same example used in the last blog:&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://www.testoften.com/uploaded_images/code-720652.png"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;" src="http://www.testoften.com/uploaded_images/code-720650.png" border="0" alt="" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;We know that &lt;a href="http://www.testoften.com/2007/04/cyclomatic-complexity-and-unit-testing.html"&gt;total paths = 4 and CC = 3&lt;/a&gt;, but what about DU? And better yet, what about trying to combine DU and CC? Would that combination benefit you? Let's take a look:&lt;br /&gt;&lt;br /&gt;Path 1: TT (DU &amp; CC)&lt;br /&gt;Path 2: FT (DU &amp; CC)&lt;br /&gt;Path 3: TF (DU &amp; CC)&lt;br /&gt;Path 4: FF (DU)&lt;br /&gt;&lt;br /&gt;In this example, there are also 4 DU paths. If you only test the 3 CC paths, you would end up with at most 66% coverage (path 1 is unrealizable). If you test the DU &amp; CC combination, you would have 75% coverage.&lt;br /&gt;&lt;br /&gt;What do you think?</content><link rel='alternate' type='text/html' href='http://www.testoften.com/2007/05/define-use-du-paths-and-unit-testing.html' title='Define-Use (DU) Paths and Unit Testing'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=30447482&amp;postID=549184939981314275' title='0 Comments'/><link rel='replies' type='application/atom+xml' href='http://www.testoften.com/atom.xml' title='Post Comments'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/30447482/posts/default/549184939981314275'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/30447482/posts/default/549184939981314275'/><author><name>Codign Software</name></author></entry><entry><id>tag:blogger.com,1999:blog-30447482.post-5399162212363385756</id><published>2007-04-29T14:53:00.000-04:00</published><updated>2007-05-01T22:14:31.438-04:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='java'/><category scheme='http://www.blogger.com/atom/ns#' term='unit testing'/><category scheme='http://www.blogger.com/atom/ns#' term='cyclomatic complexity'/><category scheme='http://www.blogger.com/atom/ns#' term='junit'/><title type='text'>Cyclomatic Complexity and Unit Testing</title><content type='html'>I follow both Andrew Binstock and Andy Glover from Stelligent and have much respect for them both and would like to add to a &lt;a href="http://binstock.blogspot.com/2007/04/how-many-unit-tests-per-method-see.html"&gt;blog &lt;/a&gt;that was posted.&lt;br /&gt;&lt;br /&gt;I like their approach to using metrics to figure out the number of unit tests needed for a method. Actually, metrics should be used to determine the &lt;span style="font-weight:bold;"&gt;minimal &lt;span style="font-style:italic;"&gt;&lt;/span&gt;&lt;/span&gt;number of unit tests.  Cyclomatic complexity (CC) can be used for this, but so can other metrics like Define-Use. For those who don't know what CC is, it is defined as the minimal number of linearly independent paths within a unit of code (like a method).&lt;br /&gt;&lt;br /&gt;The blog above provides a correlation between the number of CC paths and the number of unit tests. I think any unit test is better than no unit test, but I believe their correlation only applies if you don't know what the CC paths are.&lt;br /&gt;&lt;br /&gt;For full disclosure, I am a co-founder of a product company (&lt;a href="http://www.codign.com"&gt;www.codign.com&lt;/a&gt;). The products are Eclipse plug-ins that design unit tests based on Cyclomatic Complexity AND Define-Use. We have a new release coming out very soon, which takes a very interesting spin on commodity metrics, but I'll blog about that later :)&lt;br /&gt;&lt;br /&gt;Having worked for Tom &lt;a href="http://www.mccabe.com"&gt;McCabe&lt;/a&gt; for many years (he came up with Cyclomatic Complexity), I understand the benefits and pitfalls to this metric. The calculation is very simple: CC equals the number of decisions + 1.  The &lt;a href="http://hissa.nist.gov/HHRFdata/Artifacts/ITLdoc/235/title.htm"&gt;proof &lt;/a&gt;is a bit more complicated :) , but that's the gist of it.&lt;br /&gt;&lt;br /&gt;So, for example, a method with 2 decisions has 4 total paths and 3 CC paths. Let's take this example:&lt;br /&gt;&lt;br /&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://www.testoften.com/uploaded_images/code-742096.png"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;" src="http://www.testoften.com/uploaded_images/code-742094.png" border="0" alt="" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;The questions I hear are, what are the three CC paths?  Shouldn't you try to test them all?  Can you test them all?  &lt;br /&gt;&lt;br /&gt;To figure out what the three CC paths are, you have to define your basis path (or path #1). This is very much a judgment call - it can be the longest path, the shortest path, the happy path, and so on.  For us at Codign, our Path 1 (aka our Basis Path) is the path that executes all the true branches in a method.  From there, or from any basis path, you simply work your way down by flipping decisions.&lt;br /&gt;&lt;br /&gt;With the example above, there are four TOTAL paths:&lt;br /&gt;&lt;br /&gt;Path 1: TT (both if statements execute 'TRUE')&lt;br /&gt;Path 2: TF (first if statement is true, second is false)&lt;br /&gt;Path 3: FT (first if statement is false, second is true)&lt;br /&gt;Path 4: FF (first if statement is false, second is false)&lt;br /&gt;&lt;br /&gt;There are 2 sets of CC paths:&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;Set 1&lt;/span&gt;&lt;br /&gt;Path 1: TT&lt;br /&gt;Path 2: FT&lt;br /&gt;Path 3: TF&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;Set 2&lt;/span&gt;&lt;br /&gt;Path 1: FF&lt;br /&gt;Path 2: TF&lt;br /&gt;Path 3: FT&lt;br /&gt;&lt;br /&gt;Look at the code again and you'll notice that Path 1 from Set 1 (TT) cannot be tested (it is unrealizable). But why not test the other two paths? Given this information, is the effort required to test the other remaining basis paths that difficult?&lt;br /&gt;&lt;br /&gt;As an aside, ever measure branch coverage? The above example can be tested with two tests (TF, FT) which would produce 100% branch coverage.&lt;br /&gt;&lt;br /&gt;My point is this ... don't take CC at face value. I don't agree that, for example, if you have 20 CC paths you should only write 10 unit tests.  I believe that, if a CC path is testable, you should test it.&lt;br /&gt;&lt;br /&gt;But like I said earlier, not all CC paths are realizable, so is there a better way to identify the minimal number of unit tests? Ones that are realizable and measurable?  There is, but I will blog about that later. :)</content><link rel='alternate' type='text/html' href='http://www.testoften.com/2007/04/cyclomatic-complexity-and-unit-testing.html' title='Cyclomatic Complexity and Unit Testing'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=30447482&amp;postID=5399162212363385756' title='0 Comments'/><link rel='replies' type='application/atom+xml' href='http://www.testoften.com/atom.xml' title='Post Comments'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/30447482/posts/default/5399162212363385756'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/30447482/posts/default/5399162212363385756'/><author><name>Codign Software</name></author></entry><entry><id>tag:blogger.com,1999:blog-30447482.post-3296780451861316887</id><published>2007-02-21T10:41:00.000-05:00</published><updated>2007-02-21T10:51:07.963-05:00</updated><title type='text'>Meet us at EclipseCON March 6th and 7th</title><content type='html'>I have the privilege of speaking and exhibiting at &lt;a href="http://www.eclipsecon.org"&gt;EclipseCON &lt;/a&gt;this year. Our short talk will be based on our article on &lt;a href="http://www.codign.com/pathbranchcode.html"&gt;The Differences and Benefits of Path, Branch and Code Coverage&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;Codign will also have a booth where we will be exhibiting our latest products, techniques, and integration into &lt;a href="http://www.artifactsoftware.com/products/lighthousepro.html"&gt;Lighthouse Pro&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;Stop by and say hi - we have some great giveaways, including stylin' baseball caps, great t-shirts and tons of pens. We are at booth #114.</content><link rel='alternate' type='text/html' href='http://www.testoften.com/2007/02/meet-us-at-eclipsecon-march-6th-and-7th.html' title='Meet us at EclipseCON March 6th and 7th'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=30447482&amp;postID=3296780451861316887' title='0 Comments'/><link rel='replies' type='application/atom+xml' href='http://www.testoften.com/atom.xml' title='Post Comments'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/30447482/posts/default/3296780451861316887'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/30447482/posts/default/3296780451861316887'/><author><name>Codign Software</name></author></entry><entry><id>tag:blogger.com,1999:blog-30447482.post-4532514991839720876</id><published>2007-02-14T20:53:00.000-05:00</published><updated>2007-02-14T22:01:52.398-05:00</updated><title type='text'>JUnit Best Practices, Part 4 -- Don't Be Too Assertive</title><content type='html'>&lt;a href="http://www.testoften.com/2006/09/junit-best-practices-part-2-keep-em.html"&gt;In a previous article&lt;/a&gt; we examined the pitfalls of invoking the method under test many times from the same JUnit test.   As a counterpart to that article, let's look at the case where a JUnit test contains multiple assertions.&lt;br /&gt;&lt;br /&gt;Assertions are the part of the JUnit test that actually determines whether or not your method under test worked as expected.  With a simple method under test, it's relatively easy to make that decision with just one assertion, but when the method under test performs more complex tasks, it's tempting to add multiple assertions to your JUnit tests to verify several different behaviors at once.  Despite appearances, however, this approach can actually increase your overall testing time and effort.&lt;br /&gt;&lt;br /&gt;Let's look at an example.  Suppose we have this method under test:&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.testoften.com/uploaded_images/img1-751007.png"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;" src="http://www.testoften.com/uploaded_images/img1-749844.png" border="0" alt="" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;code&gt;separator()&lt;/code&gt; scans its input String and fills the three StringBuffers with the vowels, consonants, and whitespace found therein.  A JUnit test for this method might look like this:&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.testoften.com/uploaded_images/img2-753905.png"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;" src="http://www.testoften.com/uploaded_images/img2-752718.png" border="0" alt="" /&gt;&lt;/a&gt;&lt;br /&gt;This seems like a natural test to write, but the problem here is that JUnit tests are designed to fail as soon as the first assertion fails.  In the worst case, when all three assertions would fail, you'll wind up with this scenario:&lt;br /&gt;&lt;br /&gt;&lt;u&gt;First test run&lt;/u&gt;&lt;br /&gt;The first assertion fails.&lt;br /&gt;Debug/fix the code.&lt;br /&gt;&lt;br /&gt;&lt;u&gt;Second test run&lt;/u&gt;&lt;br /&gt;The second assertion fails.&lt;br /&gt;Debug/fix the code.&lt;br /&gt;&lt;br /&gt;&lt;u&gt;Third test run&lt;/u&gt;&lt;br /&gt;The third assertion fails.&lt;br /&gt;Debug/fix the code.&lt;br /&gt;&lt;br /&gt;&lt;u&gt;Fourth test run&lt;/u&gt;&lt;br /&gt;All assertions pass.&lt;br /&gt;&lt;br /&gt;You've had to make three separate iterations of finding and fixing problems before your test passes.  Since your brain is required for each debug/fix effort, this process cannot be automated.  Instead it becomes time-consuming, frustrating, and inefficient.  Worse, the likelihood of introducing a new bug increases every time the code is modified.&lt;br /&gt;&lt;br /&gt;A better approach would be to split this one test method into three tests, each with a single assertion:&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.testoften.com/uploaded_images/img3-798321.png"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;" src="http://www.testoften.com/uploaded_images/img3-795989.png" border="0" alt="" /&gt;&lt;/a&gt;&lt;br /&gt;Now you can run your test suite once and identify all the problems before you dive into the code to fix them.  True, your test suite has increased in size, from one test to three, but you've always had three tests all along anyway, and this change merely reorganizes your test suite to reflect that.  More importantly, striving for one assertion per unit test streamlines the test-debug-retest cycle and improves the overall testing effort.</content><link rel='alternate' type='text/html' href='http://www.testoften.com/2007/02/junit-best-practices-part-4-dont-be-too.html' title='JUnit Best Practices, Part 4 -- Don&apos;t Be Too Assertive'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=30447482&amp;postID=4532514991839720876' title='0 Comments'/><link rel='replies' type='application/atom+xml' href='http://www.testoften.com/atom.xml' title='Post Comments'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/30447482/posts/default/4532514991839720876'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/30447482/posts/default/4532514991839720876'/><author><name>John Miller</name></author></entry><entry><id>tag:blogger.com,1999:blog-30447482.post-4423310996451518037</id><published>2007-01-30T21:02:00.000-05:00</published><updated>2007-01-30T21:05:41.275-05:00</updated><title type='text'>New Article on Path vs Branch vs Code Coverage</title><content type='html'>Every so often we come across people who talk about code coverage. It's been around for a long time, but not many understand the primary types of coverage and the differences between them.&lt;br /&gt;&lt;br /&gt;We posted a new article on the differences and benefits of path, branch and code coverage, available &lt;a href="http://www.codign.com/pathbranchcode.html"&gt;here&lt;/a&gt;. &lt;br /&gt;&lt;br /&gt;You can also look at &lt;a href="http://bullseye.com/coverage.html"&gt;Bullseye &lt;/a&gt;- they have a nice list of coverage techniques as well.</content><link rel='alternate' type='text/html' href='http://www.testoften.com/2007/01/new-article-on-path-vs-branch-vs-code.html' title='New Article on Path vs Branch vs Code Coverage'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=30447482&amp;postID=4423310996451518037' title='0 Comments'/><link rel='replies' type='application/atom+xml' href='http://www.testoften.com/atom.xml' title='Post Comments'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/30447482/posts/default/4423310996451518037'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/30447482/posts/default/4423310996451518037'/><author><name>Codign Software</name></author></entry><entry><id>tag:blogger.com,1999:blog-30447482.post-1503554451888136583</id><published>2007-01-21T12:14:00.000-05:00</published><updated>2007-01-21T12:16:13.020-05:00</updated><title type='text'>JUnit 4 Resources</title><content type='html'>Came across a blog that contains some JUnit 4 references, including those from Codign Software :)&lt;br /&gt;&lt;br /&gt;&lt;a href="http://abtj.blogspot.com/2007/01/some-useful-junit-4-links.html"&gt;Something About J: Some Useful JUnit 4 Links&lt;/a&gt;</content><link rel='alternate' type='text/html' href='http://www.testoften.com/2007/01/junit-4-resources.html' title='JUnit 4 Resources'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=30447482&amp;postID=1503554451888136583' title='0 Comments'/><link rel='replies' type='application/atom+xml' href='http://www.testoften.com/atom.xml' title='Post Comments'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/30447482/posts/default/1503554451888136583'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/30447482/posts/default/1503554451888136583'/><author><name>Codign Software</name></author></entry><entry><id>tag:blogger.com,1999:blog-30447482.post-116880150287259875</id><published>2007-01-14T14:00:00.000-05:00</published><updated>2007-01-15T07:37:51.303-05:00</updated><title type='text'>Eclipse DTP JUnit and Coverage Assessment</title><content type='html'>With any &lt;a href="http://www.codign.com"&gt;startup&lt;/a&gt;, one of the most important factors in mental well-being is the feedback we receive from our users. Good, bad or ugly, we need it – its more important than getting a purchase order. Really, it’s the silence that kills us.&lt;br /&gt;&lt;br /&gt;That said, we are pleased to get some great feedback from the folks working on the &lt;a href="http://www.eclipse.org/datatools"&gt;Eclipse DTP project&lt;/a&gt;. DTP is a top level project so it gets lots of attention. The project is run by &lt;a href="http://dataplat.blogspot.com/"&gt;John Graham&lt;/a&gt;, chair of the Project Management Committee for the Dataools Platform, and I have had the opportunity of meeting John at a couple of &lt;a href="http://www.eclipsecon.org"&gt;Eclipse &lt;/a&gt;events.  I also met a number of other DTP committers and am impressed with not only their knowledge but with their passion.  These guys like unit testing.&lt;br /&gt;&lt;br /&gt;We not only donated CoView to DTP, but also our time to help them out. Being given a free product is great but unless you have time to use it, it becomes shelfware.  Our goal, worked out with DTP, was to help them analyze their existing path coverage and create additional JUnit tests to increase that coverage.  It took one of our engineers 6 hours to complete the process.  He analyzed their current path coverage and added 40 additional JUnit tests using CoView. The overall cost was $288. Overall increase in coverage: 46% increase in path coverage, 22% increase in branch coverage.  A detailed &lt;a href="http://www.codign.com/pdf/dtp.pdf"&gt;case study&lt;/a&gt; is also available.  DTP also gave us some great feedback during the whole process, and we certainly appreciate it.&lt;br /&gt;&lt;br /&gt;Thanks to DTP for letting us gather this information, and great job on releasing your latest version.</content><link rel='alternate' type='text/html' href='http://www.testoften.com/2007/01/eclipse-dtp-junit-and-coverage.html' title='Eclipse DTP JUnit and Coverage Assessment'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=30447482&amp;postID=116880150287259875' title='0 Comments'/><link rel='replies' type='application/atom+xml' href='http://www.testoften.com/atom.xml' title='Post Comments'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/30447482/posts/default/116880150287259875'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/30447482/posts/default/116880150287259875'/><author><name>Codign Software</name></author></entry><entry><id>tag:blogger.com,1999:blog-30447482.post-116309011261359419</id><published>2006-11-09T11:28:00.000-05:00</published><updated>2006-11-09T14:30:50.566-05:00</updated><title type='text'>JUnit Best Practices, Part 3 -- Constant Expectations</title><content type='html'>You've just written your new &lt;code&gt;&lt;strong&gt;doComplicatedCalculation()&lt;/strong&gt;&lt;/code&gt; method and you're ready to test it, so you create the following unit test:&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.testoften.com/uploaded_images/blog1-722674.png"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;" src="http://www.testoften.com/uploaded_images/blog1-719946.png" border="0" alt="" /&gt;&lt;/a&gt;&lt;br /&gt;It seems straightforward enough.  You invoke the method under test and compare it to an expected result to see if it matches.  The problem is that the use of the &lt;code&gt;&lt;strong&gt;computeExpectedTestResult()&lt;/strong&gt;&lt;/code&gt; method is double trouble.&lt;br /&gt;&lt;br /&gt;First, it appears that &lt;code&gt;&lt;strong&gt;computeExpectedTestResult()&lt;/strong&gt;&lt;/code&gt; is intended to perform the same computation as the method under test.  But if we haven't tested &lt;code&gt;&lt;strong&gt;doComplicatedCalculation()&lt;/strong&gt;&lt;/code&gt; then how can we rely on a copy of that method to verify that it works?  You can't use a logical proposition to prove itself, and you can't use a method to test itself.&lt;br /&gt;&lt;br /&gt;Second, even if you were absolutely sure that &lt;code&gt;&lt;strong&gt;doComplicatedCalculation()&lt;/strong&gt;&lt;/code&gt; is correct, any change made to this method also has to be made to &lt;code&gt;&lt;strong&gt;computeExpectedTestResult()&lt;/strong&gt;&lt;/code&gt;.  You've effectively doubled your coding effort, or, at the very least, set yourself up for a fumble-fingers cut-and-paste problem.&lt;br /&gt;&lt;br /&gt;A better solution would be to replace the &lt;code&gt;&lt;strong&gt;computeExpectedTestResult()&lt;/strong&gt;&lt;/code&gt; invocation with a constant value that you've computed yourself:&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.testoften.com/uploaded_images/blog2-718659.png"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;" src="http://www.testoften.com/uploaded_images/blog2-715834.png" border="0" alt="" /&gt;&lt;/a&gt;&lt;br /&gt;Now, if the assertion fails, you know where to look for the problem.  If the method under test changes, it's also clear where to change the expected value to match it.  &lt;br /&gt;&lt;br /&gt;Using constant values in your unit test assertions reduces the guesswork in your testing effort, and increases future comprehension and maintenance efforts.</content><link rel='alternate' type='text/html' href='http://www.testoften.com/2006/11/junit-best-practices-part-3-constant.html' title='JUnit Best Practices, Part 3 -- Constant Expectations'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=30447482&amp;postID=116309011261359419' title='0 Comments'/><link rel='replies' type='application/atom+xml' href='http://www.testoften.com/atom.xml' title='Post Comments'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/30447482/posts/default/116309011261359419'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/30447482/posts/default/116309011261359419'/><author><name>Codign Software</name></author></entry><entry><id>tag:blogger.com,1999:blog-30447482.post-116300064306698367</id><published>2006-11-08T10:36:00.000-05:00</published><updated>2006-11-09T14:38:10.896-05:00</updated><title type='text'>Market Driven Unit Testing</title><content type='html'>I've been in the software industry for over 15 years, focusing on unit testing for the past two, and I can honestly say that the &lt;span style="font-weight:bold;"&gt;unit testing market&lt;/span&gt; is a tough market to crack.&lt;br /&gt;&lt;br /&gt;There is no doubt that unit testing is important. Most testing vendors (us included) and many developers see the value of a unit testing program, but my experience indicates that formal unit testing solutions are frequently not  adopted by upper management. This leads me to believe unit testing only makes sense depending on the market you are in. Most startups (not us, of course!), for example, really don't care about unit testing. Startups are looking for &lt;a href="http://en.wikipedia.org/wiki/Crossing_the_Chasm"&gt;early adopters&lt;/a&gt;, and early adopters are interested in you and your technology, not your &lt;a href="http://en.wikipedia.org/wiki/Cyclomatic_complexity"&gt;cyclomatic complexity&lt;/a&gt;  or &lt;a href="http://en.wikipedia.org/wiki/Code_coverage"&gt;code coverage&lt;/a&gt; metrics. VC's who fund startups actually want the risk -- the greater the risk the greater the reward.&lt;br /&gt;&lt;br /&gt;Andy Glover, prolific &lt;a href="http://www.testearly.com"&gt;blogger&lt;/a&gt;, published author and president of &lt;a href="http://www.stelligent.com"&gt;Stelligent&lt;/a&gt;, recently wrote a &lt;a href="http://www.testearly.com/2006/11/07/quality-isnt-free/"&gt;blog&lt;/a&gt; about the cost of software quality. In that blog, he states that writing tests for software increases the overall development cost, but that the investment is well worth it given the greater ROI. &lt;br /&gt;&lt;br /&gt;I completely agree with Andy. Many studies, like the &lt;a href="http://www.nist.gov/public_affairs/releases/n02-10.htm"&gt;one&lt;/a&gt; NIST reported in 2004 show the liability and cost of software defects in the market far outweigh the initial cost of setting up a quality development effort. Bill McComas, attorney for &lt;a href="http://www.shapirosher.com"&gt;Shapiro, Sher, Guinot and Sandler&lt;/a&gt; gave a presentation to this effect, which is available &lt;a href="http://codign.com/pdf/Legal%20Ramifications%20of%20Faulty%20Software.pdf"&gt;here&lt;/a&gt;. &lt;br /&gt;&lt;br /&gt;But I want to take this discussion a step further. I recently had a conversation with the CTO of another startup company, who will remain unmentioned because of a potential distracting flame war -- let's call him Mike. Mike is someone I respect for a number of reasons. First,  Mike  helped create a company that was sold for almost $200 million  in 2001. Second, he started another software company and raised VC funding, which is very difficult to do. &lt;br /&gt;&lt;br /&gt;Our conversation focused on software quality, specifically the practice of unit testing. Mike agrees with me, on an intellectual level, about all the benefits of unit testing. Practically, however, he will never implement a unit testing program in any of his companies because of the upfront cost and potential delay in getting his solution out the door. Mike made a number of excellent points:&lt;br /&gt;&lt;br /&gt;  &lt;li&gt;Consumer demand for software provides leeway for defects.&lt;br /&gt;  &lt;/li&gt;&lt;br /&gt;  &lt;li&gt;VC&amp;rsquo;s invest money to take risk, meaning they don&amp;rsquo;t care if software has some bugs, so long as they're managed well.&lt;br /&gt;  &lt;/li&gt;&lt;br /&gt;  &lt;li&gt;Companies that acquire other companies do not look at the code or outstanding defects, but instead focus on the market penetration, number of paying customers, etc.&lt;br /&gt;  &lt;/li&gt;&lt;br /&gt;  &lt;li&gt;Testing, whether it is unit testing, functional testing, or any other type of testing, will always get cut in the effort to maintain competitive advantage.&lt;br /&gt;  &lt;/li&gt;&lt;br /&gt;&lt;br /&gt;My conclusion?  The upfront cost of implementing a unit testing solution may be worth it in the long run, but only in a market where that makes sense (including life- or mission-critical markets).  Companies trying to brand themselves and break into the majority markets are likely candidates for a unit testing solution as well.  After all, perception is reality.</content><link rel='alternate' type='text/html' href='http://www.testoften.com/2006/11/market-driven-unit-testing.html' title='Market Driven Unit Testing'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=30447482&amp;postID=116300064306698367' title='0 Comments'/><link rel='replies' type='application/atom+xml' href='http://www.testoften.com/atom.xml' title='Post Comments'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/30447482/posts/default/116300064306698367'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/30447482/posts/default/116300064306698367'/><author><name>Codign Software</name></author></entry><entry><id>tag:blogger.com,1999:blog-30447482.post-115930908650633094</id><published>2006-09-26T17:40:00.000-04:00</published><updated>2006-11-09T14:31:53.196-05:00</updated><title type='text'>JUnit Best Practices, Part 2 -- Keep 'em Separated</title><content type='html'>A common practice for those new to JUnit is to try and cover more ground with fewer tests.  As long as you're writing your &lt;code&gt;&lt;strong&gt;testAdd()&lt;/strong&gt;&lt;/code&gt; unit test, you may as well invoke &lt;code&gt;&lt;strong&gt;add()&lt;/strong&gt;&lt;/code&gt; as many different ways as possible and see what breaks, right?  &lt;br /&gt;&lt;br /&gt;Wrong.  Not only should a unit test focus on one specific unit, it should focus on one specific &lt;i&gt;behavior&lt;/i&gt; of that unit.  Testing multiple behaviors at once only muddies the water.  Consider our hypothetical &lt;code&gt;&lt;strong&gt;testAdd()&lt;/strong&gt;&lt;/code&gt; method:&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.testoften.com/uploaded_images/bestprac2-749125.png"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;" src="http://www.testoften.com/uploaded_images/bestprac2-745099.png" border="0" alt="" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Now suppose the assertion fails.  Which of the two &lt;code&gt;&lt;strong&gt;add()&lt;/strong&gt;&lt;/code&gt; invocations returned the wrong value?  You won't know until you start digging.  The purpose of unit testing is to take the guesswork out of error location, and this technique defeats that purpose.&lt;br /&gt;&lt;br /&gt;One exception to the rule here is that sometimes you may need to invoke your method under test multiple times in order put things in a particular state before performing the actual test of your method.  In such cases, consider using a &lt;a href="http://mockobjects.com"&gt;mock object&lt;/a&gt; to prepare the test state in a controlled fashion.  The net result is the same -- the only variable in your test is the one behavior that you're testing.&lt;br /&gt;&lt;br /&gt;Testing many different behaviors in a single test method is a common practice because it's so easy to do, but this is a clear case of being penny-wise and pound-foolish.  Take the time to separate each behavior into its own unit test.</content><link rel='alternate' type='text/html' href='http://www.testoften.com/2006/09/junit-best-practices-part-2-keep-em.html' title='JUnit Best Practices, Part 2 -- Keep &apos;em Separated'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=30447482&amp;postID=115930908650633094' title='0 Comments'/><link rel='replies' type='application/atom+xml' href='http://www.testoften.com/atom.xml' title='Post Comments'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/30447482/posts/default/115930908650633094'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/30447482/posts/default/115930908650633094'/><author><name>Codign Software</name></author></entry><entry><id>tag:blogger.com,1999:blog-30447482.post-115929710767588952</id><published>2006-09-26T14:46:00.000-04:00</published><updated>2006-11-09T14:33:18.580-05:00</updated><title type='text'>JUnit Best Practices, Part 1 -- Don't Decide Anything</title><content type='html'>Creating JUnit tests can be something of a craft when you first start writing them.  In our JUnit Best Practices articles, we hope to shed some light on what makes a good JUnit test, and why.&lt;br /&gt;&lt;br /&gt;The purpose of a JUnit test is to set up a controlled environment in which the unit under test can be run.  To that end, each JUnit test should be as logically simple as possible, preferably with no decisions at all.&lt;br /&gt;&lt;br /&gt;Consider this JUnit test for a sample method called &lt;code&gt;&lt;strong&gt;countDuplicates()&lt;/strong&gt;&lt;/code&gt;:&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.testoften.com/uploaded_images/bp-775409.png"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;" src="http://www.testoften.com/uploaded_images/bp-771838.png" border="0" alt="" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;If the &lt;code&gt;&lt;strong&gt;getTestInput()&lt;/strong&gt;&lt;/code&gt; returns null, then &lt;code&gt;&lt;strong&gt;countDuplicates()&lt;/strong&gt;&lt;/code&gt; is never invoked.  If you're looking at your testing status summary, this test will be marked as a success despite the fact that it didn't test anything!  Of course, every rule has an exception, so if you absolutely must have decision logic in your test case, be extra diligent to make sure that your test fails if it doesn't execute as expected.&lt;br /&gt;&lt;br /&gt;Adding decisions to your test methods only adds another variable to the test, which is exactly what you should avoid when unit testing.  Keeping your unit tests decision-free will increase their effectiveness, clarity, and maintainability.</content><link rel='alternate' type='text/html' href='http://www.testoften.com/2006/09/junit-best-practices-part-1-dont.html' title='JUnit Best Practices, Part 1 -- Don&apos;t Decide Anything'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=30447482&amp;postID=115929710767588952' title='0 Comments'/><link rel='replies' type='application/atom+xml' href='http://www.testoften.com/atom.xml' title='Post Comments'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/30447482/posts/default/115929710767588952'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/30447482/posts/default/115929710767588952'/><author><name>Codign Software</name></author></entry><entry><id>tag:blogger.com,1999:blog-30447482.post-115927931729223431</id><published>2006-09-26T09:54:00.000-04:00</published><updated>2006-11-09T14:35:26.323-05:00</updated><title type='text'>Unit Testing + Code Coverage = Bad For Business?</title><content type='html'>Measuring code coverage is a natural complement to unit testing.  It provides a clear picture of what parts of your code remain untested, and helps you focus additional tests on the uncovered code.  Even with just a few unit tests you can quickly cover the basic functionality of several methods in your class under test.  Unfortunately, &lt;b&gt;a common mistake is to measure coverage without actually verifying that the executed code is correct&lt;/b&gt;, and this is exactly what happens with many code coverage tools.&lt;br /&gt;&lt;br /&gt;Consider this example:&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.testoften.com/uploaded_images/code-1-777841.png"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;" src="http://www.testoften.com/uploaded_images/code-1-772324.png" border="0" alt="" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;This is a simple class that converts an array of integers into the corresponding array of Strings.  Note that there is an intentional bug in &lt;code&gt;&lt;strong&gt;getIntegerName()&lt;/strong&gt;&lt;/code&gt; -- case 1 of the &lt;code&gt;&lt;strong&gt;switch&lt;/strong&gt;&lt;/code&gt; is missing a &lt;code&gt;&lt;strong&gt;break&lt;/strong&gt;&lt;/code&gt; statement and causes the method to return the wrong value.&lt;br /&gt;&lt;br /&gt;We've created a couple of JUnit tests to start with.  &lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.testoften.com/uploaded_images/testcases-1-778381.png"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;" src="http://www.testoften.com/uploaded_images/testcases-1-757294.png" border="0" alt="" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;We'll use &lt;a href="http://coverlipse.sourceforge.net"&gt;Coverlipse&lt;/a&gt;, a popular open-source coverage tool for Eclipse, to help us measure statement coverage for these unit test. Here are the Coverlipse results.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.testoften.com/uploaded_images/coverlipse results-1-757004.png"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;" src="http://www.testoften.com/uploaded_images/coverlipse results-1-740541.png" border="0" alt="" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Both JUnit tests ran successfully and Coverlipse reports 100% statement coverage for the &lt;code&gt;&lt;strong&gt;Sample&lt;/strong&gt;&lt;/code&gt; class.  Looking at the detailed coverage markers in the Eclipse editor, Coverlipse reports that all statements have been covered.  It looks like the perfect testing summary to show to the boss, but a deeper analysis reveals some serious problems with these tests.&lt;br /&gt;&lt;br /&gt;Most serious, obviously, is that we did not find the bug in &lt;code&gt;&lt;strong&gt;getIntegerName()&lt;/strong&gt;&lt;/code&gt;.  In this case, our test assertions missed checking for the appropriate "one" value, but it could just as easily have been the case that the method under test masked the incorrect value returned by &lt;code&gt;&lt;strong&gt;getIntegerName()&lt;/strong&gt;&lt;/code&gt;.  The real reason that the bug was not found is that no assertions were made about the subordinate method.   &lt;br /&gt;&lt;br /&gt;The &lt;code&gt;&lt;strong&gt;createIntegerNameList()&lt;/strong&gt;&lt;/code&gt; method is invoked by our tests.  That's the unit under test, and the only method for which we should be making assertions.  The &lt;code&gt;&lt;strong&gt;getIntegerName()&lt;/strong&gt;&lt;/code&gt; method was obviously executed, but because we didn't verify its behavior, we missed an obvious problem.  Until we've verified the subordinate method directly, we should consider it untested.&lt;br /&gt;&lt;br /&gt;Even when this coverage technique is successful in uncovering bugs, it's an inefficient way to conduct testing.  Imagine trying to thread a needle from ten feet away.  Eventually you'll be able to do it, but how many attempts will fail first?  Similarly, testing a subordinate method via its invoker is coarse work at best and impossible at worst.   In our example it's easy to see how we can drive down into the subordinate method, but as a method's logic increases, this task becomes increasingly difficult.  If you're examining the logic of the invoking method to determine how to increase the coverage of the subordinate method, why not just examine (and test) the logic of the subordinate method directly?  Don't waste time with test that might give you a minimal boost in coverage.  Test the underlying method directly.&lt;br /&gt;&lt;br /&gt;Now let's look at the same test suite using a technique that reports &lt;a href="http://codign.com/pathvbranch.html"&gt;path coverage&lt;/a&gt; for unit tests in isolation.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.testoften.com/uploaded_images/coview resullts-739620.png"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;" src="http://www.testoften.com/uploaded_images/coview resullts-733328.png" border="0" alt="" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Notice that no coverage has been reported for &lt;code&gt;&lt;strong&gt;getIntegerName()&lt;/strong&gt;&lt;/code&gt;, which is what we expect since we haven't created any unit tests for it.  Until we examine and test each of the paths identified in this method, our testing effort remains incomplete.&lt;br /&gt;&lt;br /&gt;Unit tests are so named for a reason -- concentrate on coverage only for the unit under test and ignore all others.</content><link rel='alternate' type='text/html' href='http://www.testoften.com/2006/09/unit-testing-code-coverage-bad-for.html' title='Unit Testing + Code Coverage = Bad For Business?'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=30447482&amp;postID=115927931729223431' title='4 Comments'/><link rel='replies' type='application/atom+xml' href='http://www.testoften.com/atom.xml' title='Post Comments'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/30447482/posts/default/115927931729223431'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/30447482/posts/default/115927931729223431'/><author><name>Codign Software</name></author></entry></feed>