After you have created or generated tests for one purpose, you can reuse them in another situation, by loading or importing a test metadata file. People who typically reuse tests are those who do the following:
run tests on production code, such as in a build lab
develop production code
These usages are discussed in Scenarios for Test Reuse.
The Test Metadata File
The test metadata file makes it possible to reuse tests. This is an XML file that has the extension .vsmdi. A test metadata file is automatically added to your solution as when you add a test project, even if the test project contains no tests. If you have a solution open, you can see the metadata file in the Solution Items folder in Solution Explorer.
When you create test lists, they are added to the metadata file as XML tags. When you create tests, their links are also added to the metadata file. Each one of these links specifies a path on disk to the file that contains the test.
A solution can contain at most one test metadata file. A metadata file contains zero or more test lists, which in turn contain zero or more tests. Your solution's metadata file always contains the "List of Tests" node, which serves as the parent node for all the test lists in your solution.
Scenarios for Test Reuse
These are the main scenarios for loading and importing test metadata files:
In the build lab. You are a tester who works in a build lab. Because you are testing compiled code only, you do not have access to the source code you are testing. You might, however, have access to the source files of the tests you are using. As you work, you probably do not have a solution open. To obtain tests to use, load test metadata files into Visual Studio. For more information, see Loading Test Metadata Files.
After you have loaded one metadata file, you might need to run tests in an additional metadata file, In this case, you must import the second metadata file.
During development. You are a developer of production code or of tests. It is likely that your development environment is integrated into Visual Studio Team System. This means that you have access to source control and Team Foundation Build. Eventually, the code you write will either be built in the build lab or used to test production code in the build lab. As you work, you have a solution open. The way you reuse tests is to import existing test projects, import existing tests, or both, into your solution. You can also import test lists by importing test metadata files, but no actual tests are imported when you do this. For more information, see Importing Test Metadata Files.
Loading Test Metadata Files
After you have loaded a metadata file, it is the only open metadata file in your current Visual Studio session. All the tests to which you will have access were loaded when you loaded the metadata file.
You can load a metadata file in any one of three ways:
In the Test List Editor toolbar, click the Load Metadata File button. Then, using the Load Metadata File dialog box, select the file that you want to load.
On the File menu, click Open. Then, using the Open dialog box, select the metadata file that you want to open. Opening the metadata file loads it into your current Visual Studio session.
In Windows Explorer, double-click a metadata file. This opens the metadata file in Visual Studio. Opening the metadata file loads it into your current Visual Studio session.
While you are loading a metadata file, the following actions occur:
Visual Studio makes available all the test lists of the metadata file. You can see the available test lists in the test list pane of the Test List Editor.
Visual Studio tries to resolve test links that are contained in the metadata file. Tests whose links could be resolved appear as enabled in the Test List Editor.
After the metadata is finished loading, you have the following capabilities and restrictions:
You can run any tests whose links were resolved.
You can edit test lists. Editing tests includes the ability to rename them, delete them, drag tests into them, and add child test lists into them.
It is not recommended that you edit tests.
You cannot edit the production code. This code is tested using existing binaries; there is no compiling at this point.
You do not have source control capabilities.
In this situation, you loaded a metadata file while you did not have a solution open. But can you load a metadata file when a solution is open? No, because if you tried to do this, Visual Studio would first close the open solution. If the solution contained unsaved changes, you would be prompted to save it before it closes.
Importing Test Metadata Files
Importing a metadata file merges the contents of the imported metadata file with the metadata file that you currently have open. The currently open metadata file acquires the additional contents but retains its original name.
You can import a metadata file in the following way:
In the Test List Editor toolbar, click the Import Metadata File button. Then, using the Import Metadata File dialog box, select the file that you want to import.
If you currently have no tests loaded, the Import Metadata File button is unavailable.
As you are importing a metadata file, the following actions occurs:
Visual Studio makes available all the test lists of the metadata file that you are importing. You can see its test lists in the Test List pane of the Test List Editor, together with any test lists that were already present in your original metadata file.
If you currently have a metadata file loaded, Visual Studio tries to resolve links to tests in the metadata file that you are importing. If you have a solution loaded, Visual Studio just imports the new test lists. If you then want to import the actual tests, add to your solution the test project that contains the tests, or add the tests individually. Tests whose links are resolved will be available for running and will appear in the Test List Editor as enabled; tests whose links are not resolved will appear as disabled.
After importing is complete, you might have broken links. What is the best way to fix broken links? Import the test project that contains the tests whose links are broken. The tests in the imported project become local, which resolves the links to those tests.
You could also add individual tests or test files to your solution. To do this, right-click a test project in Solution Explorer, click Add, and point to Existing Item. Then, use the Add Existing Item dialog box to locate the file that contains the test. Finally, click Add to copy the item into your solution, or click Add as Link to reference the item but leave it in its original location.
When you add tests, the type of file that you select depends on the type of test you want to add. For information about how to identify test files, see Test Containers.