Multi-project test dependencies with gradle
Deprecated - For Gradle 5.6 and above use this answer.
In Project B, you just need to add a testCompile
dependency:
dependencies { ... testCompile project(':A').sourceSets.test.output}
Tested with Gradle 1.7.
Simple way is to add explicit task dependency in ProjectB:
compileTestJava.dependsOn tasks.getByPath(':ProjectA:testClasses')
Difficult (but more clear) way is to create additional artifact configuration for ProjectA:
task myTestsJar(type: Jar) { // pack whatever you need...}configurations { testArtifacts}artifacts { testArtifacts myTestsJar}
and add the testCompile
dependency for ProjectB
apply plugin: 'java'dependencies { compile project(':ProjectA') testCompile project(path: ':ProjectA', configuration: 'testArtifacts')}
This is now supported as a first class feature in Gradle. Modules with java
or java-library
plugins can also include a java-test-fixtures
plugin which exposes helper classes and resources to be consumed with testFixtures
helper. Benefit of this approach against artifacts and classifiers are:
- proper dependency management (implementation/api)
- nice separation from test code (separate source set)
- no need to filter out test classes to expose only utilities
- maintained by Gradle
Example
:modul:one
modul/one/build.gradle
plugins { id "java-library" // or "java" id "java-test-fixtures"}
modul/one/src/testFixtures/java/com/example/Helper.java
package com.example;public class Helper {}
:modul:other
modul/other/build.gradle
plugins { id "java" // or "java-library"}dependencies { testImplementation(testFixtures(project(":modul:one")))}
modul/other/src/test/java/com/example/other/SomeTest.java
package com.example.other;import com.example.Helper;public class SomeTest { @Test void f() { new Helper(); // used from :modul:one's testFixtures }}
Further reading
For more info, see the documentation:
https://docs.gradle.org/current/userguide/java_testing.html#sec:java_test_fixtures
It was added in 5.6:
https://docs.gradle.org/5.6/release-notes.html#test-fixtures-for-java-projects