How do i declare a dependency on a module's test code?

I need to declare a dependency on a module’s test code? I have a utility class in a module’s test code that another modules needs to leverage.

I tried this:

dependencies {
    testCompile project(':modules:bam-ae').sourceSets.test.classes
    groovy localGroovy()
}

But it failed with:

Cannot get the value of write-only property ‘classes’ on source set test.

1 Like

One way is to create a new configuration, publish a test (utility) Jar, and depend on the configuration from the other project. Another way is to add a test utility project.

Thanks! Any hints on adding a configuration to publish a test utility jar? My plan is to move this code to it’s own utility project, but that will take a bit.

Phil, what IDEs do you need to support? That drastically changes what the best approach to this problem is.

Well, I’m using IntelliJ. Others on my team use eclipse. I’m not sure why IDEs matter?

All I really need to do is jar up the test classes and then copy to a “lib” dir. Could I do a hook into the assemble task to jar up the test classes?

thanks

I think this is the answer:

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

In my root build.gradle, i have defined this:

configurations {
    testArtifacts
}
  task testJar (type: Jar) {
    baseName = "${project.name}-test"
    from sourceSets.test.output
}
  artifacts {
    testArtifacts testJar
}

Then in the project that needs the test code i declare it as a normal dependency:

testCompile project (path: ":some-project", configuration: 'testArtifacts')

This works fine for eclipse tests and command-line gradle test at least. Can’t say i have tried for intelliJ

3 Likes

Because IDEs have very restrictive models (compared to Gradle) and cannot express all of the things that Gradle can. This is an issue if you are using the ‘idea’ or ‘eclipse’ Gradle plugins.

I’d use this, which is slightly different:

configurations {
    testArtifacts.extendsFrom testRuntime
}
task testJar(type: Jar) {
    classifier "test"
    from sourceSets.test.output
}
artifacts {
    testArtifacts testJar
}

Without ‘testArtifacts.extendsFrom testRuntime’ you won’t get the dependencies of the tests if there are any.

Neither IDEA or Eclipse have the concept of a dependency on tests of a module. Luckily, module dependencies include visibility to any test source defined in that module. Both of the Gradle IDE plugins, will interpret the above code as a plain module dependency (i.e. just like ‘project(":some-project")’) which is pretty much what you want.

You have to be careful about dependency cycles though. You can easily have a situation where you do not have a cycle in the Gradle build, but due to the decreased granularity in the IDE you do have a cycle.

5 Likes