Create a .NET Core Development Environment using Visual Studio Code - Part 3

Create a .NET Core Development Environment using Visual Studio Code - Part 3

This is the third article in my Create a .NET Core Development Environment using Visual Studio Code series. You can read my previous articles from the following links.

In this article, I will be talking about how to run Unit Tests in a .NET Core application using Visual Studio Code. Unit testing is an integral part of software development. I am not going to explain unit testing in detail here as there are many resources online for that. I will be talking about how to include unit tests in the .NET Core application and about a Visual Studio Code extension that can be used for running the unit tests.

.NET Core projects support the popular unit testing frameworks in the .NET space - xUnit.NET, NUnit and MSTest. I will be using xUnit.NET in this article.

Let’s extend the SimpleCalculator project which we built in the previous article by adding unit tests. We created a Class Library and a Console Application for that project.

Let’s add the remaining mathematical operations to the class library.

public static class Operations
{
    public static int Add(int num1, int num2) => num1 + num2;

    public static int Subtract(int num1, int num2) => num1 - num2;

    public static int Multiply(int num1, int num2) => num1 * num2;

    public static int Divide(int num1, int num2) => num1 / num2;
}

Now add a unit test project to the solution.

We can use either the .NET CLI or the Solution Explorer extension for adding the unit test project. For adding the project through the Solution Explorer extension, right-click on the solution and select Add new Project. From the project templates select xUnit Test Project and give the name MathOperationTests. After the tests project is created, add a reference of the MathOperations class library to the tests project.

If you are using the .NET CLI you need to run the following commands.

  • dotnet new xunit -n MathOperationTests

  • dotnet add MathOperationTests\MathOperationTests.csproj reference MathOperations\MathOperations.csproj

Rename the UnitTest1.cs class file in the newly added test project to OperationTests.cs. Change the class name in the code as well. Now we shall add some tests for the class library methods.

using xUnit;
using System;
using MathOperations;

public class OperationTests
{
    [Fact]
    public void AddTwoNumbers_ReturnsSum()
    {
        var num1 = 10;
        var num2 = 20;
        var result = Operations.Add(num1, num2);
        Assert.Equal(30, result);
    }

    [Fact]
    public void SubtractTwoNumbers_ReturnsDifference()
    {
        var num1 = 20;
        var num2 = 10;
        var result = Operations.Subtract(num1, num2);
        Assert.Equal(10, result);
    }

    [Fact]
    public void MultiplyTwoNumbers_ReturnsProduct()
    {
        var num1 = 10;
        var num2 = 20;
        var result = Operations.Multiply(num1, num2);
        Assert.Equal(200, result);
    }

    [Fact]
    public void DivideTwoNumbers_ReturnsQuotient()
    {
        var num1 = 20;
        var num2 = 10;
        var result = Operations.Divide(num1, num2);
        Assert.Equal(2, result);
    }
}

Now we need to run the tests which we created. Let’s make use of the .NET CLI for this. Open the terminal. Navigate to the MathOperationTests directory. Enter the dotnet test command. We shall be getting the following output.

dotnet-test-cli-output.png

As you can see, the output is less informative of the tests. It would have been good if we have something similar to Test Explorer in Visual Studio to execute our unit tests and view the results.

The good news is that there is a Visual Studio code extension for this. The extension is named .NET Core Test Explorer. Install this extension in Visual Studio Code.

dotnet-core-text-explorer.png

After the extension is installed, you can see a beaker icon in the side activity bar. Click on that icon and you will see the sidebar panel listing the unit tests discovered in the project. The tests will be displayed in a tree view grouped by namespaces and classes. You can also see a Run button against each test and a Run All button at the top. Click on the Run All button and you can see all the tests being executed and their results.

vs-code-test-explorer-output.png

We can see that all the tests are passed and are marked with a green tick in the test explorer pane.

Now let’s make a test fail. I will change the logic of the Add method to make the test fail.

public static int Add(int num1, int num2)
{
    return num1 - num2; // Bug here
}

Now run the tests again. We can see that our test for Add method failed and is marked with the red symbol in the test explorer pane.

If we navigate to the test method we have written, it now has a red squiggly underline in the Assert method. If we hover over that squiggly line an info box will be shown displaying the actual and expected values of the test. The same information is displayed in the Problems tab in the bottom panel (the panel where your terminal lives) of the VS Code.

vs-code-test-explorer-output-failed-test.png

Fix the bug and run the tests again so that all tests are passed and we can see the green ticks again.

Summary

In this post, I have explained how to write Unit Tests in a .NET Core application. I explained the .NET Core Test Explorer extension for Visual Studio code which adds the test explorer functionality to our awesome text editor. If you want to know more about this extension you can visit the GitHub repository for this project here.