JUnit: Run simultaneous tests
Asked Answered
A

2

7

I am still fairly new to Java programming and to JUnit testing. I use NetBeans 6.9.1 which comes with junit-4.5 (but I have added junit-4.8.2 to my library).

I have a number of test classes and in each class there are a number of @Test methods.

When I run a particular Test class it runs through each @Test method one at a time. I have also created a Test Suite with

@RunWith(Suite.class)
@Suite.SuiteClasses(value = {
    TestClassA.class,
    TestClassB.class,
    TestClassC.class})
public class NewTestSuite {
}

which will run through each of my Test Classes and within each run each @Test method.

My question is: is it possible for me to run the Test Classes simultaneously? Or, within each Test Class is it possible to run the @Test methods simultaneously?

Doing so would allow me to run through all of the tests much faster than having the classes and methods run one-at-a-time.

Thanks!

Alsup answered 18/2, 2011 at 13:41 Comment(5)
How long do your unit tests run?Hepner
Anywhere from less than a second to 30-60 seconds. The application communicates with a data server so I would like to run several requests simultaneously to speed up the tests.Alsup
I think 30-60 seconds is a reasonable amount time, if you don't need to run them every 30-60 seconds:)Decrial
The information about the data server sounds like an integration test and like an unit test?Hepner
@Hepner That may very well be the case. Should I not be using jUnit to test how well my application works in a client/server setting? Is this the wrong use of jUnit? Thanks.Alsup
M
9

Use org.junit.experimental.ParallelComputer: Sample:

    public class NewTestSuite {

       public static void main(String[] s){

         Class[] cls={TestClassA.class,TestClassB.class,TestClassB.class };  

         //simultaneously all methods in all classes  
         Result result = JUnitCore.runClasses(new ParallelComputer(true, true), cls);
         System.out.print(result.wasSuccessful());

         //simultaneously among classes  
         //Result result = JUnitCore.runClasses(ParallelComputer.classes(), cls);  

         //simultaneously among methods in a class  
         //Result result = JUnitCore.runClasses(ParallelComputer.methods(), cls);  
      }
   } 
Mallorymallow answered 18/2, 2011 at 13:50 Comment(7)
Thanks. That looks promising. Where do I put this code? In my NewTestSuite class file? Or... how would you write a Test Suite which uses your code?Alsup
Welcome. Put the code into a @Test method, or a main method, whatever. And I think there's no need this feature, if the testcases don't spend long time really.Decrial
Thanks. Future test cases could potentially run for hours so it would be nice to run a few at the same time.Alsup
Could this somehow be added to a Test Suite?Alsup
I guess it could, you could try it.Decrial
The problem is I don't know how... Sorry, I am really quite new to jUnit and mostly follow examples I have managed to find on the web. I'll try putting it into a constructor for NewTestSuite and see what happens. :)Alsup
Thanks for the revised answer. However, that seems to break the "Test" in NetBeans. The previous NewTestSuite ran with @RunWith(Suite.class) @Suite.SuiteClasses(value = { TestClassA.class, TestClassB.class, TestClassC.class}) before the start of the class. Can the two be combined? That is, can I run test classes/methods simultaneously without breaking the nice testing output in NetBeans? Thanks!Alsup
P
1

You can try this simple example: I add a assertion,because in JUnitCore,we don't have a build in.

public class TestParallelExecution {
private Logger logger = LoggerFactory.getLogger(TestParallelExecution.class);

@Test
public void runAllTest(){
    //add test class
    Class[] testClasses={testClass1.class, testClass2.class, };

    //Parallel execute only classes
     Result resultClasses = JUnitCore.runClasses(ParallelComputer.classes(), testClasses);

    //Parallel execute only methods in classes
    Result result = JUnitCore.runClasses(ParallelComputer.methods(), testClasses);

    //Run Parallel all methods in all test classes which declare in testClasses[]
    //method accept new ParallelComputer(classes, methods)
    Result result = JUnitCore.runClasses(new ParallelComputer(true, true), testClasses);
    List<Failure> failures = result.getFailures();


    if(result1.wasSuccessful() != true){
        StringBuilder sb = new StringBuilder();
        for(int i = 0; i < failures.size(); i++){
            sb.append(System.lineSeparator());
            sb.append("<---------------New test method--------------->");
            sb.append(System.lineSeparator());
            sb.append(failures.get(i).toString());
            sb.append(System.lineSeparator());
        }
        File file = new File("C:\\..\\FailedTest.txt");
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
            writer.write(sb.toString());
            writer.close();
        } catch (IOException e) {
            logger.trace("I can't create file,because : ", e);
        }
        //logger.error(sb.toString());
        assertTrue(false);
    }else {
        assertTrue(true);
        }
    }
}
Poolroom answered 7/3, 2016 at 8:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.