Imagine you need to maintain a list of processed data rows in a running data-driven unit test. Well, you can use the code below as a starting point:


[DataSource(
   "System.Data.SqlClient", "Data Source=.;Initial
      Catalog=FeatureDemo;Integrated Security=True"
,
   "Person",
   DataAccessMethod.Sequential),
   TestMethod(),]
public void Test()
{
   if (!TestContext.Properties.Contains("Session"))
   {
      TestContext.Properties["Session"] = Guid.NewGuid();
   }
   Guid session = (Guid)TestContext.Properties["Session"];
   // Now add your unit test code here
   // ...
}

TestContext is the way to go in order to maintain state during one test instance. The main point here is that a data-driven test is viewed as a single test instance (although the test method is called many times for each row of the data table).

You can also use the above unit test in a load test scenario knowing that the state of each logical load test thread is maintained independently.