How to transfer data from UnitTest to LoadTest?

During my UnitTest, I create data that needs to be referenced in future UnitTests. For instance:

[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 "orderNumber" so that another UnitTest (possibly another agent) can use this generated order. I decided that I could use the database, but then I should manage it, as a turn in deleting elements, and would prefer not to go along this route.

Is there a way to return "orderNumber" to LoadTest and pass this as the Context parameter when calling another UnitTest?

+6
source share
1 answer

You can do this through the LoadTest plugin and using UserContext . Each virtual user has his own UserContext , and you can use it to transfer / retrieve data from TestContext .

  • Create Download Verification Plugin
  • Add event handlers to the TestStarting and TestFinished . The TestStarting event will fire before the TestInitialize and TestFinished after 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 } 
  • Use TestInitialize and 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); } 
  • To access the UserContext tag in the test, add the following property to each UnitTest:

     public LoadTestUserContext UserContext { get { return TestContext.Properties["$LoadTestUserContext"] as LoadTestUserContext; } } 
  • In the test configuration download configuration, Test Mix Model = Based on sequential order , so that your tests run in the order in which you add them to the Test Mix .

Note. . To do this, you need to add each TestMethod to one UnitTest file. If you have them in one file, the TestInialize and TestCleanup will be executed on each TestMethod contained and you may try to access data that you do not have (for example, try to get orderNumber on CreateOrder).

+4
source

Source: https://habr.com/ru/post/946423/


All Articles