Edit

Share via


Create a unit test project

Unit tests often mirror the structure of the code under test. For example, a unit test project might 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 from the structure 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.

  2. On the Create a new project page, enter unit test in the search box. Select the project template for the test framework that you want to use, such as MSTest Test Project or NUnit Test Project, and then select Next.

    Screenshot of test project templates in Visual Studio 2022.

    Screenshot of test project templates in Visual Studio 2019.

  3. On the Configure your new project page, enter a name for your project, and then select Create.

  4. 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. In Solution Explorer, right-click the test project, and select Add > Reference.

    2. In Reference Manager, select the Solution node under Projects.

    3. Select the code project you want to test, and then select OK.

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