How to check code coverage (using Unit Test cases)
Code coverage and report generation using coverlet (global tool).
Coverlet is a cross-platform code coverage framework for .NET, with support for the line, branch and method coverage. It works with .NET Framework on Windows and .NET Core on all supported platforms.
Steps as below
- Step 1:Create the SetupGlobalTool.bat file in your project folder.
Sample path: "C:\YourProjectFolder\BuildCodeCoverageReport\SetupGlobalTool.bat" - Step 2: Create the RunCodeCoverage.bat file in your project folder.
Sample path: "C:\YourProjectFolder\BuildCodeCoverageReport\RunCodeCoverage.bat" - Step 3 Execute both file from command prompt respectively.
- Note: File content provided in below seactions.
SetupGlobalTool.bat
@ECHO OFF dotnet tool install --global coverlet.console --ignore-failed-sources dotnet tool install -g dotnet-reportgenerator-globaltool --ignore-failed-sources pause
RunCodeCoverage.bat
@ECHO OFF coverlet "..\TestProjectName\bin\Release\netcoreapp2.2\TestProjectName.dll" --target "dotnet" --targetargs "test ..\TestProjectName\TestProjectName.csproj -c Release --no-build --logger:trx" --threshold 0 --format "opencover" -f json -f lcov --output "CodeCoverage" reportgenerator "-reports:CodeCoverage.opencover.xml" "-targetdir:CodeCoverage\Reports" pause
Note: Create the RunCodeCoverage.bat and SetupGlobalTool.bat file in BuildCodeCoverageReport folder.
Commands
- Step 1: Install global tools.
SetupGlobalTool.bat - To verify tools has been installed execute below command
dotnet tool list -g - RunCodeCoverage.bat
Note: If command is not recognized, please check the environment variable PATH.