如何使用从 Java 到 TestRail 的 API 将测试用例添加到现有测试 运行?

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

我在执行期间创建了一个测试运行,我想在它们开始执行的同时添加测试用例。测试用例已经创建,如果它们不存在的话。并且此测试用例应与其他测试用例一起添加到现有测试 运行。

我曾尝试在 运行 上使用 setCaseIds,并在更新 运行 之后尝试使用 setCaseIds,但这会覆盖现有的 运行。我认为错误是因为我正在使用 setCaseIds,但我不知道正确的方法。

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

有人知道怎么做吗?提前谢谢你。

这是我能够找到的内容:

  1. TestRail 不支持 add/append 操作。它只支持 set/override 操作。当您在同一个 运行 上两次调用 setCaseIds 时,这就是您的情况,它只保存最后一个 ID(这是您通常可以从 set 方法中得到的)。
  2. 建议的解决方案是:

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

因此,除了设置新 ID,您还可以从 运行 获取现有 ID,向其添加 ID 并更新 运行。

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

我解决了计划和条目结构的问题。我将所有测试用例保存在一个列表中,这个列表作为参数传递给 entry.setCaseIds 函数:

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

要执行测试用例,您需要测试 运行:

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