共用方式為


Creating an Automated Test That Runs an Executable Using Generic Tests

You use generic tests to call external programs and tests. After you have done this, the generic test is treated by the test engine as any other test type. For example, you can run generic tests from Test Explorer and you can obtain and publish results from generic tests just as you do from other tests.

Use a generic test to wrap an existing test, program, or third-party tool that behaves as follows:

  • It can be run from a command line.

  • It returns a value of Pass or Fail.

  • Optionally, it also returns detailed results for 'inner' tests, which are tests that it contains.

Visual Studio treats generic tests just like other tests. You can manage and run them using the same views, and you can obtain and publish their results.

Generic tests are a simple form of extensibility for Visual Studio. They let you run other tests, such as previous tests and custom tests, in addition to the predefined test types, which are Web, load, unit, manual, and ordered.

Requirements

  • Visual Studio Ultimate, Visual Studio Premium, Visual Studio Test Professional

In this topic:

Tasks

Use the following topics to help you in creating generic tests:

Key Tasks

  • Associated Topics

Creating a generic test: You can create a generic test by using the generic test template provided in Visual Studio and by specifying the program that you want to call. For more information, see How to Create a Generic Test in this topic.

Using environment variables in generic tests: You can use environment variables to represent paths to folders that contain files including your target executable and items to deploy.

Run generic tests: You can run the generic tests that you have created from Unit Test Explorer, or by using test categories from the command line.

Determining code coverage: To know how much you are improving the quality of your code, you must determine how much of the code your generic tests are covering when you run them.

Create and use a summary results file: Through the use of a summary results file, a generic test can generate specific, detailed, test results.

How to Create a Generic Test

Note

The following procedure presumes that you have an existing test, program, or third-party tool in the form of executable file to wrap as a generic test. You must be able to run this executable file from the command line, and it must return a value of Pass or Fail.

To create a generic test

  1. In Solution Explorer, open the shortcut menu for either a unit test project or a coded UI test project, point to Add, and then choose Generic Test.

    A template for a generic test is added to your test project. It appears in the main editing window. The new generic test is given a default name, such as GenericTest1.generic, and is displayed in Solution Explorer within the test project.

  2. Under Specify an existing program (a test, test harness, or test adapter) to wrap as a generic test, indicate the path and file name of the test, program, or third-party tool to wrap into a generic test.

  3. (Optional) Under Command line arguments to pass to the generic test, type one or more arguments to pass. Separate multiple arguments with spaces. For more information, see Generic Test Command-Line Arguments in this topic.

  4. (Optional) Under Additional files to deploy with this generic test, specify any files that the test must have to run correctly.

    Note

    Before the test runs, the files that you specify in this step are copied to a deployment directory that is created by Visual Studio.

    For more information, see Deploying Files for Generic Tests in this topic.

  5. (Optional) Under Working Directory, specify a directory that the executable file is to use as a working directory as it runs.

  6. (Optional) Specify the name of a results file.

  7. Save the generic test.

    Tip

    You can create and use an XML based summary results file to provide detailed results for portions of you generic test. For more information, see Using a Summary Results File with a Generic Test.

The test is now ready to run; it will return a result of 0 or another number. The test engine interprets 0 as Passed and any other number as Failed.

Generic Test Command-Line Arguments

You can pass command-line arguments to the program that your generic test wraps.

For example, to pass a single argument, simply type the argument in the box indicated by Command-line arguments to pass to the generic test, in the generic test.

To pass multiple arguments, type them in the Command-line arguments to pass to the generic test box, separated by spaces. For example, type: 12 "%TestDeploymentDir%\mydeployedfile.txt".

You can see this same example in context in Walkthrough: Creating and Running a Generic Test.

Deploying Files for Generic Tests

If either your generic test or the executable file that it wraps need additional supporting files when they run, you can deploy these files before the test is run.

This is shown in the Deploy a File When You Run the Generic Test procedure in Walkthrough: Creating and Running a Generic Test.

To deploy files for a generic test

  1. In Solution Explorer, open a test project that contains your generic test.

  2. Under Additional files to deploy with this generic test, specify any files that the test must have to run correctly.

    Note

    Before the test runs, the files that you specify in this step are copied to a deployment directory that is created by Visual Studio.

  3. Save the generic test.

Security

Not only can a generic test call any executable program on your computer, it uses your credentials as it does so. For this reason, make sure that you trust the author of the program and know what the program will do.

See Also

Tasks

Troubleshooting Generic Tests

Other Resources

Creating Automated Tests