【问题标题】:TestNG tests multiple ids (as an array) in a single annotation of JavaTestNG 在 Java 的单个注释中测试多个 id(作为数组)
【发布时间】:2016-05-03 00:06:39
【问题描述】:

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

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 和结果时...我想知道正确的做法是什么?

【问题讨论】:

    标签: java testing annotations automated-tests testng


    【解决方案1】:

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

    在这种情况下,您可以使用 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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-01
      • 1970-01-01
      相关资源
      最近更新 更多