For. NET Project Web API for business logic background development [especially as a Web API interface], writing unit test cases can greatly reduce the way code helps and runs. However, using the test framework, relative to the self-contained, I am more recommended to use xunit.net this framework, the reason for the choice is that xunit.net is developed by NUnit developers, extensibility is very good.
Don't say much nonsense, directly on the demo:
Configuration and preparation work
Step one: Create a new unit test solution, and then delete the original test file and create a new class. When these preparations are done, the next step is to add a quote!
Add NuGet Package: Search Xunit--> Install the following two items
1. Installation: Xunit.net |
2. Installation: Xunit.net[runner.visual Studio] |
|
|
Writing test Code
Step Two: Add the test code to the newly created class, reference Using.xunit, and Note that you must add [Fact] After the write test is complete
Public classDeptservicetest {Private ReadOnlyDeptservice _deptservice =NewDeptservice (); [Fact] Public voidgetdeptstest () {varDeptID ="10001"; varDepts =_deptservice.getdepts (DeptID); Assert.equal (false, Depts. Count >0);//compare expectations with true values } }
Step three: Build the solution, bring up the test explorer, and you'll see the corresponding test case
Test Cases |
Run successfully |
Failed to run |
|
|
|
This will be able to know the test run time, the expected value and the real values of comparison, of course, can also be debugged
On the xunit of MVC unit test