1
votes

I'm trying to attach the log file that I have generated, during the run of my automated tests in the Azure Release pipeline. I am running Automated UI tests using selenium and MSTest. Originally, I was under the impression that I could attach the file in the AssemblyCleanup Method of MSTest, using the TestContext object. However, it seems that that TestContext can only be used for Attaching results to individual test cases? After Searching around the web a bit I found this API call:

POST https://dev.azure.com/{Organization}/{Project}/_apis/test/Runs/{runId}/attachments?api-version=5.1-preview.1

in the Microsoft Documentation :https://docs.microsoft.com/en-us/rest/api/azure/devops/test/attachments/create%20test%20result%20attachment?view=azure-devops-rest-5.1

However, I don't understand how to get ahold of the test runId in my test code or how to authenticate with OAuth2. Here is as far as I could get using RestSharp to make the call;

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);

Any help would be really great... I'm in a bit over my head here.

1
Hi @MitchelB123 Did you try out below code, How did it go? Please let me know if there is any question. - Levi Lu-MSFT

1 Answers

0
votes

Below is a code example showing how to call restful api in c# HttpClient using Personal Access Token. You can get your Personal Access Token by following the steps 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());
    }
}

Above example is from Microsoft document. You can check it out.

For test runId, you may need to call below test runs api to get a list of the test runs and then extract the current runid from the Response.

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

Hope above helps.