Gradle module output configuration for test dependencies only

Viewed 659

I have a Gradle project containing the two modules app and test, where test contains utilities for testing only. Now I'd like to setup this module, so it doesn't include any outputs into the main configuration of app; even not accidentally.

implementation project(':test') # should fail or not contain any inputs

testImplementation project(':test') # should include all inputs
androidTestImplementation project(':test') # should include all inputs

How do I configure test to behave like this?

I'd assume it would be similarly to how the Android plugin handles configurations for build types and flavors, but I'm also not sure on how to figure this out.

I think these are handled with Gradle consumer attributes. Maybe there's a filter or an attribute which could be applied to it to make it only available to tests.

3 Answers

The java-test-fixtures plugin almost behaves like I wished, just a bit more verbose.

implementation project(':test') # should fail or not contain any inputs

testImplementation testFixtures(project(':test'))
androidTestImplementation testFixtures(project(':test'))

Some other downsides for now

As Tom Tresansky Is the java-test-fixtures plugin incompatible with the Build and run using IntelliJ IDEA setting? asked and answered, the Android Studio doesn't handle the testFixtures source set well.

Also it is not yet available for Kotlin or Android. But as Michael Evans pointed out, there's tasks already open for Android and on YouTrack.

But creating the test fixtures with a Kotlin JVM project and consuming these with an Android project works fine for me.

Related