Monday, September 19, 2011

Convert MSTest code covarage results in to XML And view through Jenkins

If you are using Jenkins as your CI for a Net project it is not easy to publish code coverage results.In Jenkins without tools such as NCover wich is costly. Instead of this you can do this your own way.
First you need to convert MSTest results in to XML and then to HTML using xstl to publish it as an HTML report in Jenkins.
Step 1

GO to your test local.settings file in visual studio and set it to display the code coverage results an add your required targeted dll's. This is same as configuring visual studio to show the test results and code coverage.
Make sure you have added the test settings file to your version controlling system then it will be in the working space of the jenkins.
Then set path to the MSTest.exe
it may be in your \Microsoft Visual Studio 10.0\Common7\IDE folder


Step 2
Add a windows batch command to run MSTest (after running ms build) and to generate test results file (“reults.trx”) and Coverage report (“data.coverage”)

del results.trx
mstest /testcontainer:Example\TestProject1\bin\debug\TestProject1.dll /resultsfile:results.trx /testsettings:Example\local.testsettings


This will generate a code coverage result in binary format (data.coverage)

Step 3
write a console app to convert binary data.coverage file in to a XML and then it to HTML by xslt and run it after adding windows batch command in jenkins. Here is the example code for console app. Make sure you have add reference to Microsoft.VisualStudio.Coverage.Analysis.dll which you can find in the \Microsoft Visual Studio 10.0\Common7\IDE\PrivateAssemblies folder. And copy Microsoft.VisualStudio.Coverage.Symbols.dll to your bin directory which is also in same folder.



And here is the code of the style.xslt file


13 comments:

  1. Hello!
    Could you please share javascript file script1.js. I cannot use generated report without it.

    ReplyDelete
    Replies
    1. Add JQuery Reference in 1st Line of head tag and then put this Javascript function below inside script tag

      function toggleDetail(control)
      {
      var ctrlId = $(control).attr('Id');
      $("tr[id='"+ctrlId +"']").toggle();
      }

      Delete
    2. It works for me now. Thank you very much.

      Delete
    3. Ah... First of all I didn't see your fist comment about script1.js and thank @Arpit , I think your way is better rather than using plain JavaScript. Thanks all for helping me for to have a valuable article. Will update it...

      Delete
    4. Thanks Jayantha for your great effort.
      We have also changed its styling...
      As it is not allowing to paste html code I have pasted it at
      http://codesharpe.blogspot.in/

      Jayantha, I have a simple query as can we also link source file on function click and show which line is executed as it is possible in ncover with same xml. And can we have one more column for total lines of code.

      Thanks
      Arpit Nagar

      Delete
  2. i getting the html report but when i am expanding the nodes using + nothing is happening for me. I am expecting it will open the child nodes..please clarify

    ReplyDelete
    Replies
    1. Please refer the previous comment thread. There was some issues with js files. .
      "Add JQuery Reference in 1st Line of head tag and then put this Javascript function below inside script tag

      function toggleDetail(control)
      {
      var ctrlId = $(control).attr('Id');
      $("tr[id='"+ctrlId +"']").toggle();
      }"

      Delete
  3. Thanks Jayantha..i made the changes...i am still facing the issue...
    my html script tag is like this
    script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"

    but when i change this to below manually..it is working
    script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"
    /script

    but how to get rid of this programatically..I removed < tags in text in order to post..it is not accepting script tags

    ReplyDelete
  4. i managed to get it work Jayantha...thanks you very much..it was very helpful

    ReplyDelete
  5. It's very helpful! Can I translate this post into Japanese on my blog? Or Do you know some site which has already done?

    ReplyDelete
    Replies
    1. @Kazunari Tanaka, yes you can do it .

      Delete
    2. Thanks a lot! please wait until it's done.

      Delete
    3. done it. little bit change your original code. Anyway thank you for your generous.
      http://axtstar.com/blog/?page_id=258

      Delete