TestNG 在 Java 的单个注释中测试多个 id(作为数组)

TestNG tests multiple ids (as an array) in a single annotation of Java

我有一个 TestInfo 接口用于注释 TestNG 测试,如下所示:

public @interface TestInfo {

    /**
     * Test case ID
     */
    public String[] id();

    boolean deploy() default true;

}

在上面的例子中,id()是一个String类型的数组(String[ ])。现在我的 testng 测试看起来像这样,例如:

@TestInfo(id={"C9114", "C9115"})
@Test 
public class testTrial() {
...something
}

如何读取此注释数组并在 for 循环中处理每个 ids。例如,我可以想到像 get the test method 这样的方法,然后注释并检查每个 id,如下所示...

Map<Object, Object> map = new HashMap<Object, Object>();
        Method method = result.getMethod().getConstructorOrMethod().getMethod();
        TestInfo annotation = method.getAnnotation(TestInfo.class);
        int status = 0;
        try {
            if (annotation!=null) {

            for(;;/*each id*/){     

                    map.put("id",annotation.id().substring(1));

                    switch (status) {
                    case ITestResult.SUCCESS:
                        map.put("result", STATUS.PASSED.getValue());
                    case ITestResult.FAILURE:
                        map.put("result", STATUS.AUTO_FAIL.getValue());
                    case ITestResult.SKIP:
                        map.put("result", STATUS.AUTO_SKIPPED.getValue());
                    default:
                        map.put("result", STATUS.UNTESTED.getValue());
                    }
                    ApiIntegration.addTestResult(map);

            }
}

因为我正在尝试存储与测试关联的 ID 和该 ID 的结果...我想知道正确的做法是什么?

如果我没理解错的话,问题是关于数据结构的,不是吗?

在这种情况下,您可以使用 Guava com.google.common.collect.ListMultimap

    ListMultimap<String, String> map = ArrayListMultimap.create();
    for (String id : annotation.ids()) {
        map.put(ITestResult.SUCCESS, id);
    }

    // getting all passed
    List<String> passedTestIds = map.get(ITestResult.SUCCESS);