-2

Hi I am having the some problem regarding the integration of TestNG with TestRail Test Suits. I am working with Java and every time I get error that the Test Suit ID is compulsory. I have also added the the Test suite ID however I am still having the same error message. Error message and code is written bellow:

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.")

2 Answers2

0

I am not sure about TestRail APIs but the case is failing in Beforesuite, where you are making a call to addRun endpoint, which seems counterintuitive. It has to be a createSuite call or something of that sort. The error also shows that the addrun (understandably) requires a suiteid.

niharika_neo
  • 8,441
  • 1
  • 19
  • 31
0

You need to add suite_id into POST payload in createSuite method as it is required parameter in case if project has multiple suites

@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();
// Add the line below:
data.put("suite_id", SUITE_ID);
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);
}

See the docs: https://www.gurock.com/testrail/docs/api/reference/runs#addrun

Sergi
  • 990
  • 5
  • 16