How to calculate code coverage of IIS hosted WCF services?

Do you have a same question?

But don’t worry. I had same and now I have answer to this question.

Before we start…

For those who are unaware of what code coverage is  : http://en.wikipedia.org/wiki/Code_coverage

Lets start…

Calculating code coverage by running unit tests on your application is no longer pain.

As unit tests directly interacts with application code dlls and if such unit tests run using VsTest with coverage option enabled, you can get coverage of your code for every run of your unit test suite.(Provided all referenced dll’s pdbs are present)

See more about VsTest here.

So now, how should I get code coverage of my WCF service hosted in IIS?

Here we go…

  • Build and deploy your service in IIS.(Should be in ‘debug’ mode strictly)
  • Stop the IIS. (Recommended- because sometimes it skips some dll from analyzing)
  • Instrument dlls.

For example : C:\Program Files (x86)\Microsoft Visual Studio 11.0\TeamTools\Performance Tools\x64>vsinstr.exe /coverage “YOUR_DLL_NAME”

Repeat above command in order to instrument all dlls.

  • Start the IIS.
  • Now, start the coverage capturing service as follows.

C:\Program Files (x86)\Microsoft Visual Studio 11.0\Team Tools\Performance Tools\x64>vsperfcmd /start:coverage /output:coverageOutputPath /cs /user:appPoolIdentity

where,
coverageOutputPath = output path of resulting coverage file. (example : “C:\Test.coverage”)
appPoolIdentity = Application pool user under which hosted service runs.

  • You can check status as follows

   C:\Program Files (x86)\Microsoft Visual Studio 11.0\Team Tools\Performance Tools\x64>vsperfcmd /status

  • Run your service/functional test suite.
  • Stop IIS. (Recommended- because sometimes it skips some dll from analyzing)
  • Shutdown code coverage capturing service as

  C:\Program Files (x86)\Microsoft Visual Studio 11.0\Team Tools\Performance Tools\x64>vsperfcmd /shutdown

  • Analyze coverage file specified at above path.(Open the .coverage file in Visual studio)

Whats Nxt???

Want to automate this with Jenkins for continuous integration?
Visit : https://dpatil1410.wordpress.com/2015/04/03/jenkins-integration-to-calculate-code-coverage-of-wcf-services-hosted-in-iis/

Happy coding…

10 thoughts on “How to calculate code coverage of IIS hosted WCF services?

  1. Hi
    Good work.. if i want to do the code coverage for my wcf service in server.
    in my windows server we haven’t installed the visual studio only dotnet framework 4.5 is installed.
    could you please assist me if i want to do the profiling in server what are the tool need to install and provide me the download url also

    Thanks in advance

    Like

    1. Hello Murugan,

      Only .Net framework won’t do. In order to calculate code coverage, you require some special tools which are available only after VS installation. You can find them under ‘C:\Program Files (x86)\Microsoft Visual Studio 11.0\Team Tools’ folder.
      I will suggest you should have VS installed on your ‘Build Server’ to carry out such a tasks which will be completely different from your Dev, QA and Stage environments.

      For remote profiling of your .Net applications, dotTrace is the best. It’s paid, but you can take maximum use of it’s free trial to dig dip inside your application to find loop holes.
      http://www.jetbrains.com/profiler/

      Like

  2. Thanks for the update ..
    we will validate our testers code coverage in QA environment.
    i have seen visual studio standalone profilers 2010.
    here it is mentioned without visualstudio we can do the codecoverage .
    i didnt checked if you done earlier please update.

    Like

  3. Hi Murugan,

    Sorry for the delay.
    I haven’t yet tried visual studio standalone profiler.
    After getting some info, I think this will be enough for code coverage calculation as it install all performance related tools without having to install VS on server.
    Give a try. Should work fine.
    And thanks for suggesting this.

    Like

  4. Thanks for the update. one more doubt is if i have done the instr in build machine and copy those dll in my Testing server and attach . it will work or not?

    Like

Leave a comment