muktoshuvro
muktoshuvro

Reputation: 438

Running multiple test in parallel with Multithreading

Can any body put some light that how should I implement multiple test in parallel with Multithreading so that I can achieve test result quickly. At the moment my code execute test one by one which kills a lot of time while testing.

  public class Test{

  @Test
  public void test1(){ }  

  @Test
  public void test2(){} 
  }

So far I have followed this link JUnit 4 TestRule to run a test in its own thread where my test can be executed in thread but one by one which doesn't serve my purpose. Any suggestion ??

Upvotes: 1

Views: 1889

Answers (3)

You might be able to meet your requirement with JUnit as other answers indicated. In case if you have flexibility to choose another testing framework, you can try with TestNG which has bigger feature set than JUnit. This might also help in a long run and worth giving a try. For your specific requirement you can try the following link to run tests in parallel with TestNG http://howtodoinjava.com/2014/12/02/testng-executing-parallel-tests/

Upvotes: 0

Liviu Stirb
Liviu Stirb

Reputation: 6075

There is no standard way of doing this only with JUnit. You need to add this multi-thread runner to your project: https://gist.github.com/djangofan/4947053

After that you can run all of its tests in parallel by adding an annotation to your class:

@RunWith (MultiThreadedRunner.class)

Upvotes: 0

Gábor Lipták
Gábor Lipták

Reputation: 9776

If you use maven, you can configure your maven build to run each method in separate threads. See more info at https://maven.apache.org/surefire/maven-surefire-plugin/examples/fork-options-and-parallel-execution.html

Something like this should work:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
      <parallel>classesAndMethods</parallel>
      <threadCount>20</threadCount>
    </configuration>
</plugin>

Upvotes: 2

Related Questions