This post will show you how to pass load test context parameters to a unit test.  This may be useful if you have some variables that you want to be able to change for each running of a load test.  My simple example will show you how to create a load test plug-in which will read the load test context parameters and pass them on to the unit test.  In my example, I am going to set a context parameter for the amount of time a unit test should sleep.

 

First let’s create the unit test.  It would look like the following:

 

using System;

using System.Text;

using System.Collections.Generic;

using Microsoft.VisualStudio.TestTools.UnitTesting;

 

namespace BlogPost

{

   

    [TestClass]

    public class SleepExample

    {

        TestContext testContextInstance1;

        public SleepExample()

        {

           

        }

 

        //used to call the base methods of TestContext

        public TestContext TestContext

        {

            get { return testContextInstance1; }

            set { testContextInstance1 = value; }

        }

 

        [TestMethod]

        public void TestMethod1()

        {

            //check for the SleepTime Context Parameter

            //If it does not exist then default to 1000

            int sleep = 1000;

            if (TestContext.Properties.Contains("SleepTime"))

            {

                sleep = Int32.Parse((string)TestContext.Properties["SleepTime"]);

            }

 

            System.Threading.Thread.Sleep(sleep);

 

            //do the rest of the work for the unit test

        }

    }

}

 

It is a good practice to define a default value for any parameter that you are reading from the context.  This way the unit test can still execute when not being run within a load test.  In this example, the sleep value is set to 1000.  Then the test checks the context for a Parameter called SleepTime.  If it exists, then sleep is set to this value.

 

Now let’s look at how you would set the value in a load test and then pass it to the unit test from a load test plug-in. 

 

First create the load test and add the above unit test.  Load test context parameters are set on the run settings node in the load test.  Simply click on the run setting you want to add the parameter to and select “Add Context Parameter”.  Then set the name and value for the context parameter.“Add Context Parameter”.  Then set the name and value for the context parameter.  In this example, I will set the parameter name to SleepTime with a value of 3000.

 

 

Now the load test context will have the parameter, but these values are not automatically passed to the unit test context.  You can accomplish this with a simple Load test plug-in.  Load test plug-ins provide a way for you to hook your own code into the load test framework.  A number of different events are exposed.  Check out this help topic for more detailed information about load test plug-ins: http://msdn2.microsoft.com/en-us/library/ms243153.aspx

 

For this example, the plug-in will connect to the TestStarting event which is fired right before each test iteration is executed.  In this event, we will copy the load test context parameters to the unit test context.  Here is the plug-in code:

 

using System;

using System.Collections.Generic;

using System.Text;

using Microsoft.VisualStudio.TestTools.LoadTesting;

 

namespace Blog

{

    public class CopyParamtersPlugin : ILoadTestPlugin

    {       

 

        //store the load test object.  

        LoadTest mLoadTest;

 

        public void Initialize(LoadTest loadTest)

        {

            mLoadTest = loadTest;

           

            //connect to the TestStarting event.

            mLoadTest.TestStarting += new EventHandler<TestStartingEventArgs>(mLoadTest_TestStarting);

        }

 

        void mLoadTest_TestStarting(object sender, TestStartingEventArgs e)

        {

            //When the test starts, copy the load test context parameters to

            //the test context parameters

            foreach (string key in mLoadTest.Context.Keys)

            {

                e.TestContextProperties.Add(key, mLoadTest.Context[key]);

            }

        }               

    }

}

 

Now we need to set this as the plug-in for the load test to use.  This is done by right clicking on the root node of the load test in the load test editor and selecting “Set Load Test Plug-In…”  This will launch a dialog which will display the plug-in above.  Select this plug-in and choose OK.

 

 

 

 

Now when you run the load test, the unit test will sleep for 3 seconds instead of one.  You can see this by looking at the test time for the unit test in the Load Test Analyzer.  In the screen shot below, the test time is about 3 seconds. 

 

 

One other thing you can do while developing load test plug-ins is debug them.  Place a break point in your code.  Then run the load test under the debugger.  You can do this from the editor by selecting debug test instead of play test from the Play button on the load test editor tool bar.  Another way to do this is select the load test in the test view window and then choose debug test.  This is useful when writing the plug-ins to make sure they are working correctly.

 

Hopefully this example shows you how you can change the behavior of unit tests running under load without modifying the unit test code.

 

Examples in VB:

Imports System

Imports System.Text

Imports System.Collections.Generic

Imports Microsoft.VisualStudio.TestTools.UnitTesting

 

<TestClass()> Public Class UnitTest1

    Dim testContextInstance1 As TestContext

 

    'used to call the base methods of TestContext

    Public Property TestContext() As TestContext

        Get

            Return testContextInstance1

        End Get

        Set(ByVal Value As TestContext)

            testContextInstance1 = value

        End Set

    End Property

 

 

    <TestMethod()> Public Sub TestMethod1()

        'check for the SleepTime Context Parameter

        'If it does not exist then default to 1000

        Dim sleep As Integer

        sleep = 1000

 

        If TestContext.Properties.Contains("SleepTime") Then

            sleep = Int32.Parse(CType(TestContext.Properties("SleepTime"), String))           

        End If

        System.Threading.Thread.Sleep(sleep)

 

        ' TODO: Add test logic here

    End Sub

 

End Class

 

 

 

 

Imports System.Collections.Generic

Imports System.Text

Imports Microsoft.VisualStudio.TestTools.LoadTesting

 

 

Public Class CopyParamtersPlugin

    Implements ILoadTestPlugin

 

 

 

    'store the load test object. 

    Dim mLoadTest As LoadTest

 

    Public Sub Initialize(ByVal loadTest As LoadTest) Implements ILoadTestPlugin.Initialize

        mLoadTest = loadTest

 

        'connect to the TestStarting event.

        AddHandler mLoadTest.TestStarting, AddressOf mLoadTest_TestStarting

 

    End Sub

 

 

 

    Sub mLoadTest_TestStarting(ByVal sender As Object, ByVal e As TestStartingEventArgs)

        'When the test starts, copy the load test context parameters to

        'the test context parameters

        For Each key As String In mLoadTest.Context.Keys

            e.TestContextProperties.Add(key, mLoadTest.Context(key))

        Next

    End Sub

End Class