Overview
Automated unit testing needs to introduction. None of us put code into production without writing unit tests, right? Right? This blog post discusses how to generate unit test code coverage using NUnit, Open Cover and Report Generator.
What is code coverage?
Its a measure of the amout of code which has been tested via automated unit testing. Code coverage is measured on the basis of multiple criteria like:
- Function coverage
- Statement coverage
- Edge coverage
- Branch coverage
- Condition coverage
Scenario
The sample application is a library which generates the Fibonacci sequence. SampleLib is a class library which is set to use .NET Standard 2.1 as the target framework. It contains a single class for generating the Fibonacci sequence.
The main library is accompanied by a separate library creates solely for housing the automated unit tests. SampleLibTests is a NUnit Test type project which targets .NET Core 3.1. It contains 3 test methods to test the correctness of the Fibonacci generator.
The code is available on GitHub.
Software required
What we want to do now is generate code coverage reports for our unit tests. We need the following software to do this:
-
Nunit console runner (NUnit.Console-3.12.0.msi).
https://github.com/nunit/nunit-console/releases/tag/v3.12
Download the NUnit.Console-3.12.0.zip file. -
Open Cover
https://github.com/OpenCover/opencover -
Report Generator
https://www.nuget.org/packages/ReportGenerator/4.8.12
Setting up NUnit console runner
Since our test-project is a .NET core library, we need to use the console runner present in the netcoreapp3.1 folder. Note, I have unzipped NUnit to c:\Utilities. The following command runs all the unit tests found in the specified assembly. Note, you must have already built the solution.
C:\Utilities\NUnit.Console-3.12.0\bin\netcoreapp3.1>nunit3-console D:\Temp\Sample-UnitTest\Sample\SampleLibTests\bin\Debug\netcoreapp3.1\SampleLibTests.dll
If everything goes fine, the result of the tests will be stored in a file called TestResult.xml. This is just to check if NUnit console runner is setup properly.
Instaling OpenCover
OpenCover is the tool which measures the code coverage of your unit test cases. Builds can be downloaded from https://github.com/OpenCover/opencover/releases/tag/4.7.1221. All you need to do is download and extract the zip file. I have extracted OpenCover to c:\Utilities.Calculating code coverage
To make it easy to locate the binaries of OpenCover and NUnit, its a good idea to add these folders to the PATH variable.
path=%path%;C:\Utilities\NUnit.Console-3.12.0\bin\netcoreapp3.1 path=%path%;C:\Utilities\opencover.4.7.1221
Running the following command will generate the code coverage metric:
OpenCover.Console.exe "-targetargs:D:\Temp\Sample-UnitTest\Sample\SampleLibTests\bin\Debug\netcoreapp3.1\SampleLibTests.dll" "-target:nunit3-console.exe" "-output:d:\temp\codecoverage.xml"
You should now have a folder named "CodeCoverage" in the same folder where you executed the above command. OpenCover generates a file named "results.xml" which contains the code coverage information. This files contains a lot of information but isn't very readable.
If you notice, it also contains information about modules we aren't necessarily interested in e.g.: nunit-console. We can pass a filter to OpenCover to remove these unwanted entries:
OpenCover.Console.exe "-targetargs:D:\Temp\Sample-UnitTest\Sample\SampleLibTests\bin\Debug\netcoreapp3.1\SampleLibTests.dll" "-target:nunit3-console.exe" "-output:d:\temp\codecoverage.xml" -filter:"+[SampleLib]* -[nunit3-console]*"
Now, to make this file readable for us humans we'll use the Report Generator.
Generating the code coverage report
The first thing to do is install Report Generator. Since this comes in the form of a nuget package, run the nuget command as shown in the website from within Visual Studio:
Install-Package ReportGenerator -Version 4.8.12
The package gets installed at C:\Users\
Let's add the report generator's folder to the PATH variable and run the tool:
path=%path%;C:\Users\Siddharth\.nuget\packages\reportgenerator\4.8.12\tools\net5.0\ ReportGenerator "-reports:d:\temp\codecoverage.xml" "-targetdir:d:\temp\reports"
Report Generator will create the reports in d:\temp\reports folder. Open the index.html file in the reports folder to see the generated report.