I can't seem to share test code between projects in a multi-project build


(Scott Palmer) #1

I have a (poorly organized) project that I’m converting from Maven. In it there is a module that has tests that depend on test classes from a sibling project. I’ve tried to set up the dependencies so this works, but I’m still getting errors when one project’s test code attempts to import classes from another project’s test code.

E.g. assume two projects: Server and ServerWeb

In build.gradle for ServerWeb there are dependencies on Server

apply plugin: 'war’
apply plugin: ‘gwt’

dependencies {
providedCompile project(’:Server’)
testCompile project(path: ‘:Server’, configuration: ‘testCompile’)
}

When it compiles the test classes for ServerWeb, it fails to import test classes from Server. I can see that the classes are there in the Server project when I look in Server/build/classes/test/

I also tried depending on ‘testRuntime’ instead of ‘testCompile’, but it made no difference.
I’m running Gradle 2.11.

Is there a trick to getting this to work? I obviously expected what I had to do the trick, so maybe there is a bug somewhere… either in my script or in how Gradle handles the class path for tests.


(Nikolay Nachev) #2

you need the compiled class files in your test class path:

dependencies {
  ...
  testCompile project(':Server').sourceSets.test.output
}

(Stefan Oehme) #3

In your Server project:

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

In your ServerWeb project:

testCompile project(path: ":Server", configuration: 'testArtifacts')

(Scott Palmer) #4

Thanks to both of you. I went with Nikolay’s solution as I would like to avoid test artifact. I would like to refactor things so this dependency isn’t required at some point anyway.


(Vignesh) #5

Getting error on sourceSets.test.output for both the solutions,

Could not get unknown property 'test' for SourceSet container of type org.gradle.api.internal.tasks.DefaultSourceSetContainer.

can anyone kindly help me
I am using Gradle 4.6 in android studio 3.2.1