Code coverage in VS 2017 Community

Recently we’ve been searching for the way, how to enable code coverage in the VS Community edition.

After a bit of searching it all ended up by the list of steps we need to do for each of our solution.

When you want to enable code coverage for you project in VS, this is what needs to be done:

  • Install OpenCover from Nuget
  • Install ReportGenerator from Nuget
  • Install NUnit from Nuget (e.g. if you were using NUnit TestAdapter up to now)
  • Play and fiddle a bit and create script running all those things together

We created two PS scripts, which will ease-up the process. Those are:

  • EnableCoverage.ps1 – enable coverage for the project specified as a parameter
  • RunCoverage.ps1 – run code coverage, store results and show them

Scripts are available at Bitbucket.

EnableCoverage

Example of use:

EnableCoverage.ps1 -Project c:\Users\pavol\source\repos\Planner\Staffingboard.UT\Staffingboard.UT.csproj

Nuget.exe must be downloaded and installed in the system, also it must be accessible via PATH env variable.

Script will install all necessary packages to the project’s solution.

RunCoverage

Example of use:

RunCoverage.ps1 -SolutionDirectory "c:\Users\pavol\source\repos\Planner\" -ProjectName "Staffingboard" -TestProjectName "Staffingboard.UT"

The script will

  • create CodeCoverage directory within $TestProjectName project
  • run all the test, which are located in test assembly
  • get the coverage for the source located in $ProjectName
  • display the results in the browser