Domanda

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?

È stato utile?

Soluzione

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)
        }
    }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top