How do I get Emma or Cobertura, with Maven, to report coverage on source code in other modules?

前端 未结 5 819
孤城傲影
孤城傲影 2021-02-01 08:57

I have a multi-module Maven setup with Java code.

My unit tests, in one of the modules, exercise code in multiple modules. Naturally, the modules have inter-dependenc

5条回答
  •  别那么骄傲
    2021-02-01 09:57

    This recent blog post by Thomas Sundberg contains a method that partially solves the issue by using ant for the cobertura calls, instead of using the maven cobertura plugin.

    It relies on the following basic approach with specialised pom.xml and build.xml files :

    Start with a typical maven compile on the parent pom, which will compile all classes in the child modules.

    mvn clean compile # maven-compile-plugin called for compiling
    

    Then instrument all of the module classes:

    ant instrument # cobertura called for instrumentation
    

    Then call the maven-surefire-plugin called for testing using the instrumented classes, with cobertura as a test dependency

    mvn test 
    

    Then use a custom report call to pull in all of the results from different modules:

    ant report # cobertura called for reporting
    

    The key elements of the ant build.xml file are to instrument all modules separately and then to report on all of the modules after merging the results. This function needs to be called for each module in his example:

    
        
        
            
                
            
        
    
    

    Then after the testing is complete, the reporting phase first merges all results from all of the different directories are merged into a new .ser file (called sum.ser in his example)

    
        
        
            
            
            
        
    
    
    
        
            
                
            
        
    
    

    It may be possible to integrate the ant components into maven using the antrun plugin, but I am not familiar enough with the phases/lifecycles to know where to put the different calls.

    This is very useful for me, as I write abstract test classes in my api modules and then provide them with an implementation in my lib modules. So far both cobertura and emma have been unable to handle this design so my code coverage is typically 0 or in the single digits.

提交回复
热议问题