Search code examples
unit-testingcode-coveragecobertura

How to merge code coverage results in VSTS


I have a Visual Studio solution with multiple projects. I have added corresponding unit test projects in my solution. I am using Cobertura in VSTS to find the code coverage for these projects.

Cobertura publishes separate Cobertura.xml files for all the test projects. 

I would like a way to have a merged code coverage report. I couldn't specify path to multiple summary files in VSTS task for publishing code coverage. Is there a way I could achieve that ?

Additional info - All test cases are run against the same DB


Solution

  • I couldn't find any direct way to do this. So, I used powershell script in another VSO task to achieve the result. Since the tool generates xml with number of lines in one of the attributes, I have written a script to get the files and read that count and generate new xml with desired Coverage report. It worked since all test projects are for same Database and every project targets different set of stored procedures. It doesn't work if two tests target same stored procedure.

    Hope this helps.

    * EDIT Adding Powershell Script which I used to merge (First 3 parameters are coverage paths from different unit test projects) *

    Param(
    [string]$UnitTestPath1,
    [string]$UnitTestPath2,
    [string]$UnitTestPath3,
    [string]$targetCoverageFilePath
     )
    
    
    
    [xml]$XmlContent = Get-Content $UnitTestPath1
    $coverage = [float]$XmlContent.coverage.'lines-covered'
    
    $XmlContent = Get-Content $UnitTestPath2
    $coverage += [float]$XmlContent.coverage.'lines-covered'
    
    $XmlContent = Get-Content $UnitTestPath13
    $coverage += [float]$XmlContent.coverage.'lines-covered'
    $coverage
    
    $node = $XmlContent.SelectNodes("/coverage");
    
    $node[0].SetAttribute('lines-covered',$coverage) 
    $XmlContent.Save($targetCoverageFilePath)
    
    $coverage