Gradle 测试依赖性
我有两个项目,项目 A 和项目 B。两者都是用 groovy 编写的,并使用 gradle 作为构建系统。
项目 A 需要项目 B。 这对于编译和测试代码都适用。
如何配置项目A的测试类可以访问项目B的测试类?
I have two projects, project A and Project B. Both are written in groovy and use gradle as their build system.
Project A requires project B.
This holds for both the compile and test code.
How can I configure that the test classes of project A have access to the test classes of project B?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
对于 Gradle
1.5
For Gradle
1.5
对于最新 gradle 版本的 Android(我目前使用的是 2.14.1),您只需在项目 B 中添加以下内容即可从项目 A 获取所有测试依赖项。
For Android on the latest gradle version (I'm currently on 2.14.1) you just need to add the below in Project B to get all the test dependencies from Project A.
您可以通过“测试”配置公开测试类,然后定义对该配置的 testCompile 依赖项。
我对所有 java 项目都有这个块,它会 jars 所有测试代码:
然后,当我有测试代码时,我想在我使用的项目之间访问
This is for Java;我认为它也应该适用于 groovy。
You can expose the test classes via a 'tests' configuration and then define a testCompile dependency on that configuration.
I have this block for all java projects, which jars all test code:
Then when I have test code I want to access between projects I use
This is for Java; I'm assuming it should work for groovy as well.
这是一个更简单的解决方案,不需要中间 jar 文件:
这个问题有更多讨论:使用gradle进行多项目测试依赖
This is a simpler solution that doesn't require an intermediate jar file:
There's more discussion in this question: Multi-project test dependencies with gradle
现在 Gradle 中支持此功能(自 5.6 起)
带有
java
或java-library
插件的模块还可以包含java-test-fixtures
插件,它公开了要与 testFixtures 帮助程序一起使用的帮助程序类和资源。这种方法针对工件和分类器的好处是:实用程序示例:
:modul: one
modul/one/build.gradle
或
lazyly
只需添加主实现
配置的所有依赖项:modul/one/src/ testFixtures/java/com/example/Helper.java
:modul:other
modul/other/build.gradle
modul/other/src/test /java/com/example/other/SomeTest.java
有关更多信息,请参阅文档:
https://docs.gradle.org/current/userguide/java_testing.html #sec:java_test_fixtures
This is now supported as a first class feature in Gradle (since 5.6)
Modules with
java
orjava-library
plugins can also include ajava-test-fixtures
plugin which exposes helper classes and resources to be consumed with testFixtures helper. Benefit of this approach against artifacts and classifiers are:Example:
:modul:one
modul/one/build.gradle
or
lazyly
just add all dependencies of mainimplementation
configuration:modul/one/src/testFixtures/java/com/example/Helper.java
:modul:other
modul/other/build.gradle
modul/other/src/test/java/com/example/other/SomeTest.java
For more info, see the documentation:
https://docs.gradle.org/current/userguide/java_testing.html#sec:java_test_fixtures
这对我有用(Java)
This works for me (Java)
上述解决方案有效,但不适用于最新版本的 Gradle 1.0-rc3。
The above solution works, but not for the latest version
1.0-rc3
of Gradle.如果 ProjectA 包含您希望在 ProjectB 中使用的测试代码,并且 ProjectB 希望使用 artifacts 来包含测试代码,那么 ProjectB 的 build.gradle 将如下所示
:需要将
archives
命令添加到 ProjectA 的 build.gradle 中的artifacts
部分:现在,当 ProjectA 的工件发布到您的工件时,它们将包含一个 -tests罐子。然后可以将这个 -tests jar 添加为 ProjectB 的 testCompile 依赖项(如上所示)。
If ProjectA contains the test code you wish to use in ProjectB and ProjectB wants to use artifacts to include the test code, then ProjectB's build.gradle would look like this:
Then you need to add an
archives
command to theartifacts
section in ProjectA's build.gradle:Now when ProjectA's artifacts are published to your artifactory they will include a -tests jar. This -tests jar can then be added as a testCompile dependency for ProjectB (as shown above).