DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
What's in store for DevOps in 2023? Find out today at 11 am ET in our "DZone 2023 Preview: DevOps Edition!"
Last chance to join
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. Categorize tests to reduce build time.

Categorize tests to reduce build time.

Partha Bhattacharjee user avatar by
Partha Bhattacharjee
·
May. 14, 12 · Interview
Like (0)
Save
Tweet
Share
7.49K Views

Join the DZone community and get the full member experience.

Join For Free
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. 

unit test Continuous Integration/Deployment Integration dev Database

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Deploying Java Serverless Functions as AWS Lambda
  • Bye Bye, Regular Dev [Comic]
  • Differences Between Site Reliability Engineer vs. Software Engineer vs. Cloud Engineer vs. DevOps Engineer
  • ChatGPT Prompts for Agile Practitioners

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: