gurock / testrail-api

TestRail API: Examples on how to use TestRail's API from various languages
Other
116 stars 87 forks source link

"Field :suite_id is a required field." when suite_id is clearly provided #12

Closed mcshakes closed 5 years ago

mcshakes commented 6 years ago

Hey,

Just wanted guidance (or to report a bug) when trying to hook up the API to my testrail plan. Here is my custom code:


class TestrailConnect
  include HTTParty
  format :json

  base_uri 'http://testrail.wowzaquality.com/index.php?/api/v2'
  debug_output

  def initialize(username, password)
    @options = {
      basic_auth: {
        username: username,
        password: password
      },
      headers: {
        "Content-Type" => "application/json"
          }
    }
  end

  def add_cases_to_plan(plan_id)
    options = @options.merge({
        "suite_id": 422,
        "include_all": false,
        "runs": [
          "include_all": false,
          "case_ids": ["59361", "61217","61215"]
        ]
    })
    self.class.post("/add_plan_entry/#{plan_id}", options)
  end

end

testrail = TestrailConnect.new('edward.mccarthy@wowza.com', '#{API_KEY}')
p testrail.add_cases_to_plan(1541)

And the full errors I get:

<HTTParty::Response:0x7ff7231f06b8 parsed_response={"error"=>"Field :suite_id is a required field."}, @response=#<Net::HTTPBadRequest 400 Bad Request readbody=true>, @headers={"date"=>["Thu, 26 Apr 2018 16:54:49 GMT"], "server"=>["Apache/2.2.15 (CentOS)"], "x-powered-by"=>["PHP/5.4.45"], "content-length"=>["48"], "connection"=>["close"], "content-type"=>["application/json; charset=utf-8"]}>

davidbrazilparker commented 6 years ago

I see you're sending the case_ids as an array of Strings. It might be necessary that you wrap your suite_id with double quotes so that it is sent as a String in the request.

jonridera commented 5 years ago

As @davidbrazilparker mentioned, this is likely an issue with the formatting of your request itself. Any IDs submitted in an API request should be treated as integers, so improper formatting could lead to failures parsing the request and the 400 error you received.

Your "case_ids" array should be an array of integers, as shown in the example documentation here.

Regards, Jon

syedghayyour commented 2 years ago

Hi I am having the same problem here, however I am working with Java and I am also passing thge Suite ID: Here is my code:

int PROJECT_ID = 1;
Long SUITE_ID = (long) 1286;
APIClient client = null;

@BeforeSuite
public void createSuite(ITestContext ctx) throws IOException, APIException {
    client = new APIClient("https://testrail.net");
    client.setUser("test@test.com );
    client.setPassword("########");
    Map data = new HashMap();
    data.put("include_all",true);
    data.put("name","Test Run "+System.currentTimeMillis());
    JSONObject c = null;

    c = (JSONObject)client.sendPost("add_run/"+PROJECT_ID,data);
    long suite_id = SUITE_ID; 
    ctx.setAttribute("suiteID" ,suite_id);

}

@BeforeMethod
public void beforeTest(ITestContext ctx,Method method) throws NoSuchMethodException {
    Method m = TestNGProject.class.getMethod(method.getName());

    if (m.isAnnotationPresent(TestRail.class)) {
        TestRail ta = m.getAnnotation(TestRail.class);
        System.out.println(ta.id());
        ctx.setAttribute("caseId",ta.id());
    }
}

@AfterMethod
public void afterTest(ITestResult result, ITestContext ctx) throws IOException, APIException {
    Map data = new HashMap();
    if(result.isSuccess()) {
        data.put("status_id",1);
    }
    else {
        data.put("status_id", 5);
        data.put("comment", result.getThrowable().toString());
    }

    String caseId = (String)ctx.getAttribute("caseId");
    Long suiteId = (Long)ctx.getAttribute("suiteId");
    client.sendPost("add_result_for_case/"+suiteId+"/"+caseId,data);

}

Error Message:

RemoteTestNG] detected TestNG version 7.4.0 FAILED CONFIGURATION: @BeforeSuite createSuite(org.testng.TestRunner@1f2586d6) com.uitesting.testRail.APIException: TestRail API returned HTTP 400("Field :suite_id is a required field.")