Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parallelize a Data-Driven unit test in Visual Studio 2010?

Tags:

I know regular MS-Test unit tests can be parallelized on a multi-core machine (with caveats of course) by specifying parallelTestCount attribute in the .testresults file in the test solution. Like this,

<Execution parallelTestCount="1">     <TestTypeSpecific />     <AgentRule name="Execution Agents"></AgentRule> </Execution> 

More at MSDN: Executing Unit Tests in parallel on a multi-CPU/core machine

However, I have a data-driven test, something like this, this is just one test, but the input comes in from a csv and runs 1000s of records through the same test.

[DeploymentItem("InputDataRows.csv"), Timeout(37800000), DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", "|DataDirectory|\\InputDataRow.csv", "InputDataRow#csv", DataAccessMethod.Sequential)]                 [TestMethod] public void RunProcessing() {     int userId = Convert.ToInt32(TestContext.DataRow[0].ToString());     int connId = Convert.ToInt32(TestContext.DataRow[1].ToString());     string xml = TestHelper.GetDataFromDb(userId, connId);     a = doStuffA(xml);      b = doStuffB(xml);     Assert.IsTrue(a == b); } 

Because this is a slow process, I am looking at parallelizing this unit test.

The Sequential enum on the attribute is just the way it accesses data, the other option is Random, which is still serial and not parallel.

like image 471
Vin Avatar asked Oct 28 '10 19:10

Vin


People also ask

How do I run a specific unit test in Visual Studio?

Run tests in Test Explorer If Test Explorer is not visible, choose Test on the Visual Studio menu, choose Windows, and then choose Test Explorer (or press Ctrl + E, T). As you run, write, and rerun your tests, the Test Explorer displays the results in a default grouping of Project, Namespace, and Class.

How do I run a parallel test in Visual Studio?

Visual Studio 2019 runs unit tests sequentially when it is suppose to run them in parallel. Click on the Run Tests in Parallel button in Test Explorer. Make sure the icon is highlighted. Run unit tests.


1 Answers

In order to parallelize this unit test, you'll need doStuffA() and doStuffB() to be able to operate on a subset of the data (e.g. a chunk or even a single row of your csv at a time). If you can refactor your methods to behave in this way, you can utilize tasks or a parallel foreach loop so that this test executes in parallel. Suppose your methods were refactored to handle a row of your csv, you could do something like this:

int userId = Convert.ToInt32(TestContext.DataRow[0].ToString()); int connId = Convert.ToInt32(TestContext.DataRow[1].ToString()); string xml = TestHelper.GetDataFromDb(userId, connId); var rows = xml.Split('\n');  Parallel.ForEach(rows, (row) => {     var a = doStuffOnRowA(row);     var b = doStuffOnRowB(row);     Assert.AreEqual(a, b); }); 
like image 192
cortez Avatar answered Oct 18 '22 20:10

cortez