Enterprise Java

Run your Unit Tests in Parallel

It was about time when the developer of Unit Tests had the ability to run the tests in Parallel using annotations. In today’s blog post, we will look at how you can make your traditional Junit Tests to run in parallel using annotations provided by Easytest. EasyTest  is a Testing Framework build on top of JUnit to provide you ease of writing and maintaining your tests. It is focused on writing Data Driven Tests for your application.

Lets start by assuming a Class ItemService  that has a method 2 methods:
 
 
 

  1. getItems that takes 2 arguments Double itemId and String itemType. The API class is described below.
  2. updateItem that takes an Item instance and updates it in the Database.
public class ItemService {

   public List getItems (Double itemId , String itemType);

   public void updateItem (Item item);

}

We will leave out the implementation for the sake of keeping the example simple.

Lets see the step by step approach of writing Parallel Unit Tests.

STEP 1:

Download the latest version(or version 1.2 and above) of EasyTest from Maven Central Repository.

STEP 2:

Next, lets write a simple Unit Test for the above business methods using EasyTest annotations and data driven testing approach.

@RunWith(DataDrivenTestRunner.class)
@DataLoader(filePaths="getItemData.csv")
@TestConfigProvider(TestConfigProvider.class)
public class ItemServiceTest {

     @Inject
     private ItemService itemService;

     @Before
     public void before() {
         System.out.println("BEFORE");
     }

     @Test
     public List testGetItems(@Param(name="itemId")Double itemId , @Param(name="itemType")String itemType) {
       
          //Actual test conditions here
           System.out.println("Run testGetItems");

     }

     @Test
     public void testUpdateItem(@Param(name="item") Item item) {
       
          //Actual test conditions here
          System.out.println("Run testUpdateItem");

     }

    @After
     public void after() {
         System.out.println("AFTER");
     }
}

The above example uses existing functionalities of EasyTest, like providing data in a test file, using CDI annotations to inject test beans. If you want to know more about the @TestConfigProvider and Dependency Injection in EasyTest you can see my previous blog post. If you want to know more about how to write Data Driven Tests in general using EasyTest, you can visit EasyTest’s WIKI Pages.

Now the above is an example of running a simple Data Driven Unit Test. All the above tests will run in Serial, one after the other. Assuming that each of the test method has two sets of test data specified in the getItemData.csv file, when the above test is run, we get the following on the console :

BEFORE
Run testGetItems
AFTER

BEFORE
Run testGetItems
AFTER

BEFORE
Run testUpdateItem
AFTER

BEFORE
Run testUpdateItem
AFTER

STEP 3:

Next, lets make the above tests run in Parallel. Simply include the @Parallel annotation at the class level and provide the number of threads you want to run. For the above test case we will run two threads.

@RunWith(DataDrivenTestRunner.class)
@DataLoader(filePaths="getItemData.csv")
@TestConfigProvider(TestConfigProvider.class)
@Parallel(threads=2)
public class ItemServiceTest {

     @Inject
     private ItemService itemService;

     @Before
     public void before() {
         System.out.println("BEFORE");
     }

     @Test
     public List testGetItems(@Param(name="itemId")Double itemId , @Param(name="itemType")String itemType) {
       
          //Actual test conditions here
           System.out.println("Run testGetItems");

     }

     @Test
     public void testUpdateItem(@Param(name="item") Item item) {
       
          //Actual test conditions here
          System.out.println("Run testUpdateItem");

     }

    @After
     public void after() {
         System.out.println("AFTER");
     }
}

Note the @Parallel annotation at the lass level.

When this test is run, the console output looks something like this(it may vary when you run the same test):

BEFORE
BEFORE
Run testGetItems
BEFORE
Run testUpdateItem
AFTER
Run testGetItems
BEFORE
AFTER
Run testUpdateItem
AFTER
AFTER

As you can see from the above console output, the tests are running alongside each other and thats why you see such a distributed console output.

Conclusion

We saw in the above example, how you can now run your tests in Parallel. Also when building your tests from build tools like Maven, the tests will run in Parallel. This is a big boost to scenarios where you have thousands of Unit Tests and they take minutes to run.

EasyTest is working on a command line/system parameter approach to enabling/disabling of Parallel feature. Stay tuned.
 

Reference: Run your Unit Tests in Parallel from our JCG partner Anuj Kumar at the JavaWorld Blog blog.

Anuj Kumar

Anuj is working as a Senior S/W Engineer in Netherlands. He is a Sun Certified Java Professional with experience in design and development of mid/large scale Java applications. He is the creator of EasyTest Framework(https://github.com/EaseTech) which is a Data Driven Testing Framework for Java.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Alexander
Alexander
8 years ago

Anuj
Can I combine @Parallel and @Repeat annotations to simulate multi-threaded load test ?
Consider the @Parallel test class with single @Repeat(100) test method , will a be able to Assert in @AfterClass assuming that all 100 invocations are completed ?
Thanks

Anuj
8 years ago

Hi Alexander.
Indeed you can. Try it out and let me know if it doesn’t work.

Cheers

Back to top button