LoadTestPlugin: Add Scenario Name to TestContext in Unit Test

This load test plugin will add the Load Test Scenario name that the test is running in to the TestContext.Properties. In a unit test, your code would look like this to access the scenario name. With this you could implement some other logic or steps to take based on the load test scenario that this test is running in.

Shows how to use the TestContext to access the Value copied
into the TestContext from the Load Test Plugin:

   [TestClass]
     public class UnitTest2
     {
         private TestContext testContextInstance;
  
         /// <summary>
         ///Gets or sets the test context which provides
         ///information about and functionality for the current test run.
         ///</summary>
         public TestContext TestContext
         {
             get
             {
                 return testContextInstance;
             }
             set
             {
                 testContextInstance = value;
             }
         }
  
         [TestMethod]
         public void TestMehod2()
         {
             string scenarioName = TestContext.Properties["ScenarioName"].ToString();
     
         }
     }

 

Load Test Plugin

Add a Class.cs file to the Test Project, then will need to Add
Reference to the LoadTestFramework
as follows:

Microsoft.VisualStudio.QualityTools.LoadTestFramework

 

These two using will also be needed, and once you have the source code in you need to compile one time before gong to the Load Test and adding the LoadTest Plugin to the loadtest (right click on the loadtest name in the top node in the loadtest editor in VS, choose Add LoadTest Plugin, select from the list, and now your ready to use this:

 

 using System.ComponentModel;
 using Microsoft.VisualStudio.TestTools.LoadTesting;

 

 

    [Description("Load Test Plugin that adds a Context Parameter containing the Test Scenario Name to the Users Context")]

    public class UserTestContextScenarioName : ILoadTestPlugin

    {

        LoadTest loadTest = null;

 

        [Description("Name of the context parameter that will hold the ScenarioName value")]

        [DefaultValue("ScenarioName")]

        public string ContextParamName { get; set; }

 

        public UserTestContextScenarioName()

        {

            ContextParamName = "ScenarioName";

        }

 

        public void Initialize(LoadTest loadTestInit)

        {

            loadTest = loadTestInit;

 

            //wire up the TestStarting event, will fire before every test iteration for each VUser

            loadTest.TestStarting += new EventHandler<TestStartingEventArgs>(loadTest_TestStarting);

        }

 

        void loadTest_TestStarting(object sender, TestStartingEventArgs e)

        {

            if (!string.IsNullOrEmpty(ContextParamName))

            {

                //this adds the ScenarioName to the users test context

                e.TestContextProperties.Add(ContextParamName, e.ScenarioName);

            }

            else

            {

                throw new ArgumentNullException("The required value for ContextParamName was null or empty, please specify a name.");

            }

        }

    }