Multi-project test dependencies with gradle
Use Gradle's test fixtures to share test code among projects. Apply the java-test-fixtures
plugin and define testFixtures
project dependencies. It ensures clear and DRY test configurations.
Making Project A's test output a testCompile
dependency for Project B
For older Gradle versions, define testArtifacts in Project A's build.gradle
to share its test outputs:
Then, in Project B, reference this testArtifacts configuration:
Important: apply the java plugin in Project B to ensure proper compilation and dependency resolution.
Sharing test resources in multi-project builds
In a multi-project set-up, create a separate source set for Project A if it contains substantial test classes or resources:
Good practices for advanced gradle configurations
In complex cases where direct task dependencies or custom JAR creation is needed, remember to keep things simple and clearly defined.
Ensuring task dependencies
To compile Project B only after Project A's tests are compiled, add a dependency on the task:
Creating a custom JAR for test classes
If you need a custom JAR for Project A's test classes:
Then, reference this JAR as a dependency in Project B:
Best practices and common pitfalls
Ensuring compatibility
Before kicking off, ensure compatibility among varying Gradle versions. You may resolve potential incompatibilities with conditional statements.
Organizing source sets
Avoid mixing the main and test classes. Use testFixtures
to maintain a neat separation.
Catering to multi-language projects
Multi-language setups may require you to ensure proper cross-language test support.
Was this article helpful?