|Important||This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer|
How to: Author a Unit Test
There are two reasons to edit a unit test: You are authoring it by hand, or you are editing a newly generated unit test. Although you can run newly generated unit tests, they are created with default content that must be initialized to appropriate values before the test can produce meaningful results. Within a generated unit test, you typically need to customize variable assignments and one or more Assert statements.
Using Assert Statements in Unit Tests
By default, each generated unit test calls themethod, which causes the test to fail because the test is still essentially unimplemented. Your next step is to add meaningful code to check the correct operation of the method being tested. A typical way to do this is to generate a value and then compare it with an expected value by using an Assert.AreEqual statement. For an example, see "Unit Test Example" in . Newly generated unit tests contain "To-do" comments that suggest changes to make.
A unit test that contains no Assert statement automatically passes as long as it does not time out and does not throw an unexpected exception. For more information, seeand .
Opening and Authoring Unit Tests
This topic contains two procedures:
The first procedure describes how to edit an existing unit test. You typically do this to prepare a unit test that has been generated automatically. See.
The second procedure describes how to create and author a unit test by hand.
To edit an existing unit test
In your test project in Solution Explorer, locate and open the file that contains the unit test, and then locate the unit test method that you want to edit.
- or -
In Test View, double-click the unit test; this opens the file that contains the unit test and scrolls to the unit test method.
Locate the variable assignments in the method.
In newly generated tests, variable assignments are marked by "To-Do" statements that remind you to customize the assignments. For example, the following is a typical assignment that needs to be edited:
string target.owner = null; // TODO: Initialize to an appropriate value
Assign an appropriate value to each variable.
To know what values are appropriate, consider the values that these variables may be initialized to before the method is called, the changes they may undergo when the method is called, and the results you expect. For an example of this process, see the procedure Run and Edit a Unit Test in.
Locate and edit the Assert statements in the method. If necessary, add additional Assert statements.
The Unit Testing Framework provides numerous additional Assert classes and methods that give you flexibility in writing useful Assert statements. For more information, see.
To create a unit test by typing it in
In Solution Explorer, right-click a test project, point to Add, and click New Test.
- or -
Right-click the surface of the Test View window and then click New Test.
This displays the Add New Test dialog box.
Under Templates, click Unit Test and then click OK.
A new source code file with a name such as UnitTest1.cs is added to your test project, in the language of the test project. This file contains several things that unit tests require:
It references the Microsoft.VisualStudio.TestTools.UnitTesting namespace and the System namespace.
It defines its own namespace, which contains a test class. Test classes have the [TestClass] attribute.
It contains an initialization method and a cleanup method. These methods have the [TestInitialize()] and [TestCleanup()] attributes, respectively.
It contains one empty test method, with a [TestMethod] attribute. It is here that you add your test logic. This method has a default name such as TestMethod1().
This file is also opened in the window for editing source code. The new (empty) test method is displayed in the Test View and Test Manager windows.
Add test code to the test method.
The Unit Testing Framework provides numerous additional Assert classes and methods that give you flexibility in writing useful Assert statements. For more information, seeand .