Visual Studio Unit Testing Framework
The Visual Studio Unit Testing Framework describes Microsoft's suite of unit testing tools as integrated into some{{Cite web|url=https://greenicicle.wordpress.com/2010/04/13/visual-studio-2010-express-no-tests-please/|title = Visual Studio 2010 Express – No tests, please|date = 13 April 2010}} 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 in Visual Studio or, using MSTest.exe, from a command line.
Elements
=Test class=
Test classes are declared as such by decorating a class with the [http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.testtools.unittesting.testclassattribute.aspx TestClass 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 method=
Test methods are declared as such by decorating a unit test method with the [http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.testtools.unittesting.testmethodattribute.aspx TestMethod 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.
=Assertions=
An assertion 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 the [http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.testtools.unittesting.assert.aspx Assert class].
=Initialization and cleanup methods=
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 the [http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.testtools.unittesting.testinitializeattribute.aspx TestInitialize attribute], while cleanup methods are declared as such by decorating a cleanup method with the [http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.testtools.unittesting.testcleanupattribute.aspx TestCleanup attribute].
Sample test
Below is a very basic sample unit test:
using Microsoft.VisualStudio.TestTools.UnitTesting;
[TestClass]
public class TestClass
{
[TestMethod]
public void MyTest()
{
Assert.IsTrue(true);
}
}
See also
References
{{reflist}}
External links
- (old) [http://msdn.microsoft.com/en-us/library/ms379625.aspx A Unit Testing Walkthrough with Visual Studio Team Test]
- (old) [http://msdn.microsoft.com/en-us/library/ms243147.aspx Microsoft's Unit Testing Framework page]
- (old) [http://msdn.microsoft.com/en-us/library/ms182486.aspx MSTest command-line test execution utility]
- [https://learn.microsoft.com/en-us/dotnet/core/testing/unit-testing-mstest-intro MSTest overview] from Microsoft Learn
- [https://learn.microsoft.com/en-us/dotnet/api/microsoft.visualstudio.testtools.unittesting Microsoft.VisualStudio.TestTools.UnitTesting Namespace] from Microsoft Learn
- [https://github.com/microsoft/testfx#readme Microsoft.Testing.Platform and Microsoft Test Framework (MSTest)], a readme file from the GitHub project page