Gradle从“测试”运行测试依赖瓶 [英] Gradle to run tests from "test" dependency jars

查看:135
本文介绍了Gradle从“测试”运行测试依赖瓶的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以在Gradle构建中运行测试依赖jar包中的测试?
我有一个gradle构建脚本,它包含几个test-jar以及testRuntime依赖关系。我希望使用gradle test在这些依赖项中运行测试。



我发现gradle没有从盒子运行测试的开箱即用解决方案如此链接中所述。我试图按照这篇文章中建议的解压缩选项。
我不确定如何将解压缩任务与测试任务绑定在一起,以遍历所有测试jar依赖项并运行测试?
PS:我知道我们不必在消耗项目中运行依赖项测试。但是出于我的原因,我必须这样做。



任何有关如何实现此目的的gradle专家?



我使用下面的代码来从jar中运行测试。但是我想要的是一个通用任务,比如runTestsFromDependencyJars,它通过所有测试配置依赖并运行测试。不知道如何让它运行所有这些依赖项?

 任务解压缩(类型:复制){
从zipTree(文件(具有绝对路径的jar文件))
到文件($ temporaryDir /)
}

任务testFromJar(类型:Test,dependsOn:unzip ){
doFirst {
testClassesDir = file($ temporaryDir /../ unzip /)
classpath = files(testClassesDir)+ sourceSets.main.compileClasspath + sourceSets.test.compileClasspath
}
}


解决方案

解决方案使用ant junit方法。

 配置{
testsFromJar {
transitive = false
}
junitAnt


依赖关系{
junitAnt('org.apache.ant:ant-junit:1.9.3'){
transitive = false
}
junitAnt('org.apache.ant:ant-junit4:1.9.3'){
transitive = false
}

编译groupid:artifact1name:version
编译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(名称:'junit',classname:'org.apache.tools.ant.taskdefs.optional.junit.JUnitTask',
classpath:configurations.junitAnt .asPath)


任务runTestsFromJar()<< {
configurations.testsFromJar.each {
file - >
ant.junit(printsummary:'on',fork:'yes',showoutput:'yes',haltonfailure:'yes'){//根据需要配置junit任务
格式化程序(类型: 'xml')
batchtest(todir:$ temporaryDir,skipNonTests:'true'){
zipfileset(src:file,
包含:** / * Test.class,

}
classpath {
fileset(file:file)
pathelement(path:sourceSets.main.compileClasspath.asPath + sourceSets.test.compileClasspath.asPath)
}
}
}
}


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

解决方案

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

这篇关于Gradle从“测试”运行测试依赖瓶的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆