.NET Core - Running Tests in Visual Studio



In this chapter, we will discuss how to run tests in Visual Studio. The .NET Core has been designed with testability in mind, so that creating unit tests for your applications is easier than ever before. In this chapter, we will run and execute our test project in Visual Studio.

Let us open the FirstApp solution in Visual Studio.

FirstApp solution

You can see that it has only two projects and you will not be able to see the test project because we haven’t added that project in our solution.

Let us add a folder first and call it test.

Test

Right-click on the test folder.

Test Folder

Select project.json file and click Open.

Project Json File

The following screenshot shows the code in Tests.cs file as output.

Tests

It is the default implementation and it is just testing that True is equal to true. It is the xUnit testing framework and you will see the Fact attribute that annotates and denotes the test method.

using System; 
using Xunit; 
  
namespace Tests { 
   public class Tests { 
      [Fact] 
      public void Test1() { 
         Assert.True(true); 
      } 
   } 
} 

Following is the implementation of project.json file.

{ 
   "version": "1.0.0-*", 
   "buildOptions": { 
      "debugType": "portable" 
   }, 
   "dependencies": { 
      "System.Runtime.Serialization.Primitives": "4.1.1", 
      "xunit": "2.1.0", 
      "dotnet-test-xunit": "1.0.0-rc2-192208-24" 
   }, 
   "testRunner": "xunit", 
   "frameworks": { 
      "netcoreapp1.0": { 
         "dependencies": { 
            "Microsoft.NETCore.App": { 
               "type": "platform", 
               "version": "1.0.1" 
            }
         }, 
         "imports": [ 
            "dotnet5.4", 
            "portable-net451+win8" 
         ] 
      } 
   } 
}

In project.json file, the most important dependency to the testing framework is the xunit, which brings in the Fact attribute. It brings in the testing framework and APIs for testing with xunit.

We also have the dotnet-test-xunit, this is an adopter so that xunit can work with .NET Core, specifically with dotnet test command line utility. Then you will see the testRunner which will run xunit and you can also see the netcoreapp1.0 framework.

You will see the .NETCore.App dependeny below.

To run test in Visual Studio, let us open Test Explorer from the Test → Window → Test Explorer menu option.

Test Explorer

And you can see that Visual Studio automatically detects the test. The name of the test consists of namespace.className.TestMethodName. Let us now click on Run All button in Test Explorer.

Run All Button

It will first build the code and the run the test and you will see the total time taken by the test. Let us change the test method so that we can see the output when the test fails.

using System; 
using Xunit; 
  
namespace Tests { 
   public class Tests { 
      [Fact] 
      public void Test1() { 
         Assert.True(false); 
      } 
   } 
} 

Let us execute the test again by clicking on the Run All button link.

Run All

You can now see the test failure.

Advertisements