如何在VSTS中构建期间以编程方式将附件添加到测试结果中?

时间:2017-12-04 10:34:29

标签: c# azure-devops

我正在寻找一种方法来将自己的附件添加到测试结果中,以便在构建完成后我可以看到它们....

enter image description here

我想在编译期间以及测试失败后以编程方式添加这些内容。附件将是截图。

这可能吗?

我快速浏览了一下API参考,但这看起来关注的是为现有的测试'运行'添加附件,或者在构建方面用于创建构建定义并触发它们。我可能错过了它,但是在测试任务完成期间或之后我无法找到如何在代码中添加附件。

谢谢,

1 个答案:

答案 0 :(得分:1)

You could get test run of the build first and then retrieve the test result from the test run

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 = x;
        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.Id);
                    Console.WriteLine(tcr.Outcome);
                }

            Console.ReadLine();
        }
        Console.ReadLine();
    }
}

一旦测试结果ID失败,您可以使用Rest API to attach a file to test result

POST https://{instance}/DefaultCollection/{project}/_apis/test/runs/{run}/results/{result}/attachments?api-version={version}
Content-Type: application/json
{
  "stream": { string },
  "fileName": { string },
  "comment": { string },
  "attachmentType": { string }
}