Unit Test Via Visual studio-part3

Source: Internet
Author: User

This article focuses on some tips for the visual Studio (2012+) Unit Testing Framework:

  1. Statistics of test coverage
  2. Test grouping
  3. Test Setup/teardown
  4. Test debugging
  5. Exception test

Statistics for code Coverage

VS can be counted in the following ways:

Select Alltests above (of course you can select several or several sets of case to run code coverage separately) to count the code coverage for all the case below the current solution, and the statistics code coverage in fact vs will re-run all the case again. The end result is as follows:

Can see very clearly each class, each method of code coverage, if the code coverage is not 100%, you can click on the method, you can see that the code is not overwritten, such as:

Test grouping

is to add a testcategory attribute, if the case is more, it is recommended to follow the business or their own logic to classify case, so late code modification, you can run only part of the business. More convenient.

Test Setup/teardown

Setup/teardown seems to be NUnit's property name. VS, it's a slightly different term, mostly because you want to execute the relevant logic before/after each case runs. For example, in Setup you can initialize some of the data, teardown inside to do data cleanup.

Test debugging

debugging is much simpler. Break point. and then Debug.

Exception test

The regular program will throw reasonable exception according to the business. How should this reasonable exception be tested? VS is also implemented through attribute.

Business code:

 Public class Exceptiondemo    {        publicstaticvoid  throwargumentnullexception ()        {              ThrowNew  ArgumentNullException ();        }    }

Test code:

The top two case, the first pass. A second fail.

These tips nunit seem to have all the above (except for the Code coverage). The use of NUnit children's shoes should not be unfamiliar.

Some simple tips.

Unit Test Via Visual studio-part3

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.