Click here to Skip to main content
13,860,694 members
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

6.6K views
3 bookmarked
Posted 20 Sep 2015
Licenced MIT

Measuring code coverage of ASP.NET 5 applications using Opencover

, 20 Sep 2015
Rate this:
Please Sign up or sign in to vote.
This post is about getting code coverage of ASP.NET 5 using Opencover. In computer science, code coverage is a measure used to describe the degree to which the source code of a program is tested by a particular test suite.

This post is about getting code coverage of ASP.NET 5 using Opencover. In computer science, code coverage is a measure used to describe the degree to which the source code of a program is tested by a particular test suite. A program with high code coverage has been more thoroughly tested and has a lower chance of containing software bugs than a program with low code coverage. Opencover is a code coverage tool for .NET 2 and above, support for 32 and 64 processes with both branch and sequence points; roots proudly based in PartCover – https://github.com/OpenCover/opencover

Here is the source file, since the post is about measuring code coverage, it is a simple math libaray with Add and Divide methods.

using System;

namespace DotNetThoughts.Net
{
    public class Math
    {
        public int Add(int number1, int number2)
        {
            return number1 + number2;
        }

        public int Divide(int number1, int number2)
        {
            if (number2 <= 0)
            {
                throw new DivideByZeroException
                    ("Number2 is less or equal to zero");
            }

            return number1 / number2;
        }
    }
}

And here is the project.json file for the libaray.

{
    "version": "1.0.0-*",
    "description": "",
    "authors": [
        ""
    ],
    "tags": [
        ""
    ],
    "projectUrl": "",
    "licenseUrl": "",
    "frameworks": {
        "dnx451": {},
        "dnxcore50": {
            "dependencies": {
                "System.Runtime": "4.0.21-beta-23225"
            }
        }
    }
}

Once libaray created, you can execute dnu build command to verify everything works fine. Once it is done, you can write the unit test file. Here is the test file.

using Xunit;

namespace DotNetThoughts.Net.Tests
{
    public class MathTest
    {
        [Fact]
        public void AddTest()
        {
            Math m = new Math();
            Assert.Equal(4, m.Add(2, 2));
        }
        
        [Fact]
        public void DivideTest()
        {
            Math m = new Math();
            Assert.Equal(4, m.Divide(8,2));
        }
    }
}

And the project.json file for unit test project.

{
    "dependencies": {
        "Math": "",
        "xunit": "2.1.0-*",
        "xunit.runner.dnx": "2.1.0-*"
    },
    "commands": {
        "test": "xunit.runner.dnx"
    },
    "frameworks": {
        "dnx451": {},
        "dnxcore50": {}
    }
}

You require global.json file in the root directory, which used to resolve the dependencies.

Project structure

Project structure

{
    "projects": [ "src", "test" ]
}

Execute dnu restore and dnx test command to verify your unit tests. To get the code coverage, you require Open cover, which you can install via Nuget or can download and install from here – https://github.com/OpenCover/opencover.

Now you can execute the test using Open cover, which can be using following command.(Line breaks added for readability)

C:\Users\[USERNAME]\AppData\Local\Apps\OpenCover\OpenCover.Console.exe
-target:”C:\Users\[USERNAME]\.dnx\runtimes\dnx-clr-win-x64.1.0.0-beta8-15618\bin\dnx.exe”
-targetargs:”–lib C:\ASPNET\UnitTestDemo\src\Math\bin\Debug\dnx451 test”
-output:coverage.xml
-register:user
-filter:”+[*]* -[xunit*]*”

Make sure you’re using proper dnx executable (You can get the exact name and location, in C:\Users\[USERNAME]\.dnx\runtimes), the lib location should be point to the dnu build output location. Once it completed successfully, you will see some output in the console window.

Open cover - Execution

Open cover – Execution

It also generates coverage.xml file, which you can be used to generate the output report using report generator tool, which can be downloaded from – http://danielpalme.github.io/ReportGenerator/

To generate report, you need to execute

ReportGenerator.exe “-reports:C:\ASPNET\UnitTestDemo\test\UnitTestDemoTests\coverage.xml” “-targetdir:C:\ASPNET\Report”

Once report generated, open the index.htm file, which is available in the report directory. You can find summary view like this.

Code coverage report - Summary view

Code coverage report – Summary view

Clicking on the link name will open the detailed view.

Code coverage - Detailed view

Code coverage – Detailed view

Hope it helps. Happy Programming :)

License

This article, along with any associated source code and files, is licensed under The MIT License

Share

About the Author

Anuraj Parameswaran
Technical Lead
India India
Working as Tech. Lead

My blog : dotnetthoughts.net.
You can follow me in twitter : @anuraj

You may also be interested in...

Pro
Pro

Comments and Discussions

 
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web03 | 2.8.190214.1 | Last Updated 20 Sep 2015
Article Copyright 2015 by Anuraj Parameswaran
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid