1

如何使用 Rest API 在 TFS 中检索构建的单元测试结果?

构建定义使用 VNext (Visual Studio 2015 Update 3)。

var vssConnection = new VssConnection(_configurationSpec.TeamProjectCollection, 
    new VssClientCredentials());
_buildClient = vssConnection.GetClient<BuildHttpClient>();
4

3 回答 3

5

构建的测试结果存储在测试运行中,因此您需要先获取构建的测试运行,然后从测试运行中检索测试结果。以下是代码示例:

class Program
{
    static void Main(string[] args)
    {
        string ur = "https://xxxxxxx/";
        TfsTeamProjectCollection ttpc = new TfsTeamProjectCollection(new Uri(ur));
        //Get build information
        BuildHttpClient bhc = ttpc.GetClient<BuildHttpClient>();
        string projectname = "Project";
        int buildId = 1;
        Build bui = bhc.GetBuildAsync(projectname,buildId).Result;
        //Get test run for the build
        TestManagementHttpClient ithc = ttpc.GetClient<TestManagementHttpClient>();

        Console.WriteLine(bui.BuildNumber);

        QueryModel qm = new QueryModel("Select * From TestRun Where BuildNumber Contains '" + bui.BuildNumber + "'");

        List<TestRun> testruns = ithc.GetTestRunsByQueryAsync(qm,projectname).Result;
        foreach (TestRun testrun in testruns)
        {

            List<TestCaseResult> testresults = ithc.GetTestResultsAsync(projectname, testrun.Id).Result;
            foreach (TestCaseResult tcr in testresults)
                {
                    Console.WriteLine(tcr.TestCase.Name);
                    Console.WriteLine(tcr.Outcome);
                }

            Console.ReadLine();
        }
        Console.ReadLine();
    }
}
于 2016-09-09T03:58:07.957 回答
1

您可以尝试通过在 powershell 脚本中使用此Rest API来获取相关步骤的日志。

GET https://fabrikam-fiber-inc.visualstudio.com/DefaultCollection/Fabrikam-Fiber-Git/_apis/build/builds/391/logs?api-version=2.0

它会返回logs1,logs2对应step1,step2。

{
  "count": 4,
  "value": [
    {
      "lineCount": 3,
      "createdOn": "2015-07-16T19:53:19.747Z",
      "lastChangedOn": "2015-07-16T19:53:19.92Z",
      "id": 1,
      "type": "Container",
      "url": "https://fabrikam-fiber-inc.visualstudio.com/DefaultCollection/6ce954b1-ce1f-45d1-b94d-e6bf2464ba2c/_apis/build/builds/391/logs/1"
    },
    {
      "lineCount": 113,
      "createdOn": "2015-07-16T19:53:29.387Z",
      "lastChangedOn": "2015-07-16T19:53:29.44Z",
      "id": 2,
      "type": "Container",
      "url": "https://fabrikam-fiber-inc.visualstudio.com/DefaultCollection/6ce954b1-ce1f-45d1-b94d-e6bf2464ba2c/_apis/build/builds/391/logs/2"
    },

例如:

只需要获取第 4 步“ Test Assemblies... ” 的日志在此处输入图像描述

于 2016-09-08T08:12:34.497 回答
0

如果您尝试从 Azure DevOps 获取构建测试,您可以使用 Microsoft.TeamFoundationServer.Client nuget 包中提供的新方法:

// Get Test Management client
using var testMgmtClient = connection.GetClient<TestManagementHttpClient>();

// Get tests run for a certain build
var tests = testMgmtClient.GetTestRunsAsync(projectName, builds[0].Uri.AbsoluteUri, includeRunDetails: true).Result;
于 2021-03-30T09:32:31.313 回答