This article focuses on some tips for the visual Studio (2012+) Unit Testing Framework:
- Statistics of test coverage
- Test grouping
- Test Setup/teardown
- Test debugging
- 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