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.
- 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, 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.
- dotnet new xunit -n MathOperationTests
- 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.
- 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.
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.
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.
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;
- }
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.
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.