Dependencies defined with project(':projectname').sourceSets.test.output are not transitive

I have a multi project build with project1, project2 and project3. Project3 compileTest depends on project2-tests and project2 compileTest depends on project1-tests

I have defined the dependencies for project3 like this:

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

I have defined the dependencies for project2 like this:

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

This does not seem to work. The build of project3 complains about classes from project1 missing. I.e. the dependency in project 2 is not transitive.

However it works if I change the project3 dependencies to look like this:

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

Is this the desired behavior or should dependencies like this be transitive?

Thanks, Simon

That’s not how project dependencies are declared. You can either do ‘testCompile project(":project2")’ (which is shorthand for ‘testCompile project(path: “:project2”, configuration: “default”)’, but that won’t, by default, give you the other project’s test code. Or you can do ‘testCompile project(path: “:project2”, configuration: “test”)’. In that case case, you’ll have to make project2 declare a configuration named ‘test’ (via a ‘configurations {}’ block) and add, say, a test Jar to it (via an ‘artifacts {}’ block). To see some examples, search the Gradle User Guide for ‘artifacts {’.

Many thanks. I fixed the problem by including the following in build.gradle for project1:

configurations {
 tests {
     extendsFrom testCompile
        }
}
  artifacts {
 tests testJar
}

and then declaring the dependency in build.gradle for project2 like this:

dependencies {
 testCompile project( path: ':project1', configuration: 'tests')
}

It didn’t work if I named my configuration “test”, it had to be “tests” or something else.