Dela via


How to: Create and Configure Test Projects for Automated Tests

Test projects can contain tests of any type. You can create a test project explicitly. Or, if your solution contains no test projects when you create your first test, a test project is created automatically and the test is placed in it.

Before you create a test project, you can choose to configure the way test projects are created. This means determining how many tests and what kinds of tests are automatically added to new test projects, in addition to the default language of the test project. To do this, you use the Options dialog box, which is available on the Tools menu.

As described in the following procedures, you can create test projects in several ways:

  • Create a test project when you add a test. When you create a new test, you can create new a test project into which the test will be added, or add the test to an existing test project that is already loaded.

  • Create a test project when you generate a unit test. When you generate unit tests, you can create a new test project into which the tests will be added, or add them to an existing test project.

  • Using the Add New Project dialog box. With this method, you use a dialog box to choose the programming language of your test project.

  • Configure test project creation. Set the programming language for new test projects, and whether one or more tests should be added to new test projects by default.

Create a Test Project When You Add a Test

To create a test project when you add a test

  1. Click Test and then click New Test.

    The Add New Test dialog box is displayed.

  2. Under Add to Test Project, choose one of the options for creating a new test project. For example, click Create a new Visual C# Test Project.

    Note

    By default, the type of test project shown under Add to Test Project is the type that is currently set as the default in the Options dialog box, which is available on the Tools menu.

  3. In the Templates pane, select the type of test that you want to add.

  4. Click OK.

    The New Test Project dialog box is displayed.

  5. Enter a name for the new test project, or accept the default name, and then click Create.

  6. Depending on the type of test that you chose, you can either hand-code or generate the newly added test.

The test project and the test it contains are added to your solution.

Create a test project as you generate unit tests

To create a test project as you generate unit tests

  1. Open a source-code file.

  2. Right-click a namespace, class, or method.

  3. Click Create Unit Tests.

    The Create Unit Tests dialog box is displayed.

  4. (Optional) Change the selection of members for which you want to generate unit tests.

  5. Under Output project, choose one of the options for creating a new test project. For example, click Create a new Visual C# Test Project.

  6. Click Generate.

    The New Test Project dialog box is displayed.

  7. Type a name for the new test project, or accept the default name, and then click Create.

The test project is added to your solution together with the new unit test.

Create a test project by using the Add New Project dialog box

To create a test project by using the Add New Project dialog box

  1. In Solution Explorer, right-click the solution.

  2. Point to Add and then click New Project.

    The Add New Project dialog box is displayed. The Project types pane contains a Test Projects node and nodes for specific programming languages.

  3. If you want your new test project to have a specific programming language, expand the node of that language. If not, expand Test Projects.

    Note

    Test projects are not available in every language.

  4. If you expanded a language node, click Test. If you expanded the Test Projects node, click Test Documents.

  5. Under Templates, click Test Project.

  6. (Optional) Change the name or location of the project that you are about to add.

  7. Click OK.

The test project is added to your solution.

Configure Test Project Creation

To configure test project creation

  1. On the Tools menu, click Options.

    The Options dialog box is displayed.

  2. Expand Test Tools in the left pane and then click Test Project.

  3. (Optional) Under Default test project language, choose a programming language for new test projects.

  4. (Optional) You might want to make sure that your test project and the project that contains your code under test use the same language. If you do want them to use the same language, select the check box When automatically creating a test project, disregard the default language setting and use the language of the production-code project instead.

  5. (Optional) You can choose to enable a feature that will cause your Test List and Test View windows to update automatically when you make changes to methods instead of having to click Refresh manually. Doing this has been known to cause some performance problems and other issues with VSMDI files. To enable this feature, clear the Disable background discovery of test methods check box.

  6. Under Select the files that will be added to each new test project, by default, choose, by language, the type of test project that you want to configure.

  7. Select the tests that you want added to test projects of the selected language as those projects are created.

    Note

    You can select zero tests. If you do select zero tests, each new test project is empty. This means that it contains, for example, a property page and a reference to a namespace, but no tests or source-code files. You can add tests later through the Add New Test dialog box.

  8. (Optional) Repeat steps 4 and 5 for test projects in different languages.

  9. Click OK.

    The settings are stored for use the next time that you create a test project.

You can change these preferences at any time, before you create additional test projects.

See Also

Other Resources

Creating Automated Tests