Search code examples
c#azureazure-devopsazure-pipelinestfs-2018

Upload test result documents to Azure Test Plans or the Test hub in TFS 2018


I'm developing an API in one of my C# library to upload the test result documents (such as log file, screenshots captured or any zip file) from the local folder to the desired test case number under the test plan on Test hub. I'm working with TFS 2018.

Can anyone please help me with the code snippet to implement this functionality.

At present I'm able to establish the connection with the server with the below lines of code:

        VssClientCredentials vssClientCred = new VssClientCredentials();
        vssClientCred.Storage = new VssClientCredentialStorage();
        VssConnection connection = new VssConnection(new Uri("TestHubServerURL"), vssClientCred);

        TestManagementHttpClient tManageHttp = connection.GetClient<TestManagementHttpClient>();

        TestResultDocument tdoc = new TestResultDocument();
        TestResultDocument tRun = tManageHttp.PublishTestResultDocumentAsync(tdoc, ProjectName, TestRunID).Result;

But now I'm stuck, I'm not getting a way to to implement "PublishTestResultDocumentAsync" or do I need to use any other way to implement this functionality?

I tried googling out but didn't get any help with any examples.

Thank you all in advance.


Solution

  • I guess you meant to upload the test result doucuments to the test run result under the Runs tab in Test hub.

    enter image description here

    You can use CreateTestRunAttachmentAsync method to upload the test result doucuments to its test run. See below example:

    string teamProjectCollectionUrl = "http://tfs2018:8080/tfs/DefaultCollection";
    string Project = "projectName";
    VssConnection _connection = new VssConnection(new Uri(teamProjectCollectionUrl), winCred);
    
    TestManagementHttpClient tManageHttp = _connection.GetClient<TestManagementHttpClient>();
    string path = "C:\\test\\image.png";
    string stream = Convert.ToBase64String(File.ReadAllBytes(path));
    
    TestAttachmentRequestModel att = new TestAttachmentRequestModel(stream, "pic.png", "", null);
    
    var res = tManageHttp.CreateTestRunAttachmentAsync(att, Project, "runId").Result;
    

    If you want to upload the test results document to the result of a specific test case. You can use CreateTestResultAttachmentAsync method.

    var res = tManageHttp.CreateTestResultAttachmentAsync(att, Project, "runId", "resultId").Result;
    

    If you were trying to upload test result documents to the desired test case number under the test plan on Test hub. You probably need to use the CreateAttachmentAsync and UpdateWorkItemAsync methods in Microsoft.TeamFoundation.WorkItemTracking.WebApi, since the Test Case is a type of Work Item.