2

I have a legacy application that has a unit test module that's separate from the application modules. I'm converting the project to use Gradle and the structure looks like this:

/root
  /module1
  /module2
  ...
  /moduleN
  /test

where the test module executes tests for module1 through moduleN (and depends on them). I know this isn't a very good practice as it kinda defeats the purpose of unit tests but as all know, legacy code is always a headache to work with.

So before I start refactoring the code so that each module has its own unit tests (which means disassembling the test module in a sensible way, i.e., a lot of work) I wanted to find a temporary solution to get the correct code coverage, i.e., have JaCoCo instrument all the classes from module1, ..., moduleN instead of just module test.

Is there a way to tell JaCoCo to instrument classes from other modules?

Giovanni Botta
  • 9,626
  • 5
  • 51
  • 94

1 Answers1

2

To include coverage results from subprojects "module*" in the "test" project, you might want to try something like this in your build.gradle from the test project:

// [EDIT] - 'afterEvaluate' NOK, use 'gradle.projectsEvaluated' instead (see comments)
// afterEvaluate {
gradle.projectsEvaluated {
    // include src from all dependent projects (compile dependency) in JaCoCo test report
    jacocoTestReport {
        // get all projects we have a (compile) dependency on
        def projs = configurations.compile.getAllDependencies().withType(ProjectDependency).collect{it.getDependencyProject()}
        projs.each {
            additionalSourceDirs files(it.sourceSets.main.java.srcDirs)
            additionalClassDirs files(it.sourceSets.main.output)
        }
    }
}
roomsg
  • 1,811
  • 16
  • 22
  • Thanks for the reply. I tried that but the build fails with the following error: `Could not find property 'sourceSets' on project 'modulex'` – Giovanni Botta Dec 18 '13 at 14:39
  • Note that 'modulex' is indeed a java project so the sourceSets should be defined. – Giovanni Botta Dec 18 '13 at 14:40
  • And [here's the answer](http://stackoverflow.com/questions/15347364/gradle-multiproject-gives-could-not-find-property-sourcesets-on-project-erro) to that question. – Giovanni Botta Dec 18 '13 at 14:50
  • So the above snippet works if I replace `afterEvaluate` with `gradle.projectsEvaluated`. – Giovanni Botta Dec 18 '13 at 15:04
  • Thx for the closer investigation/feedback/link, I was not aware of the 'gradle.projectsEvaluated'. Updated the answer accordingly. – roomsg Dec 18 '13 at 19:22