Share via


Passing Load Test Context Parameters to Unit Tests

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: https://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

Comments

  • Anonymous
    December 15, 2006
    This post will show you how to pass load test context parameters to a unit test. This may be useful if

  • Anonymous
    September 28, 2007
    Is there a way for a Load test to pass parameters by setting the Context Parameter values of a WebTest (I'm referring to the Context Parameters that can be created in the UI)?  These seems more natural then using the TestContextParameters, which requires a coded web test.

  • Anonymous
    October 17, 2007
    This article describes the behavior in VS 2008 for load tests that contain unit tests (VS 2005 was slightly

  • Anonymous
    October 19, 2011
    Helpful post.

  • Anonymous
    March 18, 2013
    The comment has been removed

  • Anonymous
    April 03, 2014
    The comment has been removed

  • Anonymous
    February 04, 2015
    [TestMethod]        public async Task GetUserRole()        {            HttpStatusCode responseResult = HttpStatusCode.OK;            //arrange            NewRequisitionAPIController objController = ApiControllerMock.CreateWebAPI(new NewRequisitionAPIController());            //Act            HttpResponseMessage response = await objController.GetUserRole().ConfigureAwait(false); //await objController.GetUserRole().ConfigureAwait(false);            var actual = response.StatusCode;            var expected = responseResult;            var strContent = response.Content.ReadAsStringAsync().Result;            //Assert                          Assert.IsNotNull(response.Content);            Assert.AreEqual(expected, actual);                } how can i pass the parameter