【问题标题】:Gradle to run tests from "test" dependency jarsGradle 从“测试”依赖 jar 运行测试
【发布时间】:2014-02-21 17:28:46
【问题描述】:

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

我看到 gradle 没有开箱即用的解决方案来从 jar 中运行测试,如 this link 中所述。我正在尝试遵循这篇文章中建议的“解包”选项。 不确定如何将 unpack 任务与 test 任务联系起来以遍历所有 test-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
        }
    }

【问题讨论】:

  • 您是否尝试过:testClassesDir?使用 project.sourceSets.test.output.classesDir 路径作为解压目的地怎么样?
  • @topr 请看我的编辑。我能够为特定的 jar 运行。但不知道如何使它适用于所有测试依赖项。

标签: gradle


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-05
    • 1970-01-01
    • 2012-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多