Learning nunit testing 2 (simple nunit usage for understanding nunit)

Source: Internet
Author: User
Related Properties in the test

Testfixture: it marks a class containing the test and declares that the class is used for testing. It is generally used before the definition of Class;

Test is usually placed before the method to test the method.ArticleClass.


Setup/teardown attributes: These two attributes are generally used to initialize/clear resources. The functions tested for these two attributes are called before/after each method. It is generally used to initialize/clear shared data.

Testfixturesetup/testfixtureteardown is also used to initialize/clear resources, but it is used to mark Test
Fixture is used for initialization/cleanup once, that is, it is called only once throughout the test. It is generally used to mark the method for connecting to the database.


Ignore attribute: You can guess what it means. When a method has not been completed, we can add this attribute before its test function, and of course add the [test] tag, so during the test, nunit will ignore the test of this function. After the method is complete, remove the [ignore] label so that it will not be ignored.


Expectedexception: some functions throw some exceptions, so we can write a test function to determine whether the exception result is expected, such as determining whether an object is null:

[Test]

[Expectedexception (typeof (nullreferenceexception)]
Public void
Testforexception ()
{
Int [] arr = NULL;

Class1 myclass = new class1 ();

Myclass. findmax (ARR );
}


Here, because arr is assigned a null value, that is, nullreferenceexception in the expectedexception type, the green light passes the test. There are many other types of exceptions, such as dividebyzeroexception. For details, refer to the nunit documentation.

CATEGORY attribute: This attribute can be used to classify the test method and define the class name, as shown in figure

[Test]
[Category ("Short Time")]
Public void
Mytest1 ()
{
}

In nunit, we can view categories and select the category to be tested for testing.

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.