Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass data from a UnitTest to a LoadTest?

During my UnitTest, I am creating data that needs to be referenced in future UnitTests. For example:

[TestMethod]
public void CreateOrder()
{
    Order order = new Order();
    int orderNumber = order.Create();
    // return orderNumber;
}

[TestMethod]
public void ProcessOrder()
{
    int orderNumber = (int)TestContext.Properties["OrderNumber"];
    ProcessOrder(orderNumber);
}

I need to save off 'orderNumber' so that another UnitTest (possibly on another agent) can use this generated order. I have decided that I can use a database, but then I have to operate it like a queue in removing items, and would prefer not to go that route.

Is there any way to 'return' the orderNumber back to the LoadTest and pass that in as a Context parameter in a call to another UnitTest?

like image 740
esac Avatar asked Oct 03 '22 22:10

esac


1 Answers

You can do it via a LoadTest Plugin and using the UserContext. Each virtual user has his own UserContext and you can use it to pass/retrieve data from TestContext.

  1. Create a load test plugin
  2. Add event handlers on the TestStarting & TestFinished events. The TestStarting event will fire before the TestInitialize method and the TestFinished after the TestCleanup:

    public void TestStarting(object sender, TestStartingEventArgs e)
    {
        // Pass the UserContext into the TestContext before the test started with all its data retrieved so far data.
        // At the first test it will just be empty
        e.TestContextProperties.Add("UserContext", e.UserContext);
    }
    
    public void TestFinished(object sender, TestFinishedEventArgs e)
    {
        // do something with the data retrieved form the test 
    }
    
  3. Use the TestInitialize & TestCleanup to get/add data from/to UserContext:

    [TestInitialize]
    public void TestInitialize()
    {
        // Get the order number which was added by the TestCleanup method of the previous test
        int orderNumber = (int) UserContext["orderNumber"];
    }
    
    [TestCleanup]
    public void TestCleanup()
    {
        // When the CreateOrder test is completed, add the order number to the UserContext
        // so the next will have access to it
        UserContext.Add("orderNumber", orderNumber);
    }
    
  4. To get access to the UserContext on the test add the following property on every UnitTest:

    public LoadTestUserContext UserContext
    {
        get
        {
            return TestContext.Properties["$LoadTestUserContext"] as LoadTestUserContext;
        }
    }
    
  5. In you load test configuration set Test Mix Model = Based on sequential order so your tests will be executed in the order you add them on the Test Mix.

Note: For this to work, you have to add each TestMethod on a single UnitTest file. If you have them at the same file, the TestInialize and TestCleanup methods will be executed on every TestMethod contained, and possibly you will try to access data that you don't have (e.g. trying to get the orderNumber on CreateOrder).

like image 182
chaliasos Avatar answered Oct 10 '22 01:10

chaliasos