4

I was writting JUnit test. I would like to know if the tests within a test class can run in parallel.

class TestMyClass {

    @Test
    public void test1() {
    }

    @Test
    public void test2() {
    }

}

Will Junit ever run test1() and test2() in parallel?

Keen Sage
  • 1,899
  • 5
  • 26
  • 44
  • Possible [duplicate](http://stackoverflow.com/questions/423627/running-junit-tests-in-parallel) – RNJ Sep 20 '12 at 19:20
  • Possible duplicate: http://stackoverflow.com/questions/3693626/how-to-run-test-methods-in-spec-order-in-junit4 – nogard Sep 20 '12 at 19:21

4 Answers4

4

Consider TestNG if you are looking for Parallel tests execution.

Santosh
  • 17,667
  • 4
  • 54
  • 79
1

Yes, you can. Take a look at this question for details on how to set that up. The correctness of your tests should not really on this behaviour though. Your tests should run correctly if they're run concurrently or not.

Community
  • 1
  • 1
Oleksi
  • 12,947
  • 4
  • 56
  • 80
1

I cannot directly answer on whether jUnit will run them in parallel or not, but theoretically that shouldn't matter. The only thing you should keep in mind is the sequence of execution you can bet on, like

  • setup
  • execution of test
  • teardown

This should be enough, as each single test should be completely independent from each other. If your tests depend on the order they're executed or whether they run in parallel, then you probably have some wrong dependencies.

Juri
  • 32,424
  • 20
  • 102
  • 136
-1

No, because a fixture is setUp before each test. Running test in parallel could change the fixture state. I guess you could write a test executor to run tests in parallel.

Assen Kolov
  • 4,143
  • 2
  • 22
  • 32