TheVisual Studio Unit Testing Framework describesMicrosoft's suite ofunit testing tools as integrated into some[1] versions of Visual Studio 2005 and later. The unit testing framework is defined in Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll. Unit tests created with the unit testing framework can be executed inVisual Studio or, usingMSTest.exe, from a command line.
Test classes are declared as such by decorating a class with theTestClass attribute. The attribute is used to identify classes that contain test methods. Best practices state that test classes should contain only unit test code.
Test methods are declared as such by decorating a unit test method with theTestMethod attribute. The attribute is used to identify methods that contain unit test code. Best practices state that unit test methods should contain only unit test code.
Anassertion is a piece of code that is run to test a condition or behavior against an expected result. Assertions in Visual Studio unit testing are executed by calling methods in theAssert class.
Initialization and cleanup methods are used to prepare unit tests before running and cleaning up after unit tests have been executed. Initialization methods are declared as such by decorating an initialization method with theTestInitialize attribute, while cleanup methods are declared as such by decorating a cleanup method with theTestCleanup attribute.
Below is a very basic sample unit test:
usingMicrosoft.VisualStudio.TestTools.UnitTesting;[TestClass]publicclassTestClass{[TestMethod]publicvoidMyTest(){Assert.IsTrue(true);}}