XUnit is used in ASP. NET Core for unit testing. corexunit
The unit test function was introduced as an important selling point when the first version of MVC was born. When comparing MVC with webform, unit testing is the foundation card, and it is useless to crush webform.
The importance of unit testing is needless to say. A unit testing project is just like buying insurance for developers. Of course, the quality of this insurance depends on the quality of unit testing, the Unit Tests of one Mock look beautiful, but nothing can be covered. At present, there are more than 20 thousand unit test cases in an old project at work, many of which are dedicated to implementing the business logic. developers can safely modify the code, of course, everything must follow the TDD principle. I will not go into details here.
Next, let's go to the topic. unit testing is an important basic function. NET Core, of course, is indispensable. The functions provided by related APIs and components are no different from those provided in previous versions.. NET Core support.. NET Framework.. NET Core. For example, the xUnit mentioned in this article, xUnit, has provided support for. NET Core. Next we will look at how to use xUnit in ASP. NET Core for unit testing.
XUnit. Net Introduction
The creators of xUnit.net are Jim Newkirk and Brad Wilson, who have summed up a new framework from their experiences including NUnit and other unit test frameworks. Compared with NUnit, xUnit.net has the following features:
- Generate an object instance for each test method
- [SetUp] and [TearDown] are canceled.
- [ExpectedException] canceled
- Functions similar to Aspect
- Reduced the number of Custom Attributes
- Generic
- Anonymous Delegation
- Extensible assertions
- Scalable testing methods
- Scalable test classes
The official website of xUnit.net is: http://xunit.codeplex.com, below is the run interface of xUnit.net:
Note that the downloaded xUnit.net package contains four exe files that support GUI running:
- Xunit.gui.clr4.exe: Used to run xUnit.net in x64 and. Net4.0.
- Xunit.gui.clr4.x86.exe: Used to run xUnit.net in x86 and. Net4.0.
- Xunit.gui.exe: Used to run xUnit.net in x64 and. Net4.0 or earlier versions.
- Xunit.gui.x86.exe: Used to run xUnit.net in x86 and. Net4.0 or earlier versions.
Prepare the project code:
1. Create a blank ASP. NET Core web project named UseXunit.
2. modify the content of project. json as follows, and add the dependency on the MVC component to the last line of dependencies.
"Microsoft. AspNetCore. Mvc": "1.0.1"
3. Modify the Startup. cs File
Add services. AddMvc () to the ConfigureServices method ();
Add app. UseMvcWithDefaultRoute () in the Configure method ();
3. 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. Here the project should be able to run. After running, the output Hello test is displayed, indicating that a simple project is ready.
Join unit test project
Next, add a unit test project to facilitate the management of an independent test project.
1. Right-click the solution file, select create a solution folder, and name it test
2. Create a. NET Core project under the test directory. The selected project type is Class Library (. NET Core) and the project name is UseXunit. Tests.
3. Modify the project. json content of the Tests project 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 overwrite the default configuration generated by the system. The default configuration cannot run and needs to be reset.
Dependencies introduces xunit and dotnet-test-xunit to support running package. Of course, it also includes UseXunit ).
Another key configuration is to specify testrunner as xunit.
After the packages are saved, the related packages are automatically retained.
4. Now you can start creating TestCase.
Create a HomeControllerTest class with the following content:
public class HomeControllerTest { [Fact] public void ShouldGetIndexResult() { var homeController = new HomeController(); var contentResult = homeController.Index() as ContentResult; Assert.NotNull(contentResult); Assert.Equal("Hello test", contentResult.Content); } }
A basic test case is created here, and then you can run the test happily with the shortcut key Ctrl + U + R (Reshaper) you are familiar.
Complete code see https://github.com/shenba2014/AspDotNetCoreMvcExamples/tree/master/UseXunit
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.