简体   繁体   English

如何通过REST API java在Azure DevOps中添加测试结果?

[英]How to add test results in Azure DevOps through REST API java?

I've tried with program below but test results are not added in VSTS: 我已尝试使用以下程序,但测试结果未添加到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();
     }
}

Following is the output: 以下是输出:

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
}

Can anyone help me out where I am going wrong. 任何人都可以帮我解决我的错误。 I am trying to solve it for days but in vain. 我试图解决它好几天但是徒劳无功。

I got the solution and here it is: 我得到了解决方案,这里是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM