Freigeben über


Create a unit test project

Applies to: yesVisual Studio noVisual Studio for Mac

Note

This article applies to Visual Studio 2017. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

Unit tests often mirror the structure of the code under test. For example, a unit test project would be created for each code project in the product. The test project can be in the same solution as the production code, or it can be in a separate solution. You can have multiple unit test projects in a solution.

Note

The location of unit tests for native code and the test project structure can be different than the structure that's described in this article. For more information, see Writing unit tests for C/C++.

To create a unit test project

  1. On the File menu, select New > Project, or press Ctrl+Shift+N.
  1. In the New Project dialog box, expand the Installed node, choose the language that you want to use for your test project, and then choose Test.

  2. Select the project template for the test framework that you want to use, for example MSTest Test Project or NUnit Test Project. Name the project, and then choose OK.

    Test project templates in Visual Studio 2017

  1. In your unit test project, add a reference to the code under test. To add a reference to a code project in the same solution:

    1. Select the test project in Solution Explorer.

    2. On the Project menu, select Add Reference.

    3. In Reference Manager, select the Solution node under Projects. Select the code project you want to test, and then select OK.

    If the code that you want to test is in another location, see Managing references in a project for information about adding a reference.

Next steps

See one of the following sections:

Writing unit tests

Running unit tests