On the xunit of MVC unit test

Source: Internet
Author: User
Tags xunit

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.