Agile Zone is brought to you in partnership with:

I like software - reading, tinkering, designing, coding. I have been doing so for 20 years or so and I would not mind continuing this for foreseeable future. Fortunately for me, this is my profession as well and I have managed to get paid for this for some 14 years now. Although I do not have any strong bias for any business domain, I have been working with some pretty big names in the finance domain and you might get a hint of that from my entries. Partha is a DZone MVB and is not an employee of DZone and has posted 20 posts at DZone. You can read more from them at their website. View Full User Profile

Categorize tests to reduce build time.

05.13.2012
| 5282 views |
  • submit to reddit
Before we progress with the main content of the article, let's get a few definitions out of the way.

Unit tests

Unit tests are tests that are small (tests one use case or unit), run in memory (do not interact with database, message queues etc.), repeatable and fast. For our conversation let us restrict these to JUnit based test cases that developers write to check their individual piece of code.

Integration tests

Integration tests are larger (tests one flow or integration of components), do not necessarily run in memory only (interact with database, file systems, message queues etc.), definitely slower, and not necessarily repeatable (as in the result might be change in case some change was done in data base for example).

Why is this differentiation important?

In Agile programming, one of the basic concepts is to run unit tests every once in a while (multiple times in a day on developer boxes) and enforce the integration tests to run once a day (on continuous integration server rather than on developer boxes). Please note that the developer should be able to run integration tests whenever he wants it, it is just that it is separate from the unit tests so the developer now has a choice to not run integration tests every time he wants to run tests.

How exactly does that flexibility help?

  1. Developers build more frequently. That – in Agile world means – developers run unit tests more frequently (often a few times per day).
  2. Developers get to know of a bug sooner and waste less time coding to a broken codebase. That means saving time and money.
  3. Fixing bugs is easier and faster. Given the frequency of builds, less amount of "offending code" could have been committed and hence it is easier to zero down on the bug and fix it.
  4. Last but not the least, anyone who has done any professional coding will testify that while it helps to be able take a 10 minute break once a day, nothing kills the creativity of a coder more efficiently than having to wait for a 1 hour build. The impact to morale is intangible, but immense.

How exactly do I bring down the build time?

There is no one size fits all (there never is). The exact executable steps to bring down build and release time will be a factor of many variables including the technology stack of the product (Java, DotNet, php), the build and release technologies (Batch files, Ant, Maven) and many other.

For Java, Maven, and JUnit combination ...

Let us start by using Maven to create a simple java application to demonstrate the case.

\MavenCommands.bat
ECHO OFF 

REM =============================
REM Set the env. variables. 
REM =============================
SET PATH=%PATH%;C:\ProgramFiles\apache-maven-3.0.3\bin;
SET JAVA_HOME=C:\ProgramFiles\Java\jdk1.7.0

REM =============================
REM Create a simple java application. 
REM =============================
call mvn archetype:create ^
  -DarchetypeGroupId=org.apache.maven.archetypes ^
  -DgroupId=org.academy ^
  -DartifactId=app001
pause
If you run this batch file you will start with a standard java application readymade for you.

The default java application does not come with the latest JUnit. You might want to change Maven configuration to add latest JUnit.

\pom.xml
[...]
<junit.version>4.10</junit.version>
[...]
<!-- Unit test. -->                    
<dependency>                           
<groupId>junit</groupId>           
<artifactId>junit</artifactId>     
<version>${junit.version}</version>
<scope>test</scope>                
</dependency>    

Now, go ahead and add a JUnit test class.


/app001/src/test/java/org/academy/AppTest.java
public class AppTest {

private final static Logger logger = LoggerFactory.getLogger(AppTest.class);

@Test
public void smallAndFastUnitTest() {
 logger.debug("Quick unit test. It is not expected to interact with DB etc.");
 assertTrue(true);
}

@Test
@Category(IntegrationTest.class)
public void longAndSlowIntegrationTest() {
 logger.debug("Time consuming integration test. It is expected to interact with DB etc.");
 assertTrue(true);
}
}
As you might notice there is a IntegrationTest.class marker. You will have to create this class as well.

/app001/src/test/java/org/academy/annotation/type/IntegrationTest.java
public interface IntegrationTest {
  // Just a marker interface. 
}
Creating the marker interface and annotating your test methods (or classes if you choose to) is all that you have to do in your code.

Now, all that remains to be done is to tell maven to run "integration tests" only at integration test phase. That means a developer could choose to run only the unit tests (the fast ones that are insulated from databases, queues etc) for most of the time. The Continuous Integration server i.e. Hudson (or the likes) will run the unit tests and the integration tests (which will be slower since they are expected to interact with databases etc.) and that can happen overnight.

So, here is how you do it.

/pom.xml
<!-- Running unit tests. -->                                                        
<plugin>                                                                            
<groupId>org.apache.maven.plugins</groupId>                                     
<artifactId>maven-surefire-plugin</artifactId>                                  
<version>2.12</version>                                                         
<dependencies>                                                                  
<dependency>                                                                
<groupId>org.apache.maven.surefire</groupId>                            
<artifactId>surefire-junit47</artifactId>                               
<version>2.12</version>                                                 
</dependency>                                                               
</dependencies>                                                                 
<configuration>                                                                 
<argLine>-XX:-UseSplitVerifier</argLine>                                
<excludedGroups>org.academy.annotation.type.IntegrationTest</excludedGroups>
</configuration>                                                                
</plugin>         

 

This will mean that a developer can run all the unit tests just by using one liner.

mvn clean test
This will not run any test that is annotated as integration test.

For integration test add the following.

/pom.xml
<!-- Running integration tests. -->                                 
<plugin>                                                            
<artifactId>maven-failsafe-plugin</artifactId>                  
<version>2.12</version>                                         
<dependencies>                                                  
<dependency>                                                
<groupId>org.apache.maven.surefire</groupId>            
<artifactId>surefire-junit47</artifactId>               
<version>2.12</version>                                 
</dependency>                                               
</dependencies>                                                 
<configuration>                                                 
<groups>org.academy.annotation.type.IntegrationTest</groups>
</configuration>                                                
<executions>                                                    
<execution>                                                 
<goals>                                                 
<goal>integration-test</goal>                       
</goals>                                                
<configuration>                                         
<includes>                                          
<include>**/*.class</include>                   
</includes>                                         
</configuration>                                        
</execution>                                                
</executions>                                                   
</plugin>  

This means the Hudson or the developer (if he chooses to) can run all the tests, unit and integration by a single command.

mvn clean verify    
Of course if you choose to go all the way i.e. compile, run unit tests, package, run integration tests and deploy, you could do that with a single line of command as well.
mvn clean install

That’s it. You have taken one step towards faster builds and more agile way of working. Happy coding.

Please note: The article has been reproduced from my original blog entry here

Published at DZone with permission of Partha Bhattacharjee, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)