A loadtest plugin to clear context parameters (and why we need it)

If a webtest stores temporary data in a context parameter, then you need to make sure that its reset to some initial state when the webtest starts. Otherwise it will fail if that webtest is included multiple times within the same testrun.

Visual Studio’s “Set Context Parameter Value” plug-in won’t help here as it cannot set the value of a parameter to an empty string. If you try this, then during the test run, the request will fail with the error “Request failed: Missing or invalid Context Parameter Name.”

The solution in my case was a very simple custom plug-in. I choose to implement it as a WebTestPlugin instead of a WebTestRequestPlugin to avoid confusing it with Visual Studio’s existing plugin.

using System.ComponentModel;
using Microsoft.VisualStudio.TestTools.WebTesting;
using Microsoft.VisualStudio.TestTools.WebTesting.Rules;

namespace LoadTestPlugins.WebTestPlugins
{
    [DisplayNameAttribute("Set context parameter value")]
    [DescriptionAttribute("Sets the context parameter to a value")]
    public class ClearContextParameter : WebTestPlugin
    {
        public override void PreWebTest(object sender, PreWebTestEventArgs e)
        {
            e.WebTest.Context[this.ContextParameter] = this.Value;
        }

        #region Properties
        [Description("Context parameter to set"),
        DisplayName("Context parameter"), IsContextParameterName(true)]
        public string ContextParameter { get; set; }

        [Description("Value to place into the context parameter"),
        DisplayName("Value")]
        public string Value { get; set; }
        #endregion
    }
}