如何检索测试用例中的 no.of 个测试步骤,并以编程方式将单个结果动态更新到 VSTS/TFS 个测试用例中的每个测试步骤

How to retrieve no.of teststeps in a test case and dynamically update individual result to each test step in testcase of VSTS/TFS programatically

我对 VSTS 中的每个测试用例都有多个测试步骤。我想以编程方式在测试用例中找到 no.of 测试步骤,并动态地为每个测试步骤手动更新测试结果(Pass/Fail)。 这样我就可以跟踪在测试用例中哪个测试步骤失败了 运行

我找到了 更新测试用例的测试步骤结果的程序 (Pass/Fail)。这既不能确定测试用例中的 no.of 个测试步骤,也不能单独动态更新测试步骤结果(通过控制台)

测试用例只是另一种工作项类型,步骤只是一个特殊格式的字符串字段(参见 https://www.visualstudio.com/en-us/docs/work/track/build-test-integration#build-and-test-data-fields)。

终于编程成功了。使用 nuget 包 Microsoft Team Foundation Server Extended Client

        Console.WriteLine("Please Provide your Test Plan id");
        int testplanid = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine("Please Provide your testpoint id");
        int testpointid = Convert.ToInt32(Console.ReadLine());

        var u = new Uri("Your account url");
        VssCredentials c = new VssCredentials(new Microsoft.VisualStudio.Services.Common.VssBasicCredential(string.Empty, "Your PAT"));
        TfsTeamProjectCollection _tfs = new TfsTeamProjectCollection(u, c);
        ITestManagementService test_service = (ITestManagementService)_tfs.GetService(typeof(ITestManagementService));
        ITestManagementTeamProject _testproject = test_service.GetTeamProject("Your Project name");
        ITestPlan _plan = _testproject.TestPlans.Find(testplanid);
        ITestRun testRun = _plan.CreateTestRun(false);
        testRun.Title = "TestStep Updation Trail1";
        ITestPoint point = _plan.FindTestPoint(testpointid);
        testRun.AddTestPoint(point, test_service.AuthorizedIdentity);
        testRun.Save();
        testRun.Refresh();
        ITestCaseResultCollection results = testRun.QueryResults();
        ITestIterationResult iterationResult;

        foreach (ITestCaseResult result in results)
        {
            iterationResult = result.CreateIteration(1);
            foreach (ITestStep testStep in result.GetTestCase().Actions)
            {
                String TeststepResult;
                ITestStepResult stepResult = iterationResult.CreateStepResult(testStep.Id);
                Console.WriteLine("Enter the TestStep Outcome:");
                TeststepResult= Console.ReadLine();
                if (TeststepResult=="Passed" || TeststepResult=="passed" || TeststepResult=="pass")
                {
                    stepResult.Outcome = TestOutcome.Passed; 
                }
                else
                {
                    stepResult.Outcome = TestOutcome.Failed;
                }
                iterationResult.Actions.Add(stepResult);
            }
            String TestResult;
            Console.WriteLine("Enter the Overall TestCase Result [Passed/Failed] :");
            TestResult= Console.ReadLine();
            if (TestResult == "Passed" || TestResult == "passed" || TestResult=="pass")
            {
                iterationResult.Outcome = TestOutcome.Passed;
                result.Iterations.Add(iterationResult);
                result.Outcome = TestOutcome.Passed;
            }
            else
            {
                iterationResult.Outcome = TestOutcome.Failed;
                result.Iterations.Add(iterationResult);
                result.Outcome = TestOutcome.Failed;
            }
            result.State = TestResultState.Completed;
            result.Save(true);
        }
        testRun.State = TestRunState.Completed;
        results.Save(true);
        Console.WriteLine("Sucesfully Updated TestCase TestSteps");
        Console.Read();