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.
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.
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.
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); });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With