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
.
- Create a load test plugin
Add event handlers on the
TestStarting
&TestFinished
events. TheTestStarting
event will fire before theTestInitialize
method and theTestFinished
after theTestCleanup
: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 the
TestInitialize
&TestCleanup
to get/add data from/toUserContext
:[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 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; } }
- 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 theTest 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).