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