jugLviv

Meta


Share on:


Run your Unit Tests in Parallel

juglvivjuglviv

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.

Оригінал: http://www.kumaranuj.com/2013/07/run-your-unit-tests-in-parallel.html

juglviv
Author