Welcome to MSDN Blogs Sign in | Join | Help
How to maintain state in a data-driven unit test

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.

Posted: Thursday, February 08, 2007 5:01 PM by pedramr
Filed under:

Comments

No Comments

Leave a Comment

(required) 

(required) 

(optional)

(required) 

  
Enter Code Here: Required

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Page view tracker