Create A .NET Core Development Environment Using Visual Studio Code - Part Three

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

This is the third article in my Create a .NET Core Development Environment using Visual Studio Code series. You can read the previous articles in the series from the following links.
In this article, I will be talking about how to run Unit Tests in .NET Core applications using Visual Studio Code. Unit tests are an integral part of software development. I am not going to explain unit testing in detail here as there are many online resources for that. I will be talking about how to include unit tests in .NET Core applications and about a Visual Studio Code extension which can be used for running the unit tests.
 
.NET Core projects support the popular unit testing frameworks in the .NET space - xUnit, NUnit and MSTest. I will be using xUnit in this article. 
 
Let's extend the SimpleCalcualtor project which was made in the previous article to add our unit tests. If you haven't read that article, please read that article to get some context. We had created a Class Library and a Console Application in that project.
 
Let's add the remaining mathematical operation methods to the class library project.  
  1. public static class Operations  
  2. {  
  3.     public static int Add(int num1, int num2) => num1 + num2;  
  4.   
  5.     public static int Subtract(int num1, int num2) => num1 - num2;  
  6.   
  7.     public static int Multiply(int num1, int num2) => num1 * num2;  
  8.   
  9.     public static int Divide(int num1, int num2) => num1 / num2;  
  10. }  

Now, we need to add a Unit Test project to the solution.

We can use either the .NET CLI or the Solution Explorer extension (which I have mentioned in the previous article) 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 context menu. From the project templates select xUnit Test Project and give the name MathOperationTests. After the tests project is created, add reference of MathOperations class library to the tests project.

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

  1. dotnet new xunit -n MathOperationTests  
  2. dotnet add MathOperationTests\MathOperationTests.csproj reference MathOperations\MathOperations.csproj  

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

  1. public class OperationTests  
  2. {  
  3.     [Fact]  
  4.     public void AddTwoNumbers_ReturnsSum()  
  5.     {  
  6.         var num1 = 10;  
  7.         var num2 = 20;  
  8.         var result = Operations.Add(num1, num2);  
  9.         Assert.Equal(30, result);  
  10.     }  
  11.   
  12.     [Fact]  
  13.     public void SubtractTwoNumbers_ReturnsDifference()  
  14.     {  
  15.         var num1 = 20;  
  16.         var num2 = 10;  
  17.         var result = Operations.Subtract(num1, num2);  
  18.         Assert.Equal(10, result);  
  19.     }  
  20.   
  21.     [Fact]  
  22.     public void MultiplyTwoNumbers_ReturnsProduct()  
  23.     {  
  24.         var num1 = 10;  
  25.         var num2 = 20;  
  26.         var result = Operations.Multiply(num1, num2);  
  27.         Assert.Equal(200, result);  
  28.     }  
  29.   
  30.     [Fact]  
  31.     public void DivideTwoNumbers_ReturnsQuotient()  
  32.     {  
  33.         var num1 = 20;  
  34.         var num2 = 10;  
  35.         var result = Operations.Divide(num1, num2);  
  36.         Assert.Equal(2, result);  
  37.     }  
  38. }  
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.
 
Create A .NET Core Development Environment Using Visual Studio Code 
 
As you can see, the output is less informative. It would have been good if we had 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.
 
Create A .NET Core Development Environment Using Visual Studio Code 
 
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 in the top. Click on Run All button and you can see all the tests being executed and its results.
 
Create A .NET Core Development Environment Using Visual Studio Code 
 
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.
  1. public static int Add(int num1, int num2)  
  2. {  
  3.     return num1 - num2; // Bug here  
  4. }  

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

If we navigate to the test method we have written  we can see it is now having 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. This can be seen in the below figure.

Create A .NET Core Development Environment Using Visual Studio Code 
 
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 article, I have explained how to write Unit Tests in .NET Core application. I had 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.