Gradle to run tests from "test" dependency jars

gradle

Solution

Found the solution using ant junit approach.

 configurations {
        testsFromJar {
        transitive = false
        }
        junitAnt
 }

 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: 'org.apache.tools.ant.taskdefs.optional.junit.JUnitTask',
            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' ) {
                        zipfileset(src:file,
                                includes:"**/*Test.class",
                        )
                    }
                    classpath {
                        fileset(file:file)
                        pathelement(path:sourceSets.main.compileClasspath.asPath+sourceSets.test.compileClasspath.asPath)
                    }
                }
        }
    }

Problem

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? PS: I know that we do not have to run tests of dependencies in consuming projects. But for my reasons, I have to do this. Any gradle experts on how to achieve this? [EDIT] 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 { testClassesDir=file("$temporaryDir/../unzip/") classpath=files(testClassesDir)+sourceSets.main.compileClasspath+sourceSets.test.compileClasspath } } ```

Original source