Unit Testing Using nunit, nmock, and ncover

Source: Internet
Author: User
What is nunit? Nunit is a unit test framework and is specifically written for. net. You can download. http://www.nunit.org in http://www.cnblogs.com/confach/archive/2005/08/14/177817.html for free

1. nunit runtime Diagram

The color of the progress bar reflects the test execution status :· GreenDescription all tests currently passed · YellowIt means some tests are ignored, but it does not fail · RedIndicates that there is a status bar at the bottom of the failure to indicate the following status :· Status. Shows the status of the current test. The status changes Completed. Running the test. The status is Running:< Test-name>
(< Test-name> Is the name of the running test ). · Test CasesDescribes the total number of test cases in the loaded assembly. This is also the number of leaf nodes in the test tree. · Number of tests completed by tests run. · Failures so far, the number of failures in all tests. · time shows the test run time (in seconds) 2. Common attributes of nunit

· TestfixtureattributeThis attribute marks a class containing test cases. A class must be a nunit test case, and the access modifier must be public. · TestattributeThe method for marking a class can be tested. In addition, it must meet the following requirements: 1) access restriction modification must be performed without public2) the method cannot have parameters. 3) the method cannot have return values. · IgnoreattributeMarking a test method will be ignored by nuint. · Setupattribute/teardownattributeWhen running test cases, we often need some common resources, such as creating database links and disabling database links after the operation. Nunit provides the attributelai to support such regular initialization and clear operations. The scope is measured in testing methods. When each test method is executed by nunit, The setupattribute method is executed first, the code of the test method is executed, and the teardownattribute method is executed. · Testfixturesetupattribute/testfixtureteardownattributeSimilar to setupattribute/teardownattribute, but the scope is the entire class marked with testfixtureattribute and will only be executed once. In this way, initialization and cleanup are required for every test method executed, improving code efficiency. · ExpectedexceptionMarking a test method throws an exception. The exception type is the same as that of. net. · CategoryClassify the test cases so that you can choose the categories you need to run the test cases. · ExplicitattributeIgnore a test/testfixture until it is explicitly executed. The reason is that ignoreattribute ignores a test and it is impossible to call it again. At this time, explicitattribute will come in handy.

 

 

 

 

What is nmock? Http://msdn.microsoft.com/msdnmag/issues/04/10/NMock/default.aspxhttp://nmock.org/
  • Why mock?
The basic principle of unit testing should be to verify only one method at a time, but if such a situation occurs: the testing method depends on other difficult operations, such as networks and databases. Or the code you test depends on other parts of the system, or even multiple other parts of the system. In this case, if you are not careful, you may eventually find that you accidentally initialize almost every component of the system, and all this is just to create a necessary runtime environment for a test. This not only took a lot of time, but it was terrible that such a test routine was introduced with a large number of coupling factors, and it was difficult to achieve the goal of "unit" testing. What should we do? This is a handy method for testing mock. The literal meaning of mock is: ridicule, imitation, and deception. With mock, we can create many alternatives to real objects and use them in test cases.
  • When to use mock

 

1) real objects have undefined behaviors (for example, real-time price of stocks required by a program through Web Service). 2) objects are hard to be created (for example, it is difficult to initialize a system environment) 3) Some actions of real objects are difficult to trigger (such as network errors and Database ID auto-incrementing sequence overflow). 4) real objects make the program run slowly. 5) real objects include UI and other factors that are inconvenient to test 6) test needs to ask how the real object is called (such as Asynchronous call, callbak function needs to be verified) 7) real objects do not exist yet (for example, dependent on other project groups or new hardware systems are required)
  • Mock testing procedure

    • Sample

    • The test code first references the nmock framework
    • Nunit sample code
1) define an interface to describe this object 2) product code to implement this interface 3) Mock object in the test implements this interface
 

What is ncover?

Http://ncover.org/

A good test should have the following qualities: l automation l thorough l repeatable l independent l professional) A good unit test should be thorough. It tests all possible situations. In an extreme case, all possible branches of each line of code and exceptions that may be thrown are used as test objects. Another scenario is the most likely case for testing: for example, various boundary values. In fact, this is a decision-making problem based on your project needs. If you want to test your test coverage rate, you can use tools such as ncover.

The command line used is as follows: "C:/program files/ncover/ncover.lele.exe" "C:/program files/nunit 2.4.1/bin/nunit-console.exe" V: /web/bctest/function/bin/debug/bctest. function. dll/Config = app. config/err = D:/ncoverresult/UTest4BC-Error.txt/xml = D:/ncoverresult/UTest4BC-Result.xml // X
D:/ncoverresult/Coverage-BC.xml // l d:/ncoverresult/Coverage-BC_Log.log // v // a BC. Function

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.