合并相同DLL的代码覆盖率

时间:2013-12-10 15:56:59

标签: mstest code-coverage

我正在使用MSTEST.EXE(Visula Studio 2010)生成的代码覆盖率。 我有三种类型的测试在同一个dll上运行,它生成三个代码覆盖文件。

  1. moq单元测试(作为门控办理登机手续)
  2. 整合单元测试(Jenkins中的单独工作)
  3. GUI单元测试(Jenkins中的单独工作)
  4. 我想将文件中的所有三个代码覆盖合并为一个,以生成合并报告。 我尝试了以下代码。

    Public Function JoinCoverageFiles(pcolCoverageFiles As List(Of String)) As CoverageInfo
            Dim lobjResultCoverageInfo As CoverageInfo = Nothing
            Console.WriteLine("Joining coverage file(s)...")
            Try
                For Each lstrCoverageFile As String In pcolCoverageFiles
    
                    Dim lobjCurrentCoverageInfo As CoverageInfo = CoverageInfo.CreateFromFile(lstrCoverageFile)
    
                    If lobjResultCoverageInfo Is Nothing Then
                        lobjResultCoverageInfo = lobjCurrentCoverageInfo
                        Continue For
                    End If
    
                    Dim lobjJoinedCoverageInfo As CoverageInfo = Nothing
    
                    Try
                        lobjJoinedCoverageInfo = CoverageInfo.Join(lobjResultCoverageInfo, lobjCurrentCoverageInfo)
                    Finally
                        lobjCurrentCoverageInfo.Dispose()
                        lobjResultCoverageInfo.Dispose()
                    End Try
    
                    lobjResultCoverageInfo = lobjJoinedCoverageInfo
                Next
            Catch generatedExceptionName As Exception
                If lobjResultCoverageInfo IsNot Nothing Then
                    lobjResultCoverageInfo.Dispose()
                End If
                Throw
            End Try
            Console.WriteLine("Joining coverage file(s)... Done")
            Return lobjResultCoverageInfo
    End Function
    

    使用此代码,我可以加入代码覆盖率文件,但我想要合并结果,这样我就可以知道在所有三个单元测试中覆盖和发现了哪些代码。

1 个答案:

答案 0 :(得分:2)

您可以使用CoverageInfo类获取所有运行的合并覆盖率信息。

string ccFileMerged = Path.Combine(Path.GetTempFileName + ".coverage");
CoverageInfo.MergeCoverageFiles("A.coverage", "B.coverage", ccFileMerged);
// now you can use the ccFileMerged to get your merged data

但是你应该注意,合并会根据一些静态信息对dll进行分组,例如。 dll guid,每个版本重新生成。因此,只要您确保使用相同 dll的运行生成所有.coverage文件,合并将是无缝的。如果每次运行生成这些dll,您最终会在.coverage文件中看到每个唯一dll的dll条目