Gradle从" test"运行测试依赖罐子

时间:2014-01-29 18:05:49

标签: gradle

任何能够从"测试"运行测试的人gradle构建中的依赖jar? 我有一个gradle构建脚本,其中包含一些test-jar以及testRuntime依赖项。我想使用" gradle test"。

在这些依赖项中运行测试

我看到gradle没有开箱即用的解决方案来运行jar中的测试,如this link中所述。我正在努力遵循"解压缩"本文中建议的选项。 我不确定如何将解包任务与测试任务联系起来迭代所有测试jar依赖项并运行测试? PS:我知道我们不必在使用项目时运行依赖项测试。但出于我的原因,我必须这样做。

任何关于如何实现这一目标的专家?

[编辑]
我使用下面的代码从jar运行测试。但我想要的是一个通用的任务,例如" runTestsFromDependencyJars"它遍历所有测试配置依赖项并运行测试。不知道如何让它运行所有这些依赖项?

    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
        }
    }

1 个答案:

答案 0 :(得分:2)

使用ant junit方法找到解决方案。

 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)
                    }
                }
        }
    }