Unit Test in Visual Studio

Source: Internet
Author: User
Make sure that the team tester component is installed in your Visual Studio (optional)

After installation, a "test" will be added to the menu of Visual Studio"

Create a C # class library project as a demo to complete an addition operationUsingSystem;
UsingSystem. Collections. Generic;
UsingSystem. text;

Namespace Classlibrary1
{
Public   Class Class1
{
Public   Int Mysum ( Int A, Int B)
{
Return A + B;
}
}
}

Unit test: Right-click the method name mysum and choose "create unit test" from the menu, or choose "test"> "new test" from the menu of Visual Studio ".

You can automatically create a test project that contains the corresponding classes and methods. The solution is as follows:

Automatically Generated tests Code As follows: Code
// The following code is generated by Microsoft Visual Studio 2005.
// The test owner should check the validity of each test.
Using Microsoft. visualstudio. testtools. unittesting;
Using System;
Using System. text;
Using System. Collections. Generic;
Using Classlibrary1;
Namespace Testproject1
{
/**/ /// <Summary>
///This is the test class for classlibrary1.class1.
///Includes all classlibrary1.class1 Unit Tests
///</Summary>
[Testclass ()]
Public   Class Class1test
{


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
{
ReturnTestcontextinstance;
}
Set
{
Testcontextinstance=Value;
}
}
Additional test attributes # Region Additional test attributes
//  
// When writing a test, you can use the following additional attributes:
//
// Use classinitialize to run the code before the first test in the running class
//
// [Classinitialize ()]
// Public static void myclassinitialize (testcontext)
// {
// }
//
// Use classcleanup to run the code after all tests in the class are completed
//
// [Classcleanup ()]
// Public static void myclasscleanup ()
// {
// }
//
// Use testinitialize to run the code before running each test
//
// [Testinitialize ()]
// Public void mytestinitialize ()
// {
// }
//
// Use testcleanup to run the code after each test is run
//
// [Testcleanup ()]
// Public void mytestcleanup ()
// {
// }
//
# Endregion


/**/ /// <Summary>
///Test mysum (INT, INT)
///</Summary>
[Testmethod ()]
Public   Void Mysumtest ()
{
Class1 target =   New Class1 ();

Int A =   0 ; // Todo: Initialize to an appropriate value.

Int B =   0 ; // Todo: Initialize to an appropriate value.

Int Expected =   0 ;
Int Actual;

Actual = Target. mysum (A, B );

Assert. areequal (expected, actual );
}

}
}

Change the initial value and run the test.

Select "test"> "window"> "test Manager" and select "run.

Note: Only Visual Studio team system has the unit test function. For differences between versions, see:
Visual Studio 2005 product line Overview
Comparison of Visual Studio 2005 team system

Related Article

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.