Gradle to run tests from “test” dependency jars

(suman jakkula) #1

Anyone able to run tests from “test” dependency jars in gradle build? I have a gradle build script which includes few test-jars as well under testRuntime dependency. I would like to run the tests in these dependencies using “gradle test”.

I see that gradle does not have out-of-box solution to run tests from a jar as mentioned in this link. I am trying to follow the “unpack” option suggested in this post. Am not sure how do I tie the unpack task with test task to iterate over all the test-jar dependencies and run the tests?

I know that we do not have to run tests of dependencies in consuming projects. But for my reasons, I have to do this.

I used the below to code to run tests from a jar. But what I want is a generic task such as “runTestsFromDependencyJars” which goes through all the test configuration dependencies and run the test. Not sure how do I get it to run for all such dependencies?

task unzip(type: Copy )
        from zipTree(file('jar file with absolute path'))
        into file("$temporaryDir/")
      task testFromJar(type: Test , dependsOn: unzip) {
        doFirst {

Any gradle experts on how to achieve this?

(suman jakkula) #2

Found the solution using ant junit approach.

configurations {
        testsFromJar {
        transitive = false
   dependencies {
        junitAnt('org.apache.ant:ant-junit:1.9.3') {
            transitive = false
        junitAnt('org.apache.ant:ant-junit4:1.9.3') {
            transitive = false
      compile "groupid:artifact1name:version"
    compile "groupid:artifact2name:version"
    testsFromJar ( group:'groupid', name:'artifact1 name', version:'version',classifier:'tests')
    testsFromJar ( group:'groupid', name:'artifact2 name', version:'version',classifier:'tests')
 ant.taskdef(name: 'junit', classname: '',
            classpath: configurations.junitAnt.asPath)
    task runTestsFromJar(
) << {
        configurations.testsFromJar.each {
            file ->
                ant.junit(printsummary:'on', fork:'yes', showoutput:'yes', haltonfailure:'yes') { //configure junit task as per your need
                    formatter (type:'xml')
                    batchtest(todir:"$temporaryDir", skipNonTests:'true' ) {
                    classpath {