// Mark result in MTM private void MarkResultMethod() { TfsTeamProjectCollection teamCollection = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri("https://mseng.visualstudio.com:443/defaultcollection")); ITestManagementTeamProject project = teamCollection.GetService <ITestManagementService>().GetTeamProject("VSOnline"); var service = teamCollection.GetService <ITestManagementService>(); ITestPlan plan = project.TestPlans.Find(Convert.ToInt32(txtProjectID.Text)); // Create a new test run ITestRun testRun = plan.CreateTestRun(true); // Add the certain a test to the run string query = string.Format("SELECT * from TestPoint where SuiteID='{0}'", txtSuitID.Text); ITestPointCollection testPoints = plan.QueryTestPoints(query); List <string> failedCaceMTM = new List <string>(); foreach (ITestPoint testPoint in testPoints) { // for caseId please type: testPoint.TestCaseId testRun.AddTestPoint(testPoint, null); //string caseName = testPoint.TestCaseWorkItem.Implementation.DisplayText; //string cname = caseName.Substring(caseName.LastIndexOf(".") + 1); //if (GetFailedCaseNamesListFromTrxFile.Contains(cname)) //{ // failedCaceMTM.Add(cname); //} } var blockCase = GetFailedCaseNamesListFromTrxFile.Except(failedCaceMTM).ToList(); testRun.Save(); //Update the outcome of the test ITestCaseResultCollection results = testRun.QueryResults(); ; foreach (ITestCaseResult result in results) { // Get case name in MTM. string caseName = result.Implementation.DisplayText; string name = caseName.Substring(caseName.LastIndexOf(".") + 1); result.Outcome = GetFailedCaseNamesListFromTrxFile.Contains(name) ? TestOutcome.Passed : TestOutcome.Failed; result.State = TestResultState.Completed; result.Save(); } testRun.Save(); testRun.Refresh(); File.Delete(xmlPath); }
public Task SaveResults(string runId, int suiteId) { return(Task.Factory.StartNew(() => { // tell the spinner this task is starting ViewModelLocator.MainWindowViewModel.TaskManager.AddTask(); // retrieve the info for the specified run RunInfo rfRunInfo = Services.Rainforest.Runs.GetSingle(runId); // check to see if the run is finished if (rfRunInfo.state != "complete" && rfRunInfo.state != "aborted") { ViewModelLocator.MainWindowViewModel.TaskManager.RemoveTask(); throw new Core.ServiceException(Core.Error.RunNotComplete, runId); } // run has completed or has been aborted and is being processed, tag can be removed this.TFS.RemoveTag(suiteId, $"RUNID{runId}"); // if run was aborted then drop results if (rfRunInfo.state == "aborted") { ViewModelLocator.MainWindowViewModel.TaskManager.RemoveTask(); throw new Core.ServiceException(Core.Error.RunAborted, runId); } // Run was completed successfully // deserialize the RF tests info that were in the run, ids here are all rfIds, these are NOT JsonObjects.Test objects var anon = new[] { new { id = 0, result = "" } }; var rfTestResults = JsonConvert.DeserializeAnonymousType(rfRunInfo.extras["tests"].ToString(), anon); // Get all the testpoints from the suite var testPointDict = TFS.Session.workingPlan .QueryTestPoints(String.Format("SELECT * FROM TestPoint WHERE SuiteId = {0}", suiteId)) .ToDictionary(tp => tp.TestCaseId, tp => tp); // create a run in tfs to create results against ITestRun tfsTestRun = Session.workingPlan.CreateTestRun(false); // map the RF test results to a new enumerable with stored TFS id var refinedRFTestResults = rfTestResults.Select(r => { // retrieve the test from RF, need to search tag for matching TFS id // each should have a tag already as it was ensured before the run was created // TOEXPAND -> could possible add redundant check for no tags found in case manually removed Test rfTest = Services.Rainforest.Tests.GetSingle(r.id.ToString()); string tfsId = Parse.ExtractIdFromTags("TFSID", rfTest.tags); return new { rfid = r.id, verdict = r.result, tfsId = Int32.Parse(tfsId) }; }) .ToList(); // go through RF results once to find the needed testpoints and add to run foreach (var result in refinedRFTestResults) { // find test point for the matching TFS tc and add to run ITestPoint testPoint = testPointDict[result.tfsId]; tfsTestRun.AddTestPoint(testPoint, testPoint.TestCaseWorkItem.Owner); } tfsTestRun.Save(); // retrieve all the TFS results from the run Dictionary <int, ITestCaseResult> tfsTestResults = tfsTestRun.QueryResults().ToDictionary(r => r.TestCaseId, r => r); // go through RF results again to save the outcomes to the tfs results foreach (var result in refinedRFTestResults) { ITestCaseResult tfsResult = tfsTestResults[result.tfsId]; switch (result.verdict) { case "passed": tfsResult.Outcome = TestOutcome.Passed; tfsResult.State = TestResultState.Completed; break; case "failed": tfsResult.Outcome = TestOutcome.Failed; tfsResult.State = TestResultState.Completed; break; default: // TOEXPAND -> should never get here, verdict will always have a value break; } tfsResult.State = TestResultState.Completed; tfsResult.RunBy = Session.userTFId; tfsResult.Save(); } tfsTestRun.Save(); tfsTestRun.Refresh(); // tell the spinner this task is done ViewModelLocator.MainWindowViewModel.TaskManager.RemoveTask(); })); }
/// <summary> /// This method is used to initiate execution in test manager, create test run and results and set required parameters /// </summary> /// <returns></returns> public bool InitExecution() { //Create a connection to tfs project ITestManagementTeamProject tfsProject = null; tfsProject = GetProject(ProjectUrl, ProjectName); if (tfsProject == null) { throw new Exception("Unabled to connect to test project: " + ProjectName); } //Retrieve test plan details ITestPlanCollection testPlans = tfsProject.TestPlans.Query("select * from TestPlan where PlanName ='" + TestPlanName + "'"); if (testPlans.Count == 0) { throw new Exception("Unabled to locate test plan: " + TestPlanName + " in Test Manager."); } ITestPlan tfsTestPlan = testPlans.First(); //Retrieve test suite details ITestSuiteCollection testSuites = null; //Optionally, test suite id of test manager can be passed as an command line arguments //This helps when same test case has been added to multiple test suites if (TestSuiteId.ToLower().Equals(string.Empty) || TestSuiteId.ToLower().Equals(string.Empty) || TestSuiteId.ToLower().Equals("testsuiteid", StringComparison.OrdinalIgnoreCase)) { testSuites = tfsProject.TestSuites.Query("Select * from TestSuite where Title='" + TestSuiteName + "' and PlanID='" + tfsTestPlan.Id + "'"); } else { testSuites = tfsProject.TestSuites.Query("Select * from TestSuite where Id='" + TestSuiteId + "' and PlanID='" + tfsTestPlan.Id + "'"); } IStaticTestSuite tfsTestSuite = testSuites.Cast <IStaticTestSuite>().FirstOrDefault(testSuite => testSuite.Title.ToLower().Equals(TestSuiteName.ToLower()) || testSuite.Id.ToString().Equals(TestSuiteId)); if (tfsTestSuite == null) { throw new Exception("Unabled to locate test suite: " + TestSuiteName + " in Test Manager Test Plan: " + TestPlanName); } //Get handle to a specific test case in the test suite ITestCase tfsTestCase = tfsTestSuite.AllTestCases.FirstOrDefault(testcase => testcase.Id.Equals(TestCaseId)); if (tfsTestCase == null) { throw new Exception("Unabled to locate test case id: " + TestCaseId + " in Test Manager"); } //Create a test run ITestPoint tfsTestPoint = CreateTestPoints(tfsTestPlan, tfsTestSuite, tfsTestCase); ITestRun tfsTestRun = CreateTestRun(tfsProject, tfsTestPlan, tfsTestPoint); tfsTestRun.Refresh(); //Suprisingly, most recently created test results should be available in last, but test manager returns it at first position //Find test results that were create by the test run ITestCaseResultCollection tfsTestCaseResults = tfsProject.TestResults.ByTestId(tfsTestCase.Id); ITestCaseResult tfsTestResult = tfsTestCaseResults.Last(); //Default assignment foreach (ITestCaseResult testResult in tfsTestCaseResults) { if (testResult.DateCreated.CompareTo(tfsTestRun.DateCreated) == 1) { tfsTestResult = testResult; break; } } //Set test run and result id to property variable for usage while uploading results Property.RcTestRunId = tfsTestRun.Id; Property.RcTestResultId = tfsTestResult.TestResultId; //Set status of test case execution //Set other details on test execution tfsTestResult.ComputerName = Property.RcMachineId; tfsTestResult.DateStarted = DateTime.Now; tfsTestResult.State = TestResultState.InProgress; tfsTestResult.Save(); return(true); }