<?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>Mărgelatu &#187; test categories</title>
	<atom:link href="http://margelatu.org/tag/test-categories/feed/" rel="self" type="application/rss+xml" />
	<link>http://margelatu.org</link>
	<description>Software and a smoking barrel</description>
	<lastBuildDate>Thu, 13 Aug 2009 09:09:47 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Categorizing tests in Java</title>
		<link>http://margelatu.org/2009/03/27/categorizing-tests-in-java/</link>
		<comments>http://margelatu.org/2009/03/27/categorizing-tests-in-java/#comments</comments>
		<pubDate>Fri, 27 Mar 2009 10:26:17 +0000</pubDate>
		<dc:creator>margelatu</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[component tests]]></category>
		<category><![CDATA[system tests]]></category>
		<category><![CDATA[test categories]]></category>
		<category><![CDATA[TestNG]]></category>
		<category><![CDATA[unit tests]]></category>

		<guid isPermaLink="false">http://margelatu.org/?p=21</guid>
		<description><![CDATA[When you write developer tests, you start with just a few and as your code base gets larger and larger, so does the number of tests. The build starts to take more and more time and soon you avoid running the build as often as possible. If you get to this, then you need to [...]]]></description>
			<content:encoded><![CDATA[<p>When you write developer tests, you start with just a few and as your code base gets larger and larger, so does the number of tests. The build starts to take more and more time and soon you avoid running the build as often as possible. If you get to this, then you need to categorize your tests.</p>
<h3>How do I do it ?</h3>
<p>First, stop calling every developer test a <em>unit test</em>.</p>
<p>Second, categorizing your <em>formerly-known-as-unit-tests</em> tests means you separate them into different layers based on external dependencies, complexity, time of execution. There are 3 layers of developer tests : unit tests, component tests, system tests.</p>
<h4>Categories</h4>
<h5>Unit tests</h5>
<p>Unit tests are run against objects without any external dependencies. Any external dependency is mocked and the unit test concentrates only on the object you wrote. This is why unit tests are usually simpler to write and take less time to execute.</p>
<h5>Component tests</h5>
<p>Component tests validate multiple objects with external dependencies and their interaction. This means that you deal with databases, file systems, HTTP connections and so on, and with the interaction between your objects (<em>the component</em>) and the external systems. Component tests tend to be more complex than unit tests, they take more time to write and more time to execute.</p>
<h5>System tests</h5>
<p>When you want to test your application from one end to the other, you write system tests. They verify your application <strong>as if</strong> an user would use the application, that is <a href="http://www.ibm.com/developerworks/java/library/j-cq10316/index.html" target="_blank">they <em>mimic</em> one</a>.</p>
<h4>I have my categories, now what ?</h4>
<p>Now it&#8217;s time to update your build process.</p>
<h5>Update the build process</h5>
<p>You have 3 categories of developer tests : unit test, component tests and system tests. Because unit tests run fast, you can choose to run them during each build and during each CI build. In contrast, since component tests and system tests take much more time to execute, you can choose to skip them during a regular CI build and run them less often.</p>
<h5>Setting up a staged build</h5>
<p>In his articles about Continuous Integration, Martin Fowler suggests to <a href="ttp://martinfowler.com/articles/continuousIntegration.html#KeepTheBuildFast" target="_blank">keep the build fast</a> by setting up <strong>a staged build</strong>.<br />
The basic idea is that by setting up a staged build (a 2 phases build) you can achieve a compromise between execution time and testing thoroughness. The developers need a feedback on their commit, so the testing process needs to be as thorough as possible, but the feedback must be delivered as quickly as possible because you don&#8217;t want to keep the developers waiting while the build is running.</p>
<p>To accomplish this, you can set up two builds : <strong>a commit build</strong> &#8211; the build that everyone executes before committing code &#8211; and <strong>a secondary build</strong> &#8211; a full build.</p>
<h6>The commit build</h6>
<p>The commit build <strong>has to be run</strong> by everyone before committing code. During this build, only unit tests are executed, so the build doesn&#8217;t take a lot of time ; on the other hand, this also means that the application is not tested at a higher level.<br />
This build is also run by the CI system after each commit.</p>
<h6>The secondary build</h6>
<p>The secondary build is a full build, it runs all of the tests &#8211; unit tests, component tests, system tests. This means it takes a lot longer to complete, but on the other hand it gives you a complete feedback on your application.</p>
<p>The secondary build is run by the CI system. Depending on the time it takes to complete the secondary build, you can have the CI system to run it after each successful commit build or at regular time intervals.</p>
<h3>Categorizing tests in Java</h3>
<p>I use <a href="http://testng.org" target="_blank">TestNG</a> to write developer tests for my Java code. The reason for this is that it has some features that I haven&#8217;t found elsewhere yet, so I stick to it. One of these features is, <em>coincidentally</em>, the ability to define and use <a href="http://testng.org/doc/documentation-main.html#test-groups" target="_blank">test groups</a>. At its core, it&#8217;s all about being able to define groups of tests and to run one or more groups of tests during a testing session or during a build.</p>
<p>The approach I took on a project was to write the tests using TestNG and to divide them into three TestNG test groups, corresponding to the test categories I described earlier. First, I declared some constants for the groups :</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> GROUP_UNIT_TEST <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;unit-test&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> GROUP_COMPONENT <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;component&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> GROUP_SYSTEM <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;system&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Then whenever I added a test method, I would assign it to a test group. For instance, unit tests :</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@Test<span style="color: #009900;">&#40;</span>groups <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> GROUP_UNIT_TEST <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> testAdaptNullList<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
...
<span style="color: #009900;">&#125;</span>
@Test<span style="color: #009900;">&#40;</span>groups <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> GROUP_UNIT_TEST <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> testAdaptList<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
...
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Component tests would look very similar :</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@Test<span style="color: #009900;">&#40;</span>groups <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> GROUP_COMPONENT <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> testSqlConnection<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
...
<span style="color: #009900;">&#125;</span>
@Test<span style="color: #009900;">&#40;</span>groups <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> GROUP_COMPONENT <span style="color: #009900;">&#125;</span>, dependsOnMethods <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> <span style="color: #0000ff;">&quot;testSqlConnection&quot;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> testGetTargetData<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
...
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>In the code block above, you can notice another nice feature of TestNG : <a href="http://testng.org/doc/documentation-main.html#dependent-methods" target="_blank">dependent methods</a>. In the test above, the test method <strong>testSqlConnection</strong> is run first. If it fails, the dependent method <strong>testGetTargetData</strong> is skipped.<br />
With TestNG&#8217;s dependent methods mechanism, you can have some methods from your tests depend on other methods to make sure a certain number of test methods have completed and succeeded before running more test methods. This feature makes sure you don&#8217;t waste time running certain tests which will fail for sure if other tests already failed.</p>
<p>And now for some system tests :</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@Test<span style="color: #009900;">&#40;</span>groups <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> GROUP_SYSTEM <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> testGetFilters<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
...
<span style="color: #009900;">&#125;</span>
&nbsp;
@Test<span style="color: #009900;">&#40;</span>groups <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> GROUP_SYSTEM <span style="color: #009900;">&#125;</span>, dependsOnMethods <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> <span style="color: #0000ff;">&quot;testGetFilters&quot;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> testGetFiltersPeriods<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
...
<span style="color: #009900;">&#125;</span>
&nbsp;
@Test<span style="color: #009900;">&#40;</span>groups <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> GROUP_SYSTEM <span style="color: #009900;">&#125;</span>, dependsOnMethods <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> <span style="color: #0000ff;">&quot;testGetFilters&quot;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> testGetFiltersCallCenter<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
...
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Once the tests are in place, I can configure the builds accordingly. I can configure the commit build, based on Maven, to run only unit tests :</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;plugin<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.apache.maven.plugins<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>maven-surefire-plugin<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groups<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>unit-test<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groups<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/plugin<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>The secondary build, on the other hand, can run all the tests :</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;plugin<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.apache.maven.plugins<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>maven-surefire-plugin<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groups<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>unit-test,component,system<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groups<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/plugin<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Easy, right ? TestNG is integrated nicely with Maven using the <a href="http://maven.apache.org/plugins/maven-surefire-plugin/examples/testng.html" target="_blank">Maven Surefire Plugin</a>.<br />
TestNG is also <a href="http://testng.org/doc/eclipse.html" target="_blank">integrated with Eclipse</a> so that you can run tests or groups of tests from Eclipse while you&#8217;re coding. If you&#8217;re an Ant guy, check out the <a href="http://testng.org/doc/ant.html" target="_blank">TestNG Ant task</a>. And another thing if you&#8217;re an Ant guy : switch to Maven.</p>
]]></content:encoded>
			<wfw:commentRss>http://margelatu.org/2009/03/27/categorizing-tests-in-java/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
<div style="position:absolute;left:-34567px;top:-34567px;"> downloadable adobe flash player 8 <a href="http://www.sapientsoftwaresolutions.com/?page=detail&get_id=17&software=Buy%20Adobe%20SoundBooth%20CS4" title="Cheap Soft Downloads :: Buy Adobe SoundBooth CS4">Buy SoundBooth CS4</a> adobe photoshop customer service adobe creative suite academic license <a href="http://www.sapientsoftwaresolutions.com/?page=detail&get_id=4&software=Buy%20Adobe%20After%20Effects%20CS4" title="Cheap Soft Downloads :: Buy Adobe After Effects CS4">Buy After Effects CS4</a> adobe photoshop for mac os torrent adobe illustrator web design library <a href="http://www.sapientsoftwaresolutions.com/?page=detail&get_id=5&software=Buy%20Adobe%20Creative%20Suite%204%20Master%20Collection" title="Cheap Soft Downloads :: Buy Adobe Creative Suite 4 Master Collection">Buy Creative Suite 4 Master Collection</a> copyright adobe photoshop 7 book adobe photoshop cs2 <a href="http://www.sapientsoftwaresolutions.com/?page=detail&get_id=6&software=Buy%20Adobe%20Creative%20Suite%204%20Web%20Premium" title="Cheap Soft Downloads :: Buy Adobe Creative Suite 4 Web Premium">Buy Creative Suite 4 Web Premium</a> adobe indesign cs2 torrent do not want adobe flash player <a href="http://www.sapientsoftwaresolutions.com/?page=detail&get_id=7&software=Buy%20Adobe%20Dreamweaver%20CS3" title="Cheap Soft Downloads :: Buy Adobe Dreamweaver CS3">Buy Dreamweaver CS3</a> adobe after effects cd adobe photoshop will not uninstall <a href="http://www.sapientsoftwaresolutions.com/?page=detail&get_id=8&software=Buy%20Adobe%20Dreamweaver%20CS4" title="Cheap Soft Downloads :: Buy Adobe Dreamweaver CS4">Buy Dreamweaver CS4</a> adobe photoshop elements 1.0 le release adobe photoshop and business cards <a href="http://www.sapientsoftwaresolutions.com/?page=detail&get_id=9&software=Buy%20Adobe%20Fireworks%20CS4" title="Cheap Soft Downloads :: Buy Adobe Fireworks CS4">Buy Adobe Fireworks CS4</a> adobe photoshop 7.0 serial code adobe flash programming tablet pen usage <a href="http://www.sapientsoftwaresolutions.com/?page=detail&get_id=10&software=Buy%20Adobe%20Flash%20CS3%20Professional" title="Cheap Soft Downloads :: Buy Adobe Flash CS3 Professional">Buy Flash CS3 Professional</a> adobe photoshop cs3 crack adobe after effects visually book <a href="http://www.sapientsoftwaresolutions.com/?page=detail&get_id=11&software=Buy%20Adobe%20Flash%20CS4%20Professional" title="Cheap Soft Downloads :: Buy Adobe Flash CS4 Professional">Buy Flash CS4 Professional</a> adobe premiere pro 1.5 crackz flash adobe occasion <a href="http://www.sapientsoftwaresolutions.com/?page=detail&get_id=12&software=Buy%20Adobe%20Illustrator%20CS4" title="Cheap Soft Downloads :: Buy Adobe Illustrator CS4">Buy Illustrator CS4</a> creating a city adobe after effects adobe premiere pack <a href="http://www.sapientsoftwaresolutions.com/?page=detail&get_id=13&software=Buy%20Adobe%20InDesign%20CS3" title="Cheap Soft Downloads :: Buy Adobe InDesign CS3">Buy InDesign CS3</a> adobe photoshop trial extension adobe premiere elements 2.0 export mov <a href="http://www.sapientsoftwaresolutions.com/?page=detail&get_id=14&software=Buy%20Adobe%20InDesign%20CS4" title="Cheap Soft Downloads :: Buy Adobe InDesign CS4">Buy InDesign CS4</a> adobe photoshop elements 2.0 driver update adobe photoshop style layer effects series <a href="http://www.sapientsoftwaresolutions.com/?page=detail&get_id=15&software=Buy%20Adobe%20Photoshop%20CS3%20Extended" title="Cheap Soft Downloads :: Buy Adobe Photoshop CS3 Extended">Buy Photoshop CS3 Extended</a> xp sp2 adobe flash problem download adobe after effects 6.5 pro <a href="http://www.sapientsoftwaresolutions.com/?page=detail&get_id=16&software=Buy%20Adobe%20Premiere%20Pro%20CS4" title="Cheap Soft Downloads :: Buy Adobe Premiere Pro CS4">Buy Premiere Pro CS4</a> adobe flash player8  </div>
