2014-01-29 47 views
4

任何人都可以从gradle中的“测试”依赖瓶运行测试吗? 我有一个gradle构建脚本,其中包括testRuntime依赖项下的几个测试jar。我想使用“gradle test”在这些依赖项中运行测试。Gradle从“测试”依赖瓶中运行测试

我看到Gradle并没有开箱即用的解决方案来运行从this link中提到的jar中的测试。我试图按照这篇文章中建议的“解压缩”选项。 我不确定如何将解压缩任务与测试任务绑定以遍历所有测试jar依赖项并运行测试? PS:我知道我们不必在消耗项目中运行依赖关系测试。但出于我的原因,我必须这样做。

任何gradle专家如何实现这一目标?

[编辑]
我用下面的代码从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 
     } 
    } 
+0

您是否尝试过在这里:_ [testClassesDir(http://www.gradle.org/docs/current/dsl/org.gradle.api.tasks.testing.Test.html#org.gradle。 api.tasks.testing.Test:testClassesDir)_? 如何使用_project.sourceSets.test.output.classesDir_路径作为解包目的地? – topr

+0

@topr请参阅我的编辑。我可以运行特定的jar。但不知道如何使其对所有测试依赖项通用。 –

回答

2

实测值使用蚂蚁的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) 
        } 
       } 
     } 
    }