Does Maven surefire plugin run tests using multiple threads?

harschware picture harschware · Feb 18, 2010 · Viewed 13.3k times · Source

I'm wondering if the Maven surefire plugin either runs tests multi-threaded by default (and if so can the number of threads be controlled? ) or if it runs tests from the Test classes in a random order or predictable order, or if the order can dictated by some means.

I haven't verified this yet (I'll do so tomorrow just looking for some heads up guidance and verification at this point), but it looks as if my various JUnit Test classes are getting the tests run in some intermixed order. Which makes it a real pain to orchestrate the creating of the test resources (which are quite hefty in my case).

Its probably a classic problem I run my suite with the Eclipse JUnit runner and everything runs very linear and plays nice. I go to Maven cmd line and things seems to be stepping all over each other.

Answer

Pascal Thivent picture Pascal Thivent · Feb 18, 2010

By default, Maven runs your tests in a separate ("forked") process, nothing more (this can be controlled using the forkMode optional parameter).

If you are using TestNG or Junit 4.7+ (since SUREFIRE-555), it is possible to run tests in parallel (see the parallel and the threadCount optional parameters) but that's not a default.

Now, while I'm not sure if the surefire plugin behaves the same as JUnit, it is possible to get some control by manually creating a TestSuite and specify the order in which tests are executed:

TestSuite suite= new TestSuite();
suite.addTest(new MathTest("testAdd"));
suite.addTest(new MathTest("testDivideByZero")); 

You are however strongly advised never to depend upon test execution order, unit tests really should be indeed independent.

P.S.: Just in case, there is also this request SUREFIRE-321 (to run tests in alphabetical order) that you might want to vote for.