// 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 static void UpdateTestRun(ITestRun run, bool outcome, string comment) { run.Comment = comment; run.Save(); // Query results from the run. ITestCaseResult result = run.QueryResults()[0]; if (outcome == true) { // Fail the result. result.Outcome = TestOutcome.Failed; } else if (outcome == false) { // Pass the result. result.Outcome = TestOutcome.Passed; } result.State = TestResultState.Completed; result.Save(); }
static void Main(string[] args) { TfsTeamProjectCollection tfs = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri("http://pradeepn-tcm:8080/tfs/DefaultCollection")); ITestManagementTeamProject project = tfs.GetService <ITestManagementService>().GetTeamProject("Pradeep"); // Create a test case. ITestCase testCase = CreateTestCase(project, "My test case"); // Create test plan. ITestPlan plan = CreateTestPlan(project, "My test plan"); // Create test configuration. You can reuse this instead of creating a new config everytime. ITestConfiguration config = CreateTestConfiguration(project, string.Format("My test config {0}", DateTime.Now)); // Create test points. IList <ITestPoint> testPoints = CreateTestPoints(project, plan, new List <ITestCase>() { testCase }, new IdAndName[] { new IdAndName(config.Id, config.Name) }); // Create test run using test points. ITestRun run = CreateTestRun(project, plan, testPoints); // Query results from the run. ITestCaseResult result = run.QueryResults()[0]; // Fail the result. result.Outcome = TestOutcome.Failed; result.State = TestResultState.Completed; result.Save(); Console.WriteLine("Run {0} completed", run.Id); }
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(); })); }