编程语言
首页 > 编程语言> > c# – 如何在VSTS构建期间以编程方式将附件添加到测试结果中?

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

作者:互联网

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

enter image description here

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

这可能吗?

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

谢谢,

解决方法:

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 }
}

标签:c,azure-devops
来源: https://codeday.me/bug/20190705/1389718.html