Run parallel test task using gradle

此生再无相见时 提交于 2019-11-27 10:52:44

问题


We use Junit as a test framework. We have many projects. We use gradle(Version 1.12) as a build tool. To run the unit test parallelly using gradle we use the bellow script in every projects under test task.

maxParallelForks = Runtime.runtime.availableProcessors()

Ex:

 test {    
         maxParallelForks = Runtime.runtime.availableProcessors()       
    }

We maintain the single gradle.properties file also. Is it possible to define test.maxParallelForks = Runtime.runtime.availableProcessors() in gradle.properties file rather than defining in each build.gradle files under test task?


回答1:


$rootDir/build.gradle:

subprojects {
    tasks.withType(Test) {
        maxParallelForks = Runtime.runtime.availableProcessors()
    }
}



回答2:


The accepted answer above works but the Gradle documentation here suggests you use

maxParallelForks = Runtime.runtime.availableProcessors().intdiv(2) ?: 1

I tried both and after testing both on a 2.3 GHz Intel Core i7 Mac Book Pro with 16GB RAM (4 cores with hyperthreading)

test {
    maxParallelForks = Runtime.runtime.availableProcessors()
}

and

test {    
    maxParallelForks = Runtime.runtime.availableProcessors().intdiv(2) ?: 1      
}

The approach suggested by Gradle documentation produced faster response times for our unit test suite: 7 minutes vs. 8 minutes (compared to the original 13 minutes). In addition my Mac CPU didn't get pegged and the fan didn't kick off.

I assume there is either contention on a shared resource - even if it is only the machine one which we are running the unit tests.



来源:https://stackoverflow.com/questions/23805915/run-parallel-test-task-using-gradle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!