3

So I have some code that can read the methods out of a .coverage file...

using (CoverageInfo info = CoverageInfo.CreateFromFile(this.myCoverageFile))
{

    CoverageDS ds = info.BuildDataSet();

    foreach (ICoverageModule coverageModule in info.Modules)
    {
        CodeModule currentModule = new CodeModule(coverageModule.Name);
        byte[] coverageBuffer = coverageModule.GetCoverageBuffer(null);

        using (ISymbolReader reader = coverageModule.Symbols.CreateReader())
        {
            Method currentMethod;
            while (reader.GetNextMethod(out currentMethod, coverageBuffer))
            {
                if (currentMethod != null)
                {
                    currentModule.Methods.Add(currentMethod);
                }
            }
        }

        returnModules.Add(currentModule);
    }
}

... but I want to be able to read .coverage files that have been exported to xml too. The reason for this is that .coverage files require the source dlls be in the exact location they were when code coverage was measured, which doesn't work for me.

When I try to load a coveragexml file using CreateFromFile(string) I get the following exception.

Microsoft.VisualStudio.Coverage.Analysis.InvalidCoverageFileException was unhandled Message=Coverage file "unittestcoverage.coveragexml" is invalid or corrupt.

The coveragexml file opens in Visual Studio just fine, so I don't believe there's any issue with the file's format.

I know that CoverageDS can import an xml file, but the API is less than intuitive and the only example I can find of its use is...

using(CoverageInfo info = CoverageInfo.CreateFromFile(fileString))
{
   CoverageDS data = info.BuildDataSet();
   data.ExportXml(xmlFile);
}

...which tells me nothing about how to actually read the coverage data from that file.

Does someone know how to process code coverage data from a .coveragexml file?

1
  • 1
    For future readers : There is a "clue" at the URL below. "To convert the binary VSTest.Console output to the Microsoft CoverageDS XML format, you may use one of the prebuilt applications referenced in the V0.14 release notes below, or you can build the following converter application:" I ~~think~~ "CoverageInfo.CreateFromFile" is for loading from binary files (from VSTest.Console), not the trx files (which is just xml). See : wiki.jenkins-ci.org/display/JENKINS/MSTest+Plugin What got me was I didn't know (d'oh) that the trx files were just xml. :( Commented Apr 29, 2016 at 14:12

2 Answers 2

3

Probably the best introduction to manipulating code coverage information programmatically is available here and also in the linked ms_joc blog.

I'm pretty sure you can use 'CreateInfoFromFile' with either the .coverage file or the XML file you exported in the sample above.

UPDATE: CreateInfoFromFile throws an exception if the coveragexml is passed as the argument. Here is an alternative:

CoverageDS dataSet = new CoverageDS();
dataSet.ImportXml(@"c:\temp\test.coveragexml");

foreach (CoverageDSPriv.ModuleRow module in dataSet.Module)
{
    Console.WriteLine(String.Format("{0} Covered: {1} Not Covered: {2}", module.ModuleName, module.LinesCovered, module.LinesNotCovered));
}
Sign up to request clarification or add additional context in comments.

3 Comments

Yep, I've seen that exact blog you mentioned, and I have found it the best and most helpful. CreateInfoFromFile seems to throw an exception if you hand it the exported xml file (that was one of the first things I tried). I'll update the problem with it.
Updated with a different approach.
This is exactly what I wanted. I managed to extract the data just using the DataSet interface, but this is much better.
0

Have you tried the CoverageDS.ReadXml(fileName_string) method?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.