Brief Introduction to nunit

Source: Internet
Author: User

Namespace unittestingexamples
{

Using system;
Using nunit. Framework;

/** // <Summary>
/// Testfixture attribute is mainly used on the class, which indicates that the class contains the test methods to be executed.
/// When you add this attribute to the definition of a class, test runner checks the class to see if the class contains
/// Test Methods
/// </Summary>
[Testfixture]
Public class sometests
{
/** // <Summary>
/// These two functions are mainly used in testfixture, which provides a set of functions before performing any test run (testfixturesetup) and
/// After the last test is executed (testfixtureteardown ). Each testfixture can have only one testfixturesetup method and
/// Testfixtureteardown method. If more than one testfixturesetup and testfixtureteardown methods are used,
/// It can be compiled but not executed. Note that a testfixture can have a testfixturesetup and a setup,
/// You can also have a testfixtureteardown and a teardown method.
/// Testfixturesetup and testfixtureteardown are not convenient to use the setup and teardown methods.
/// Usually use setup and teardown attributes.
/// </Summary>
[Testfixturesetup]
Public void runbeforealltests ()

{

Console. writeline ("testfixturesetup ");

}

[Testfixtureteardown]

Public void runafteralltests ()

{

Console. writeline ("testfixtureteardown ");

}

[Setup]

Public void runbeforeeachtest ()

{

Console. writeline ("setup ");

}

[Teardown]

Public void runaftereachtest ()

{

Console. writeline ("teardown ");

}


/** // <Summary>
/// Test attribute is mainly used to mark the method in text fixture, indicating that the method needs to be tested runner Application
/// Executed. The method with the test attribute must be public and return void, without any input parameters.
/// If these rules are not met, this method will not be listed in the test runner GUI, and it will not be listed during unit test execution.
/// This method will be executed. The code above demonstrates how to use this attribute.
/// </Summary>
[Test]
Public void test1 ()

{

Console. writeline ("test1 ");

}

/** // <Summary>
/// Sometimes, you want your program to produce certain exceptions under certain special conditions. Use unit test to test the program
/// No If the exception is expected to be generated, you can catch the exception using a try .. catch program section, and then set another
/// Boolean value to prove that exception does occur. This method is feasible, but it takes too much effort. In fact, you should use
/// Expectedexception attribute to indicate which exception should be generated by a method
/// </Summary>
[Test]

[Expectedexception (typeof (invalidoperationexception)]
Public void Test2 ()
{
// Do something that throws an invalidoperationexception
}

/** // <Summary>
/// You may not use this attribute frequently. However, this attribute is convenient to use once necessary. You can use this
/// Attribute indicates a test method, which is called test runner. Skip this method during execution and do not execute it.
/// </Summary>
[Test]
[Ignore ("we're skipping this one for now.")]
Public void testone ()
{
// Do something
}



}

}

========================================================== ========================
Blog adapted from diannet

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.