Run one Automated Unit Test against multiple Test Cases in Microsoft Test Manager

Over the last few months, I have been running into an issue with the Microsoft Unit Test Framework an association with Microsoft Test Manager (MTM). The idea was to have one [TestMethod] for multiple test cases inside of MTM with test parameters to associate a suite of tests with ReadyAPI. The issue I was occurring was when multiple test cases were selected for a single test run. The same test cases was running against multiple test cases. This was because the TestContext was not updating because the [TestMethod] was associated to multiple test cases.

Below is the Test Initialize method to grab the test run information each time the test has been run

public void TestInitialize()  
                // Check if we are running with a MTM test context.
                // If not, throw a friendly exception with error message.
                if (this.TestContext.Properties["__Tfs_TestRunId__"] != null)
                    // Get test run id from current test run.
                    TestRunId = this.TestContext.Properties["__Tfs_TestRunId__"].ToString();

                    ITestManagementService testMgmtService =
                    ITestManagementTeamProject testMgmtTeamProject =

                    // Get the ongoing test run object by test run id.
                    ITestRun testRun = testMgmtTeamProject.TestRuns.Find(Convert.ToInt32(TestRunId));

                    TestResults = null;

                    // Find the in-progress test.
                    // Since updates are pushed async to TFS, wait for this test to show up in the progress list.
                    // Based on that, query the test configuration and obtain the test case id in a workaround.
                        TestResults = testRun.QueryResultsByStatus(TestResultState.InProgress);

                        // Find the test result that corresponds to the current run on this server.
                        if ((TestResults != null) && (TestResults.Count > 0))
                            int id = TestResults.First().TestPointId;
                            // If this is the first test case in a run,
                            // determine the original test point id so that changes
                            // to the test point id can be seen in consecutive runs.
                            if (lastTestPointId == 0 || lastTestPointId != id)
                                lastTestPointId = id;

                        // If no break was reached,
                        // reset the while loop variable to continue with the next iteration.
                        // Also wait for X seconds before querying TFS again.
                        TestResults = null;

                    } while (TestResults == null);

                    // At this point, the currently running test has been found.
                    ITestCaseResult testCaseResult = TestResults.Where(t => t.TestPointId == lastTestPointId).FirstOrDefault();

                    // Set the current test case id.
                    this.TestCaseId = testCaseResult.TestCaseId.ToString();

                    // Set 'testCase' to the TFS ID of the current test case.
                    ITestCase testCaseItem = testMgmtTeamProject.TestCases.Find(Convert.ToInt32(TestCaseId));

                    // The 'Rows' object contains a collection of the parameter values on the test case.
                    // e.g. @TestCase = "Simple Tests"; rows[0].ItemArray[0].ToString() = "Simple Tests"
                    DataRowCollection rows = testCaseItem.DefaultTable.Rows;

                    // Set the values to be passed to RunTest().
                    this.TestCaseName = rows[0].ItemArray[0].ToString();
                    this.Log.LogMessage(MessageType.INFORMATION, "Attempting: Get Test Suite Name parameter.");
                    this.TestSuiteName = rows[0].ItemArray[1].ToString();
                    throw new ArgumentNullException("Test Run Id was not found in Test Context.");

            catch (Exception e)
                throw e;


Inside the test method

        public void RunTestCase()
            TestRunner = new TestRunner(this.TestSuiteName, this.TestCaseName);

Conclusion: The tests are able to run via the parameters, but it adds an additional 30 seconds to each test run because of a limitation to TFS where the default refresh interval is 30 seconds.