【问题标题】:How to add test results in Azure DevOps through REST API java?如何通过 REST API java 在 Azure DevOps 中添加测试结果?
【发布时间】:2019-01-02 10:38:20
【问题描述】:

我试过下面的程序,但测试结果没有添加到 VSTS 中:

public static void addTestResults() throws ClientProtocolException, IOException {
     String pat = "abc@abc.com:xxxxxxxxxxxxxxxxxxxxxx";
     byte[] byteArray2 = Base64.encodeBase64(pat.getBytes());
     String encodedString2 = new String(byteArray2);

     HttpClient httpClient = HttpClientBuilder.create().build();
     HttpPost postRequest = new HttpPost("https://xxx.visualstudio.com/DefaultCollection/XXX/_apis/test/runs/1000012/results?api-version=5.0-preview.5" );
     List<NameValuePair> arguments1 = new ArrayList<NameValuePair>(4);
     arguments1.add(new BasicNameValuePair("state", "Completed"));
     arguments1.add(new BasicNameValuePair("testPoint", "{\"id\":40}"));
     arguments1.add(new BasicNameValuePair("outcome", "Passed"));
     arguments1.add(new BasicNameValuePair("testCase", "{\"id\":7340}"));

     try {
         postRequest.setHeader(HttpHeaders.CONTENT_TYPE, "application/json");
         patchRequest.setHeader(HttpHeaders.ACCEPT, "application/json");
         postRequest.setHeader("Authorization", "Basic "+encodedString2);
         postRequest.setEntity(new UrlEncodedFormEntity(arguments1,"UTF-8"));

         HttpResponse response = httpClient.execute(postRequest);
         System.out.println("Out is"+EntityUtils.toString(response.getEntity()));

     }    catch (IOException e) {
         e.printStackTrace();
     }
}

以下是输出:

Out is
{  
"$id":"1",
"innerException":null,
"message":"Value cannot be null.\r\nParameter name: results",
"typeName":"System.ArgumentNullException, mscorlib",
"typeKey":"ArgumentNullException",
"errorCode":0,
"eventId":0
}

谁能帮我找出哪里出错了。我试图解决它几天但徒劳无功。

【问题讨论】:

  • 看来,您的 json 无效。您需要获取正确的请求示例并与您发送的最终请求正文进行比较。如果您可以在此处提供此示例,将更容易发现问题。
  • POST dev.azure.com/fabrikam/Fabrikam-Fiber-TFVC/_apis/test/Runs/24/… 请求正文:[ {“testCaseTitle”:“VerifyWebsiteTheme”,“automatedTestName”:“FabrikamFiber.WebSite.TestClass.VerifyWebsiteTheme”,“优先级”:1,“结果”:“通过“},{“testCaseTitle”:“VerifyWebsiteLinks”,“automatedTestName”:“FabrikamFiber.WebSite.TestClass.VerifyWebsiteLinks”,“优先级”:2,“结果”:“失败”,“关联错误”:[{“id” : 30 } ] } ]
  • 在请求中发送的请求体呢?你能得到正在发送的最终请求吗?您可以使用调试来获取它。

标签: java selenium-webdriver azure-devops azure-devops-rest-api


【解决方案1】:

我得到了解决方案,这里是:

public static void addTestResults() throws ClientProtocolException, IOException {
     String pat = "abc@abc.com:xxxxxxxxxxxxxxxxxxxxxx";
     byte[] byteArray2 = Base64.encodeBase64(pat.getBytes());
     String encodedString2 = new String(byteArray2);
    String reqbody = "{\n" + 
            "        \"state\": \"Completed\",\n" + 
            "        \"testPoint\": {\n" + 
            "          \"id\": 40\n" + 
            "        },\n" + 
            "        \"outcome\": \"Passed\",\n" + 
            "        \"testCase\": {\n" + 
            "          \"id\": 7390\n" + 
            "        }\n" + 
            "      }";
     HttpClient httpClient = HttpClientBuilder.create().build();
     HttpPost postRequest = new HttpPost("https://xxx.visualstudio.com/DefaultCollection/XXX/_apis/test/runs/1000012/results?api-version=5.0-preview.5" );


     try {
         postRequest.setHeader(HttpHeaders.CONTENT_TYPE, "application/json");
         patchRequest.setHeader(HttpHeaders.ACCEPT, "application/json");
         postRequest.setHeader("Authorization", "Basic "+encodedString2);
         postRequest.setEntity(new StringEntity(reqbody, ContentType.APPLICATION_JSON));

         HttpResponse response = httpClient.execute(postRequest);
         System.out.println("Out is"+EntityUtils.toString(response.getEntity()));

     }    catch (IOException e) {
         e.printStackTrace();
     }
}

【讨论】:

  • 太棒了!感谢您在这里分享您的解决方案,您可以Accept it as an Answer,这样可以帮助遇到相同问题的其他社区成员,我们可以关闭此线程,谢谢。
猜你喜欢
  • 1970-01-01
  • 2019-04-06
  • 2021-11-13
  • 2021-08-29
  • 1970-01-01
  • 2021-04-20
  • 1970-01-01
  • 1970-01-01
  • 2021-02-27
相关资源
最近更新 更多