I am looking for ways to run test suites in parallel.
I am aware of .testrunconfig setting. This allows you to multiplex on the numb
In the AsyncExecutionContext method add the following:
public static void AsyncExecutionContext(DataRow currentRow, AsyncExecutionTask test)
{
if(!BatchStarted)
{
foreach(DataRow row in currentRow.Table)
{
Task testTask = new Task(()=> { test.Invoke(row); });
AsyncExecutionTests.Add(row[0].ToString(), testTask);
testTask.Start();
}
BatchStarted = true;
}
Task currentTestTask = AsyncExecutionTests[row[0].ToString()];
currentTestTask.Wait();
if(currentTestTask.Exception != null) throw currentTestTask.Exception;
}
Now use the class like so:
[TestMethod]
public void TestMethod1()
{
ParallelTesting.AsyncExecutionContext(TestContext.DataRow, (row)=>
{
//Test Logic goes here.
}
);
}
Note: You will have to do some tinkering with exceptions to get them to bubble correctly (you may have an aggregate exception here, you'll need the first exception from it). The amount of time displayed that each test takes to execute will no longer be accurate. You will also want to cleanup the ParallelTesting class after the last row is completed.
How it works: The test logic is wrapped in a lambda and passed to a static class that will execute the logic once for each row of test data when it is first called (first row executed). Successive calls to the static class simply wait for the prestarted test Task to finish.
In this way each call the test framework made to the TestMethod simply collects the test results of the corresponding test that was already run.
Possible Improvements: