Running a test in parallel in @Rule

I want to reuse some integration tests for load testing. I applied a rule that is parameterized by annotation:

@Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface Parallel { int invocations() default 1; int rampUpTime() default 0; } 

In my implementation of the rule, the annotation is evaluated and an operator is established that has an evaluation method:

 @Override public void evaluate() throws Throwable { ScheduledExecutorService exe = Executors.newScheduledThreadPool(invocations); for (int i = 0; i < invocations; i++) { ScheduledFuture<?> scheduledFuture = exe.schedule(new Runnable() { @Override public void run() { try { invocated++; // Request test = Request.method(description.getTestClass(), description.getMethodName()); // new JUnitCore().run(test); statement.evaluate(); } catch (Throwable e) { e.printStackTrace(); } } }, i * rampUpTime, this.timeUnit); futures.add(scheduledFuture); } } 

So, the evaluate call ends in Runnable() and is scheduled as described in the annotation. The fact is that in my rule only planning occurs, the runner does not know (or does not care) about all the runes that work only until he sets up the whole set of tests. Therefore, I am trying to add evalute() calls to the test runner. First tried to use JUnitCore.run(...) , which of course ends in recursion.

The next attempt was to collect all the futures and wait for their completion. This works fine on every test basis, but I want to run a whole set of tests in parallel. And also, in my test report, I only see that the test is run once.

So, I thought that I was using a parameterized set with a context (an object that collects all futures from all tests) as a parameter, but I did not find a way to promote this context object to tests, each test to have its own parameters.

Now I ask you to specify a way to add several executions from my rule to the test runner that executes it.

+7
java junit junit-rule
source share
1 answer

If I understand you correctly, you are trying to control the behavior of the test runner. A TestRule may be limited to achieve this. But what about writing your own Runner that controls the flow of your unit test? Just take a look at ParentRunner from JUnit . From there, you should get a pretty good idea about how basic planning works, and perhaps implement your own version of org.junit.experimental.ParallelComputer .

+2
source share

All Articles