Udostępnij za pośrednictwem


How to: Create a Unit Test without Source Code

[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]

You can create a unit test for an .EXE or .a DLL for which you do not have access to the source code. As with other unit tests, there are two phases to creating a unit test for a compiled .EXE or .DLL.

The first phase is generating a unit test file that contains a skeleton version of a test method for each method in the code that you are testing. Each skeleton test method is generated with empty variables and a placeholder Assert statement.

The second phase is initializing the variables and replacing the placeholder Assert statement with an appropriate one.

After you have created your unit test, you can run it at any time.

Note

For instructions about how to generate a unit test from code that you do have access to, see Creating and Running Unit Tests for Existing Code.

To generate the skeleton for your unit test

  1. Start Visual Studio, and on the Test menu, click New Test.

  2. In the New Test dialog box, click Unit Test Wizard.

  3. In the Add to Test Project box, select the type of project that you want to create, and then click OK.

  4. In the New Test Project dialog box, type a new name or accept the default name, and then click Create.

  5. In the Create Unit Test dialog box, click Add Assembly.

  6. In the Add Assembly dialog box, locate the assembly for which you want to create unit tests and then click Open.

  7. In the Create Unit Tests dialog box, under Types, select the methods for which you want to create unit tests and then click OK.

To add validation to your unit test

  1. Open the unit test file that contains your unit test, and then locate the unit test for which you want initialize variables.

  2. Locate the variable assignments in the unit test.

    In newly generated tests, variable assignments are marked by "TODO" statements that remind you to customize the assignments. For example, the following is a typical assignment that has to be edited:

    string target.owner = null; // TODO: Initialize to an appropriate value

  3. Assign an appropriate value to each variable. For an example of assigning appropriate variables, see the procedure "Run and Edit a Unit Test" in Walkthrough: Creating and Running Unit Tests.

  4. Locate and edit the Assert statement in the unit test. If it is necessary, add more Assert statements. For more information about available Assert statements, see Using the Assert Classes.

  5. (Optional) Add setup and cleanup code for your unit tests by using the [TestInitialize()] and [TestCleanup()] methods of the Microsoft.VisualStudio.TestTools.UnitTesting namespace. When you generate a unit test, an "Additional test attributes" section is added to your unit test file. Expand this section to show commented out methods that can be used to include initialization and cleanup.

See Also

Concepts

Creating and Running Unit Tests for Existing Code