Assert class in unit test

Source: Internet
Author: User

I. Use of assert class

1. The namespace of the assert class is Microsoft. visualstudio. testtools. unittesting. You only need to reference Microsoft. visualstudio. qualitytools. unittestframework. dll in the project file.

2. The assert class can be used to verify specific functions. The unit test method executes the method code in the development code, but only when the assert statement is included can the code behavior be reported.

3. In the test method, assert can call any number of assert class methods, such as assert. areequal. The assert class has many methods to choose from, and many of them have multiple overloading.

4. You can use the collectionassert class to compare an object set or verify the status of one or more sets.

5. Use the stringassert class to compare strings. This class contains various useful methods. For example, stringassert. Contains, stringassert. matches, and stringassert. startwith.

6. assertfailedexception if the test fails, an assertfailedexception exception is thrown. If the test times out, an unexpected exception is thrown, or an Assert statement containing the failed result is generated, the test fails.

7. assertinconclusiveexception as long as the test result is inconclusive, assertinconclusiveexception is thrown. Generally, add assert. Inconclusive to a test that is still being processed to indicate that the test is not ready and cannot be run.

 

Ii. Main static members of the assert class

 

1. areequal: The method is overloaded for N multiple times. The main function is to determine whether the two values are equal. If the two values are not equal, the test fails. 2. arenotequal: The method is overloaded for N multiple times. The main function is to determine whether the two values are not equal. If the two values are equal, the test fails. 3. arenotsame: whether the referenced objects are different. If the two inputs reference the same objects, the test fails. 4. aresame: whether the referenced objects are the same. If the two inputs reference different objects, the test fails. 5. Fail: The assertion fails. 6. Inconclusive: test results that cannot be proved to be true or false. 7. isfalse: whether the specified condition is false. If the condition is true, the test fails. 8. istrue: whether the specified condition is true. If the condition is false, the test fails. 9. isinstanceoftype: whether the specified object is an instance of the required type; if the required instance is not in the inheritance hierarchy of the object, test loss 10, isnotinstanceoftype: test whether the specified object is an instance of the required type; if the required instance is in the inheritance hierarchy of the object, the test fails. 11. isnull: test whether the specified object is non-empty. 12. isnotnull: test whether the specified object is non-empty. Iii. test instancesC # code Replication
Using Microsoft. visual Studio. testtools. unittesting; using system. text; using system. collections. generic; namespace temp_test {// <summary> // This is tempdll. tempdll test class, designed to // contain all tempdll. tempdll unit test // </Summary> [testclass ()] public class tempdlltest {private testcontext testcontextinstance; // <summary> // obtain or set the test context, the context provides // information about the current test run and its functions. /// </Summary> Public testcontext {get {return testcontextinstance;} set {testcontextinstance = value ;}} /// <summary> /// test method entry // </Summary> [testmethod ()] public void test () {system. io. fileinfo file = NULL; system. io. fileinfo file1 = file; system. io. directoryinfo dir = NULL; string temptrue = "T"; string tempfalse = "F"; assert. areequal (temptrue, tempfalse); // The test fails if the two pairs are different. arenotsame (file1, temptrue); // The two imported objects are the same. Test failed "assert. aresame (file1, file); // The two introduced objects are different assert. fail (); // whatever it is, directly declare the test failed assert. inconclusive (); // similar to fail, but not a failure, but the test cannot pass assert. isfalse (true); // if it is true, it indicates that the test failed assert. istrue (false); // if it is false, request assert for test failure. isinstanceoftype (true, temptrue. getType (); // The type is different. Assert fails to be tested. isnull (tempfalse); // test failed because it is not empty }}}

Assert class in unit test

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.