如何在Gradle项目中共享测试类? [英] How to share test classes across Gradle projects?

查看:89
本文介绍了如何在Gradle项目中共享测试类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Gradle构建和测试我的项目。我有两个项目:

I am using Gradle to build and test my projects. I have two projects:

ProjA  contains      src\test\java\BaseTest.java       
ProjB  contains      src\test\java\MyTest.java

MyTest extends BaseTest

当我运行 ProjB.gradle 时,如何获取ProjA中的 BaseTest 类?

When I run ProjB.gradle, how do I get it to see the BaseTest class from ProjA?

我尝试添加:

I tried adding:

dependencies {
  testCompile project('ProjA') 
}

但它没有用。

推荐答案

也许有更好,更简单的方法,更简洁的方法,但我认为您在这里有三个选项。

Maybe there are better, simpler ways, cleaner ways, but I think you have three options here.

你可以简单地创建一个测试子项目,其中BaseTest在src / main / java中定义,而不是src / test / java。其他两个子项目的 testCompile 配置都会依赖于项目('testing')

Since BaseTest is a class that is in fact part of a reusable testing library (you use it in both projects), you could simply create a testing subprojects, where BaseTest is defined in src/main/java and not src/test/java. The testCompile configuration of the other two subprojects would both have a dependency on project('testing').

在第二个选项中,您可以在第一个项目中定义一个额外的工件和配置:

In this second option, you would define an additional artifact and configuration in the first project:

configurations {
  testClasses {
    extendsFrom(testRuntime)
  }
}

task testJar(type: Jar) {
  classifier = 'test'
  from sourceSets.test.output
}

// add the jar generated by the testJar task to the testClasses dependency
artifacts {
  testClasses testJar
}

,你会在第二个项目中依赖这个配置:

and you would depend on this configuration in the second project:

dependencies {
  testCompile project(path: ':ProjA', configuration: 'testClasses')
}



第三选项



基本上和第二个一样,除外它不会为第一个项目添加新配置:

Third option

Basically the same as the second one, except it doesn't add a new configuration to the first project:

task testJar(type: Jar) {
  classifier = 'test'
  from sourceSets.test.output
}

artifacts {
  testRuntime testJar
}

dependencies {
  testCompile project(path: ':one', configuration: 'testRuntime')
}

这篇关于如何在Gradle项目中共享测试类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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