swirl
Home Software Blog Wallpapers Webtools
Code Coverage for NUnit
Saturday 31, July 2021   |   Post link

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
Details of code coverage is covered quite well in Wikipedia.

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:

  1. 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.
  2. Open Cover
    https://github.com/OpenCover/opencover
  3. 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\\.nuget\packages\reportgenerator\4.8.12
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.

Report



Comments

Posts By Year

2021 (5)
2020 (12)
2019 (6)
2018 (8)
2017 (11)
2016 (6)
2015 (18)
2014 (2)
2013 (4)
2012 (2)

Posts By Category

.NET (4)
.NET Core (2)
ASP.NET MVC (4)
AWS (3)
AWS API Gateway (1)
Android (1)
Architecture (1)
Azure (2)
Book review (3)
Business (1)
C# (3)
C++ (2)
CloudHSM (1)
Containers (3)
Corporate culture (1)
Database (3)
Database migration (1)
Desktop (1)
DotNet (2)
DotNet Core (2)
Entity Framework (3)
Git (2)
IIS (1)
JDBC (1)
Java (6)
Learning (1)
Life (6)
Linux (1)
Lucene (1)
Multi-threading (1)
OData (1)
Office (1)
PHP (1)
PowerShell (2)
Programming (24)
Rants (5)
SQL (2)
SQL Server (1)
Security (1)
Software Engineering (1)
Software development (1)
Solr (1)
Sql Server (2)
Storage (1)
T-SQL (1)
TDD (1)
TSQL (5)
Tablet (1)
Technology (1)
Test Driven (1)
Unit Testing (1)
Unit Tests (1)
Utilities (2)
VC++ (1)
VMWare (1)
VSCode (1)
Visual Studio (2)
Wallpapers (1)
Web API (2)
Win32 (1)
Windows (9)
XML (2)

Posts By Tags

.NET(6) API Gateway(1) ASP.NET(4) AWS(1) Adults(1) Advertising(1) Android(1) Anti-forgery(1) Authentication(2) Azure(2) Backup(1) Beliefs(1) BlockingQueue(1) Book review(2) Books(1) Busy(1) C#(4) C++(3) CLR(1) CORS(1) CSRF(1) CTE(1) Callbacks(1) Checkbox(1) CloudHSM(1) Cmdlet(1) Commons(1) Company culture(1) Complexity(1) Consumer(1) Consumerism(1) Containers(3) Core(2) Custom(2) DPI(1) Data-time(1) Database(4) Debugging(1) Delegates(1) Developer(2) Dockers(2) DotNetCore(3) EF 1.0(1) Encrypted(1) Entity framework(1) Events(1) File copy(1) File history(1) Font(1) Git(2) GradleApache(1) HierarchyID(1) IIS(1) Installing(1) Intelli J(1) JDBC(1) JSON(1) JUnit(1) JWT(1) Java(1) JavaScript(1) LinkedIn(1) Linux(1) Localization(1) Log4J(1) Lucene(1) MVC(4) Management(2) Migration history(1) Mobile Apps(1) Modern Life(1) Money(1) NGINX(1) NTFS(1) NUnit(2) OData(1) OPENXML(1) Objects(1) Office(1) OpenCover(1) Organization(1) PHP(1) Paths(1) PowerShell(2) Producer(1) Programming(1) Python(1) Quality(1) REDIS(2) Runtimes(1) S3-Select(1) SD card(1) SQL(2) SQL Code-first Migration(1) SSH(1) Sattelite assemblies(1) School(1) Secrets Manager(1) Self reliance(1) Shell(1) Solr(1) Sony VAIO(1) Spirituality(1) Sql Express(1) System Image(1) TDD(1) TSQL(3) Table variables(1) Tables(1) Tablet(1) Url rewrite(1) VMWare(1) VSCode(1) Validation(2) Wallpaper(1) Wallpapers(1) Web Development(4) Windows(2) Windows 10(2) Windows 2016(2) Windows 8.1(1) Work culture(1) XML(1) Yii(1)