Unit test functionality since the first version of MVC was born, is as an important selling point to introduce, usually in the MVC and WebForm comparison, unit test is must kill the cards, the WebForm crushed to nothing.
The importance of unit testing needless to say, there are unit tests to do backstop project, like to buy a developer insurance, of course, the quality of this insurance depends on the quality of unit testing, those who mock the Unit test, looks beautiful, but nothing cover. At present, an old project in the work, there are more than 20,000 unit test cases, many of them are intentions, the real implementation of the business logic, developers can rest assured to modify the code, of course, all must follow the principle of TDD, the specific not detailed said.
Next go to the point, Unit test this important basic function, in. NET core is certainly not few, the related API and the component provides the function and the previous version is not big difference, mainly lies in needs to the. NET core support, plainly is in the. Net The code under the framework platform has to be regenerated to support. NET Core. For example, Xunit,xunit has provided support versions of. NET core for this article, let's look at how to use Xunit for unit testing in ASP.
Prepare the Project code:
1. Create a blank ASP. NET Core Web project named Usexunit.
2. Then modify the contents of the Project.json as follows, adding a dependency on the MVC component on the last line of dependencies
" MICROSOFT.ASPNETCORE.MVC": "1.0.1 "
3. Modify the Startup.cs file
Join services in the Configureservices method . Addmvc ();
Add the app in the Configure method . Usemvcwithdefaultroute ();
3. Then create a new controllers directory in the project root and add a homecontroller with the following code
Public class Homecontroller:controller { public iactionresult Index () { return Content (" Hello test"); } }
4. Here the project should be able to run and see the output Hello Test after running, indicating that a simple project is available.
Join Unit Test Project
Next, add a unit test project that is easy to manage with a single test project.
1. Right-click the solution file and select Create a new solution folder, and then name test
2. Create a new. NET core project in the test directory, the project type selected here is Class Library (. NET core), named Project name Usexunit.tests
3. Then 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 rewrite the default configuration of the system, and the default configuration of the system will almost never run up and need to be reset.
Dependencies introduced the Xunit, as well as Dotnet-test-xunit's operational support package, and of course the dependency on the test project (Usexunit).
Another key configuration is the designation of Testrunner as Xunit
After saving, it will automatically restore related packages.
4. You can now start creating testcase
Create a new Homecontrollertest class with the following content
public class Homecontrollertest {[Fact] public void Shouldgetindexresult () {
var HomeController = HomeController (); var Contentresult = Homecontroller.index () as Contentresult; Assert.notnull (Contentresult); Assert.equal ( hello test "
Here you create a basic test case, and then use your familiar shortcut key ctrl+u+r (reshaper) to run the test happily.
Complete code please refer to Https://github.com/shenba2014/AspDotNetCoreMvcExamples/tree/master/UseXunit
Unit testing with Xunit in ASP.