首页 > 解决方案 > 必须为 AddTestResults Azure DevOps API 调用的自动化测试运行指定 AutomatedTestName

问题描述

我正在尝试在 .NET 中尽可能简单地在 CSV 输入中创建测试结果,看起来最快的方法是首先创建一个空的测试运行,然后将新的测试结果添加到测试运行中。

我已经通过硬编码和运行 Postman 进行了测试,但似乎通过 REST 和 SDK 都收到了相同的错误消息:

当未指定 TestPointId 和 TestCaseId 时,必须为自动化测试运行指定 AutomatedTestName。

这是.NET代码:

var testCase = await testPlanClient.GetTestCaseAsync(_teamProject, {planId}, {suiteId}, "1141");

var testPoint = testCase[0].PointAssignments.SingleOrDefault(p => p.ConfigurationId == testConfiguration.Id);
var pointId = testPoint.Id;

var run = new RunCreateModel(name: testRun.Name, state: "NotStarted", isAutomated: false, plan: new ShallowReference({planid}), pointIds:new int[] { 1654 });

...

testResultList.Add(new TestApi.TestCaseResult()
                {
                    TestCaseTitle = testResult.TestCaseTitle,
                    TestCase = new ShallowReference(id: testResult.TestCaseId.ToString()),
                    TestPoint = new ShallowReference(id: "1654"),
                    TestCaseReferenceId = testResult.TestCaseId,
                    Outcome = SetOutcome(testResult.Outcome),
                    Configuration = new ShallowReference(id: testConfiguration.Id.ToString()),
                    CreatedDate = testResult.CreatedDate,
                    StartedDate = testResult.StartedDate,
                    CompletedDate = testResult.CompletedDate
                });
            }           

List<TestApi.TestCaseResult> createdTestResults = new List<TestApi.TestCaseResult>();
createdTestResults = await testClient.AddTestResultsToTestRunAsync(testResultList.ToArray(), _teamProject, testRunId);

邮差:

POST https://dev.azure.com/{org}/{project}/_apis/test/Runs/{runId}/results?api-version=5.1

[
  {
    "testCaseTitle": "testcase1",
    "testPoint": {
        "id": 17
    },
    "testCase": {
        "id": 1141
    },
    "priority": 1,
    "outcome": "Passed"
  }
]

我在上面的示例中同时输入了 TestPointId 和 TestCaseId ,如果我仍然收到相同的错误消息,两者似乎都不起作用。我查看了类似的答案,例如:How to Add test results to a test run in VSTS using Rest API programmatically但他们似乎都在使用现有的测试结果,我只想将新的测试结果添加到运行中。有任何想法吗?

标签: c#.netazure-devopsazure-devops-rest-apitfs-sdk

解决方案


因为您的测试运行是自动化的,所以您需要向 提供属性AutomatedTestNameTestCaseResults例如:

TestCaseResults results = new TestCaseReults()
{
     TestCaseTitle = "test",
     AutomatedTestName = "name",
     TestCase = new ShallowReference(id: "32"),
     TestPoint = new ShallowReference(id: "1"),
     Outcome = "Passed"
}
List<TestCaseResult> list = new List<TestCaseResult>();
list.add(results);
var response = test.AddTestResultsToTestRunAsync(list.ToArray(), "project", 3214 // Test Run ID).Result;

推荐阅读