Unit Test in Visual Studio

Source: Internet
Author: User
Original article: unit test in Visual Studio

Note: This article is original and can be reproduced at will, but please indicate the source. If you do not want to leave it blank, you are strongly opposed to modifying the original source.


Test-driven development is a core practice and technology in Agile development and a design methodology. The principle of TDD is to write unit test case code before developing function code, and test code to determine the product code to be written. Unit testing is the most basic test step. Located at the bottom of the V model of the entire product development process.
Generally, after ra & PSD is completed in various development processes, The UAT solution can be completed without the underlying foundation. The following steps are the same. As an important step for the underlying layer and coding, ut is the foundation that every coder must master.
RA (Requirement Analysis )&
PSD (productive specification design) UAT (user acceptance testing)
\/
\/
\/
FSD (functional specification design) Sit (System Integration Testing)
\/
\/
\/
Construction (coding) Ut (Unit tesing)
\/
\/
\/
\/
\/
\/
\/
When writing a small amount of code, you can often use the console application, web form, and so on to enter the test code for testing. Once the amount of code and the logical relationship become complex, this test will be difficult to expand and will not be maintainability. In addition, a lot of functional Code cannot be fully implemented without the entire project. For example, if you have a class library in a web project and write some model-layer methods to verify the correctness of the methods, you may have to reference a large number of models and interfaces in the project to test the method, and library functions. After compilation is successful, you find that you must log on to call this method or your directory does not have access permissions, therefore, you have to modify the authorization configuration or add logon methods. As a result, your test involves almost all aspects of the project, and the test scheme is not highly reusable.
The following uses vs2012 as an example to illustrate how to perform unit tests in Visual Studio.
1. Right-click solution to bring up the context menu ). Select Add-new project. In the template, select Visual C #-test-unit test project.

2. Get the template.






3. Add the code you want to test in the test method (testmethod1 is the default testmethod1 here, which is generally changed to the method name + test to be tested. However, before that, you must first reference the project to be tested and the class library to be used. In this example, I reference Microsoft's mvc4.0 framework and the Controller in the project to be tested. Here I test the productcategories () method of the controller. This method returns an actionresult. There are three lines in the test code. The first line is used to instantiate the controller, and the second line calls the Controller method. If only the two rows are passed, the Code has no Syntax problems and can work with other parts. Row 3 uses an assertion to check whether the result returned by the method is null. Here I assert that it is not empty. If it is null, the test will fail and an error will be reported.
 
4. After writing the test code, you can click test-run-alltests in the top menu to perform the test.
 
After the test is completed. The result list is displayed below. The red color indicates the testcase that fails. To debug it, right-click the red testcase and select debug selected tests. After modification, you can also right-click the testcase you want to re-test and select Run selected tests.

Unit Test in Visual Studio

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.