4

Does anyone know how to run the tests from a different gradle project and still get emma coverage reporting data?

Here is my current layout:

Root/
  settings.gradle (no explicit build.gradle - just defines all subprojects)
  SubProjectA/
    build.gradle
    src/ (all real source is here)
  SubProjectATest/
    build.gradle
    src/ (all testing code is here)
  SubProjectB/ (similar structure as A)
  SubProjectBTest/ (similar structure as ATest)

I am currently using the emma plugin, and I would like to build SubProjectA and run all the tests in SubProjectATest from within the build.gradle of SubProjectA.

Here are some things I tried inside the build.gradle of SubProjectA

  1. testCompile project(':SubProjectATest').sourceSets.test.classes (as suggested by this article), but I got an error "Could not find property 'sourceSets' on project"
  2. Just the straight-up testCompile project(':SubProjectATest'), but then I get "..SubProjectA/build/classes/test', not found" and also "Skipping task ':SubProjectA:compileTestJava' as it has no source files."
  3. Simply adding a sourceSet like the following:

    test { java { srcDir '../SubProjectATest/src' } }

Adding the source set in (option 3) is the only option that worked, but it seems sloppy to do it this way. Does anyone know how to do this using project dependencies?

Update #1 I also tried one of the answers below to use test.dependsOn and the tests do run, but the emma plugin reported the following: build/classes/test', not found

Community
  • 1
  • 1
Keith P
  • 2,150
  • 1
  • 18
  • 15

1 Answers1

4

1. and 2. just add classes to the test compile class path. This doesn't have any effect on which tests are going to be executed.

3. is the wrong approach because you should not add sources from project X to project Y.

If what you want is that gradle :SubProjectA:test also executes :SubProjectATest:test, all you need to do is to add a task dependency:

SubProjectA/build.gradle:

test.dependsOn(":subProjectATest:test")

By the way, what is your motivation for putting the tests in a separate project?

Peter Niederwieser
  • 121,412
  • 21
  • 324
  • 259
  • Motivation? Good question. I am using Code Pro Analytix to generate some tests on some basic DTOs (only getters and setters), and this automagically throws them in a new test project. This is fine with me because it is generated code. Is this non-standard in Java? I am not a Java expert by any means. I am also new to gradle and groovy. This is pretty standard in a CMake project though. "test.dependsOn" – Keith P Jan 04 '12 at 17:41
  • Note: adding the test.dependsOn is great because the tests do execute, but emma still can't find build/classes/test so no code coverage results are reported. Thanks for looking into this though! I will clarify my question a bit. – Keith P Jan 04 '12 at 17:47
  • Did you apply the Emma plugin to the SubProjectATest project? That's where it belongs. – Peter Niederwieser Jan 04 '12 at 18:47
  • If you never interact with SubProjectATest directly (but always via SubProjectA), it's a sign that it shouldn't be its own Gradle project. You can just add the generated sources to SubProjectA's test source set (no matter where they live physically), or add an additional source set (say 'generatedTests') and test task to SubProjectA. – Peter Niederwieser Jan 04 '12 at 18:51
  • Thanks! This is very helpful Peter. I will keep it as a sourceSet! – Keith P Jan 04 '12 at 22:09