Codementor Events

Generating code coverage for free with AltCover

Published Jul 03, 2023
Generating code coverage for free with AltCover

Cross-posted from my personal blog.

Code coverage is an important metric that can help us get an idea of our code health. There are many tools available for us to generate those metric, but most of them are paid. Fortunately, AltCover got us covered (ha ha) and provides it for free.

AltCover is an instrumenting coverage tool for .NET (framework 2.0+ and core) and Mono. It is pretty simple to use it and can be customized quite a lot. In this tutorial, we will go with the simplest approach.

The solution that we will be using here is pretty simple. It is composed of 2 projects, one with the source code, and one with the unit tests.

Let’s take a look at the source code for each class. By the way, if you are interested in learning about naming, feel free to take a look at these articles about naming methodsand tests.

Shirt.cs

public class Shirt
{
    public bool IsDirty { get; set; }

    public bool IsDry { get; set; }
}

WashingMachine.cs

public class WashingMachine
{
    public int NumberOfUsesSinceLastFix { get; set; }

    public void Wash(Shirt shirt)
    {
        if (NumberOfUsesSinceLastFix == 3)
            throw new Exception("Whoops I seem to be broken again");

        if (!shirt.IsDirty)
            throw new Exception("Why are you even trying to wash this?");

        shirt.IsDirty = false;
        shirt.IsDry = false;
        NumberOfUsesSinceLastFix++;
    }

    public void Fix()
    {
        NumberOfUsesSinceLastFix = 0;
    }
}

WashingMachineTests.cs

[TestClass]
public class WashingMachineTests
{
    [TestMethod]
    public void ShirtIsClean_ShouldThrowException()
    {
        //Arrange
        var washingMachine = new WashingMachine();
        var shirt = new Shirt();

        //Act & Assert
        Assert.ThrowsException<Exception>(() => washingMachine.Wash(shirt));
    }

    [TestMethod]
    public void ShirtIsDirty_ShouldSetItToClean()
    {
        //Arrange
        var washingMachine = new WashingMachine();
        var shirt = new Shirt()
        {
            IsDirty = true
        };

        //Act
        washingMachine.Wash(shirt);

        //Assert
        Assert.IsFalse(shirt.IsDirty);
    }
}

Now that have our project set up, it is time to finally use AltCover. First, we need to install the Nuget package into our test project.

dotnet add UnitTests package AltCover

And then, all we have to do is run our UnitTests slightly differently and we will have our coverage file ready. You can run the command directly from your UnitTest folder, or anywhere else, but then you will have to provide the full path for your .csproj.

dotnet test /p:AltCover=true

dotnet test /p:AltCover=true "C:\path\to\UnitTests.csproj"

And that’s it! We now have a file called “coverage.xml” in our folder, ready to be analyzed. Are you not sure how to do it? Stay tuned, because we will be covering that soon enough.

Discover and read more posts from Henrique Dalcin Dalmas
get started
post commentsBe the first to share your opinion
Show more replies