ASP.NET Unit testing using xUnit in core


Since the birth of the first version of MVC, the function of unit testing has been introduced as an important selling point. When comparing MVC with webform, unit testing is always playing cards and crushing webform to nothing.

There is no need to say much about the importance of unit testing. For projects with unit tests, it is like buying insurance for developers. Of course, the quality of this insurance depends on the quality of unit tests. The unit tests that have been mocked all the way look beautiful, but they can’t cover anything. At present, there are more than 20000 unit test cases in an old project in our work. Many of them are made by heart and are really implemented in the business logic. Developers can feel free to modify the code. Of course, everything must be carried out according to the principles of TDD, so I won’t talk about it in detail.

Let’s move on to the main topic. Unit testing is an important basic function in. Net core. The functions provided by related APIs and components are no different from those of previous versions. The main reason is that the. Net core needs to be supported. In other words, the code under the. Net framework platform has to be regenerated to support. Net core. For example, xUnit in this article has already provided a support version for. Net core. Let’s see how to use the ASP.NET XUnit is used in the core for unit testing.

xUnit.Net introduce Its creators are Jim Newkirk and Brad Wilson, a new framework summarized from the experience of using NUnit and other unit testing frameworks, It has the following characteristics:

  1. Generate an object instance for each test method
  2. Cancel [setup] and [teardown]
  3. [expectedexception] canceled
  4. Aspect like functionality
  5. Reduces the number of custom attributes
  6. Using generics
  7. anonymous delegate
  8. Extensible assertion
  9. Extensible testing method
  10. Extensible test class Its official website is: Here is Running interface of

Note that the There are four exe files supporting GUI operation in the compressed package

  1. xunit.gui.clr4 . exe: used to run under x64 and. Net4.0 。
  2. xunit.gui.clr4 . x86.exe: used to run under x86 and. Net4.0 。
  3. Xunit.gui.exe : for running on x64 and below. Net4.0 。
  4. xunit.gui . x86.exe: used to run on X86 and below. Net4.0 。

Prepare project code:

1. Create a blank ASP.NET The core web project, named usexunit.

2. Then modify project.json Add a dependency on the MVC component in the last line of dependencies

   “Microsoft.AspNetCore.Mvc”: “1.0.1”

3. Modification Startup.cs file

Add in the configureservices method services.AddMvc ();

Add in the configure method app.UseMvcWithDefaultRoute ();

3. Then create a new controllers directory in the project root directory and add a homecontroller. The code is as follows

public class HomeController : Controller
    public IActionResult Index()
      return Content("Hello test");

4. At this point, the project should be able to run. After running, you can see the output of Hello test, indicating that a simple project has been provided.

Add unit test project

Next, add a unit test project to facilitate the management of a separate test project.

1. Right click on the solution file, select new solution folder, and then name it test

2. Create a new. Net core project in the test directory. The selected project type is class library (. Net core), and the project name is UseXunit.Tests

3. Then modify the project.json The contents are as follows

 "version": "1.0.0-*",
 "testRunner": "xunit",
 "dependencies": {
  "Microsoft.NETCore.App": {
   "type": "platform",
   "version": "1.0.0"
  "xunit": "2.1.0",
  "dotnet-test-xunit": "2.2.0-preview2-build1029",
  "UseXunit": "1.0.0",

  "Microsoft.AspNetCore.Mvc": "1.0.1"
 "frameworks": {
  "netcoreapp1.0": {
   "imports": [ "dotnet5.6", "portable-net45+win8" ]

The changes here almost rewrite the default configuration generated by the system. The default configuration of the system can hardly run and needs to be reset.

Dependencies introduces xUnit and dotnet test xUnit support package. Of course, it also adds usexunit to the test project.

Another key configuration is to specify the testrunner as xUnit

After saving, the related packages will be automatically restored.

4. Now you can start creating testcase

Create a new homecontrollertest class with the following contents

public class HomeControllerTest
    public void ShouldGetIndexResult()
      var homeController = new HomeController();
      var contentResult = homeController.Index() as ContentResult;
      Assert.Equal("Hello test", contentResult.Content);

Here you create a basic test case, and then use the familiar shortcut key Ctrl + U + R (reshaper) to run the test happily

For complete code, please refer to

The above is the whole content of this article, I hope to help you in your study, and I hope you can support developeppaer more.