How to add test cases to an existing test run with the API from Java to TestRail? How to add test cases to an existing test run with the API from Java to TestRail? selenium selenium

How to add test cases to an existing test run with the API from Java to TestRail?


Here is what I was able to find:

  1. TestRail does not support an add/append operation. It only supports set/override operation. That is what happens in your case when you call setCaseIds two times on the same run it saves the last id only (and that is what you can typically expect from a set method).
  2. Suggested solution is:

Run activeRun = testRail.runs().get(1234).execute();List<Integer> testCaseIds = activeRun.getCaseIds() == null ? new ArrayList<>() : new ArrayList<>(activeRun.getCaseIds());testCaseIds.add(333);testRail.runs.update(activeRun.setCaseIds(testCaseIds)).execute();

So instead of just setting a new id(s) you take the existing ids from run, add id(s) to it and update run.

source:https://github.com/codepine/testrail-api-java-client/issues/24


I solved the problem with the Plan and Entry structure. I'm saving all the test cases in a list, and this list is passed as parameter in the entry.setCaseIds function:

// First Test CaseCase mycase = new Case().setTitle("TEST TITLE").setSuiteId(suite.getId()).setSectionId(section.getId());mycase = testRail.cases().add(mycase.getSectionId(), mycase, customCaseFields).execute();// List for Test CasesList<Integer> caseList = new ArrayList<>();caseList.add(mycase.getId());// Create new Entry and add the test casesEntry entry = new Entry().setIncludeAll(false).setSuiteId(suite.getId()).setCaseIds(caseList);entry = testRail.plans().addEntry(testPlan.getId(),entry).execute();// Create the second test caseCase mycase2 = new Case().setTitle("TEST TITLE 2").setSuiteId(suite.getId()).setSectionId(section.getId());mycase2 = testRail.cases().add(mycase.getSectionId(), mycase, customCaseFields).execute();// Add the second test case to the listcaseList.add(mycase2.getId());// Set in the Entry all the test cases and update the Entryentry.setCaseIds(caseList);testRail.plans().updateEntry(testPlan.getId(), entry).execute();

To execute the test cases you need the test run:

run = entry.getRuns().get(0);