Retrieving codecoverage info programmatically “off-road” code coverage experience.

hi,
i have reffered the url
http://blogs.msdn.com/ms_joc/articles/406608.aspx
that says how we can retrieve the codecoverage info from an assembly/exe.

i am using the code to create the coverage file after retrieving the coverage info.

/*******************************************************

Collecting code coverage info from assemblies/binaries.

********************************************************/

public void CreateCoverageInfo(string strFileArtifact, string strCoverageFilePath)

{

const string VSTS_INSTRUMENTING_APPL = "vsinstr.exe";

Process objProcess = new Process();

StringBuilder objStringBuilder = new StringBuilder("/COVERAGE ");

objStringBuilder.Append(strFileArtifact);

objProcess.StartInfo.FileName = VSTS_INSTRUMENTING_APPL;

objProcess.StartInfo.Arguments = objStringBuilder.ToString();

objProcess.Start();

objProcess.WaitForExit();

Guid objGuid = Guid.NewGuid();

Monitor objCoverageMonitor = new Monitor();

objCoverageMonitor.StartRunCoverage(objGuid, strCoverageFilePath);

objCoverageMonitor.FinishRunCoverage(objGuid);

}


and the code below to extract the coverage data from .coverage file and export into an XML file.

/*******************************************************

'Dumping Coverage info into XML for the covered lines in a

'coverage file collected in method CreateCoverageInfo

********************************************************/

public void GetCoverageInfo(string strCoverageFilePath , string strXMLFilePath)

{

CoverageInfo objCoverageInfo = CoverageInfoManager.CreateInfoFromFile(strCoverageFilePath);

CoverageDS objCoverageData = objCoverageInfo.BuildDataSet(null);

objCoverageData.Lines.WriteXml(strXMLFilePath);

}

the above  function CreateCoverageInfo() creates the coverage file perfectly but with empty results.we can open that coverage file in visual studio to view the coverage info.

and function GetCoverageInfo() creates the XML also perfectly but without any data.

the 2 doubts i have in this regard...
 1.am i missing something to create the coverage file with above function CreateCoverageInfo() ,so that it does'nt extract exact coverage info.

2.i am not able to open any .coverage file that is created by system while testing the project, separately in visual studio.
it throws exception like "can not load symbols".

and the same exception i am getting while opening any existing system generated .Coverage file when i am trying to use in function GetCoverageInfo() by this line of code
CoverageInfo objCoverageInfo = CoverageInfoManager.CreateInfoFromFile
(strCoverageFilePath);

what can be the cause and solutions. 

Note: i am using the VSTS BETA 2.


Thanks.




Answer this question

Retrieving codecoverage info programmatically “off-road” code coverage experience.

  • mb974

    vikassony,

       One possible thing that might be causing your issues is not having access to the pdb files for your binaries at any stage of the process. When instrumenting and analyzing code coverage runs the pdb files for the binaries need to remain right next to them throughout the process. Are you keeping your pdbs around and next to the binaries throughout the process that you described above If this is not the issue, then tell me and I'll see if I can dig up anything else that might be going wrong.

    Ian



  • DarkRoses

    thanks for the reply Ian,
    but it does'nt work according to your suggestion also.

    one more thing i want to know, do we have any thing by that we can run the code coverage tool programmatically befor opening the .coverage file and till completion of reading.

    Regards
    vikas

  • ykhambia

    vikas,

         I belive that I may have the solution to your empty coverage files issue. In the sample code provided on joc's blog between StartRunCoverage and EndRunCoverage is the following line:

    // TODO: Launch some tests or something                    
    // that can exercise myassembly.exe

       
    This is saying that after StartRunCoverage you need to exercise some code in myassembly.exe to collect coverage information. In your current code you do not seem to be doing anything between StartRunCoverage and EndRunCoverage, this is why you are not collecting any data. You need to run some tests or scenarios in between StartRunCoverge and EndRunCoverage to collect coverage information for your assembly. Currently you are creating an empty coverage file and an empty XML which would be the expected behaviour as you are not collecting any data.

       In regard to your question:
    "one more thing i want to know, do we have any thing by that we can run the code coverage tool programmatically befor opening the .coverage file and till completion of reading."

       Could you restate this question for me or explain it a little more I'm not exactly sure what you are asking for here.

    Thanks,

    Ian


  • raj78

    Hi Ian,
       Further to this discussion, as you have pointed out we need to exercise our assembly to get the code coverage results, I have the following question.
       Details:
       I have an exe called codecover.exe
       Also I have created the test project for this which are basically the test cases. This is named as TestProject2.dll
       Now in the section where we need to exercise the assembly(codecover.exe) , I have the following piece of code.

    Process pTester = new Process();

    StringBuilder sb1 = new StringBuilder("/testcontainer:");

    sb1.Append("E:\\samples\\cctm\\bin\\Debug\\TestProject2.dll");

    pTester.StartInfo.FileName = "D:\\Program Files\\Microsoft Visual Studio 8\\Common7\\IDE\\mstest.exe";

    pTester.StartInfo.Arguments = sb1.ToString();

    pTester.Start();

    But this has resulted in me getting the codecoverage file run for the test results (namely TestProject2.dll) instead of CodeCover.exe......

    Am I doing something wrong
     
    Thanks in advance,
    Guru



  • Retrieving codecoverage info programmatically “off-road” code coverage experience.