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

送分小仙女□ 提交于 2019-12-11 02:25:43

问题


I have created a test run during the execution time, and I would like to add test cases meanwhile they are starting the execution. The test cases have been created, if they already doesn't exist. And this test case should be added to an existing test run with other test cases.

I have tried to use the setCaseIds over the run and after update the run, but that override the existing run. I think the error is because I'm using the setCaseIds, but I don't know the proper way to do.

Case mycase = new Case().setTitle("TEST TITLE").setSuiteId(suite.getId()).setSectionId(section.getId());
mycase = testRail.cases().add(mycase.getSectionId(), mycase, customCaseFields).execute();
final List<Integer> caseToAdd = new ArrayList();
caseToAdd.add(mycase.getId());
run.setCaseIds(caseToAdd);
run = testRail.runs().update(run).execute();
//The first test start the execution
.
.
.
// The first test case finish
// Now I create a new testcase to add
Case mySecondCase = new Case().setTitle("TEST TITLE").setSuiteId(suite.getId()).setSectionId(section.getId());
mycase = testRail.cases().add(mySecondCase.getSectionId(), mySecondCase, customCaseFields).execute();
// I repeat the prevous steps to add a new test case
final List<Integer> newCaseToAdd = new ArrayList();
newCaseToAdd.add(mySecondCase.getId());
    run.setCaseIds(newCaseToAdd);
    run = testRail.runs().update(run).execute();

Anyone knows how to do that? Thank you in advance.


回答1:


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




回答2:


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 Case
Case mycase = new Case().setTitle("TEST TITLE").setSuiteId(suite.getId()).setSectionId(section.getId());
mycase = testRail.cases().add(mycase.getSectionId(), mycase, customCaseFields).execute();
// List for Test Cases
List<Integer> caseList = new ArrayList<>();
caseList.add(mycase.getId());
// Create new Entry and add the test cases
Entry entry = new Entry().setIncludeAll(false).setSuiteId(suite.getId()).setCaseIds(caseList);
entry = testRail.plans().addEntry(testPlan.getId(),entry).execute();
// Create the second test case
Case 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 list
caseList.add(mycase2.getId());
// Set in the Entry all the test cases and update the Entry
entry.setCaseIds(caseList);
testRail.plans().updateEntry(testPlan.getId(), entry).execute();

To execute the test cases you need the test run:

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


来源:https://stackoverflow.com/questions/52819170/how-to-add-test-cases-to-an-existing-test-run-with-the-api-from-java-to-testrail

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!