How to: Create a Web Performance Test Plug-In

This topic applies to:

Visual Studio Ultimate

Visual Studio Premium

Visual Studio Professional 

Visual Studio Express

Topic applies Topic does not apply Topic does not apply Topic does not apply

Web performance tests plug-ins enable you to isolate and reuse code outside the main declarative statements in your Web performance test. A customized Web performance test plug-in offers you a way to call some code as the Web performance test is run. The Web performance test plug-in is run one time for every test iteration. In addition, if you override the PreRequest or PostRequest methods in the test plug-in, those request plug-ins will run before or after each request, respectively.

You can create a customized Web performance test plug-in by deriving your own class from the WebTestPlugin base class.

You can use customized Web performance test plug-ins with the Web performance tests you have recorded, which enables you to write a minimal amount of code to obtain a greater level of control over your Web performance tests. However, you can also use them with coded Web performance tests. For more information, see How to: Create a Coded Web Performance Test.

Note

You can also create load test plug-ins. For more information, see How to: Create a Load Test Plug-In.

To create a custom Web performance test plug-in

  1. Open a test project that contains a Web performance test.

    For more information about how to create a test project, see How to: Create and Configure Test Projects for Automated Tests.

  2. In Solution Explorer, right-click on the solution and select Add and then click New Project.

    The Add New Project dialog box is displayed.

  3. Under Installed Templates, select Visual C#.

  4. In the list of templates, select Class Library.

  5. In the Name text box, type a name for your class.

  6. Click OK.

  7. The new class library project is added to Solution Explorer and the new class appears in the Code Editor.

  8. In Solution Explorer, right-click the References folder in the new class library and select Add Reference.

  9. The Add Reference dialog box is displayed.

  10. Click the .NET tab, scroll down, and select Microsoft.VisualStudio.QualityTools.WebTestFramework

  11. Click OK.

    The reference to Microsoft.VisualStudio.QualityTools.WebTestFramework is added to the Reference folder in Solution Explorer.

  12. In Solution Explorer, right-click on the top node of the test project that contains the load test to which you want to add the Web performance test plug-in and select Add Reference.

  13. The Add Reference dialog box is displayed.

  14. Click the Projects tab and select the Class Library Project.

  15. Click OK.

  16. In the Code Editor, write the code of your plug-in. First, create a new public class that derives from WebTestPlugin.

  17. Implement code inside one or both of the PreWebTest and PostWebTest event handlers. See the following Example section for a sample implementation.

  18. After you have written the code, build the new project.

  19. Open a Web performance test.

  20. To add the Web performance test plug-in, click Add Web Test Plug-in on the toolbar.

    The Add Web Test Plug-in dialog box is displayed.

  21. Under Select a plug-in, select your Web performance test plug-in class.

  22. In the Properties for selected plug-in pane, set the initial values for the plug-in to use at run time.

    Note

    You can expose as many properties as you want from your plug-ins; just make them public, settable, and of a base type such as Integer, Boolean, or String. You can also change the Web performance test plug-in properties later by using the Properties window.

  23. Click OK.

    The plug-in is added to the Web Test Plug-ins folder.

    Warning

    You might get an error similar to the following when you run a Web performance test or load test that uses your plug-in:

    Request failed: Exception in <plug-in> event: Could not load file or assembly '<"Plug-in name".dll file>, Version=<n.n.n.n>, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.

    This is caused if you make code changes to any of your plug-ins and create a new DLL version (Version=0.0.0.0), but the plug-in is still referencing the original plug-in version. To correct this problem, follow these steps:

    1. In your test project, you will see a warning in references. Remove and re-add the reference to your plug-in dll.

    2. Remove the plug-in from your test or the appropriate location and then add it back.

Example

The following code creates a customized Web performance test plug-in that adds an item to the WebTestContext that represents the test iteration.

After running the Web performance test, by using this plug-in you can see the added item that is named TestIteratnionNumer in the Context tab in the Web Performance Results Viewer.

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.VisualStudio.TestTools.WebTesting;

namespace SampleRules
{
    public class SampleWebTestPlugin : WebTestPlugin
    {
        // start counting iterations at 1 not 0
        // so that the iteration number we give matches the run number
        static int testIterationNumber = 1;

        public override void PostWebTest(object sender, PostWebTestEventArgs e)
        {
        }

        public override void PreWebTest(object sender, PreWebTestEventArgs e)
        {
            e.WebTest.Context["TestIterationNumber"] = testIterationNumber;
            testIterationNumber++;
        }
    }
}

See Also

Tasks

How to: Create a Request-Level Plug-In

How to: Create a Custom Extraction Rule for a Web Performance Test

How to: Create a Custom Validation Rule for a Web Performance Test

How to: Create a Load Test Plug-In

How to: Create a Coded Web Performance Test

How to: Edit an Existing Web Performance Test Using the Web Performance Test Editor

Reference

WebTestRequestPlugin

Other Resources

Creating and Using Custom Plug-ins for Load and Web Performance Tests