将日志文件附加到Azure Devops测试运行中

问题描述 投票:1回答:1

我正在尝试附加我在Azure发布管道中运行自动测试的过程中生成的日志文件。我正在使用Selenium和MSTest运行自动UI测试。最初,我的印象是可以使用TestContext对象将文件附加到MSTest的AssemblyCleanup方法中。但是,看来TestContext只能用于将结果附加到各个测试用例吗?在网上搜索了一下之后,我发现了这个API调用:

POSThttps://dev.azure.com/{组织} / {项目} / _ apis / test / Runs / {runId} /attachments?api-version=5.1-preview.1

在Microsoft文档中:https://docs.microsoft.com/en-us/rest/api/azure/devops/test/attachments/create%20test%20result%20attachment?view=azure-devops-rest-5.1

但是,我不知道如何在测试代码中保留测试runId或如何通过OAuth2进行身份验证。这是我可以使用RestSharp拨打电话的范围;

RestClient httpClient = new RestClient("");
//I have no clue if this is even close to right with the OAuth stuff
httpClient.Authenticator = new RestSharp.Authenticators.OAuth2AuthorizationRequestHeaderAuthenticator("");
RestRequest request = new RestRequest(Method.POST);
APIRequestBody body = new APIRequestBody
{
    //not sure if this is the right stream
    Stream = "",//not sure what to put in here
    FileName = "TestRun.log",
    Comment = "Test run log file.",
    AttachmentType = "GeneralAttachment"
};
request.AddJsonBody(body);
IRestResponse response = httpClient.Execute(request);

任何帮助都将非常棒...我在这里有点不高兴。

c# selenium azure-devops mstest web-api-testing
1个回答
0
投票

下面是一个代码示例,显示了如何使用个人访问令牌在c#HttpClient中调用Restful api。您可以按照here步骤获取您的个人访问令牌。

public static async void GetProjects()
{
    try
    {
        var personalaccesstoken = "PAT_FROM_WEBSITE";

        using (HttpClient client = new HttpClient())
        {
            client.DefaultRequestHeaders.Accept.Add(
                new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
                Convert.ToBase64String(
                    System.Text.ASCIIEncoding.ASCII.GetBytes(
                        string.Format("{0}:{1}", "", personalaccesstoken))));

            using (HttpResponseMessage response = await client.GetAsync(
                        "https://dev.azure.com/{organization}/_apis/projects"))
            {
                response.EnsureSuccessStatusCode();
                string responseBody = await response.Content.ReadAsStringAsync();
                Console.WriteLine(responseBody);
            }
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.ToString());
    }
}

以上示例来自Microsoft document。您可以检查出来。

对于测试runId,您可能需要在test runs api下调用以获取测试运行的列表,然后从响应中提取当前的runid。

GET https://dev.azure.com/{organization}/{project}/_apis/test/runs?api-version=5.1

希望以上帮助。

© www.soinside.com 2019 - 2024. All rights reserved.