1

我们使用 testNg 作为我们的测试框架,其中我们使用 @groups 注释来运行特定的测试。我们如何自定义此注释以使构建目标中指定的组在“与”而不是“或”中考虑。

@groups({'group1', 'group2'})
public void test1

@groups({'group1', 'group3'})
public void test2

如果我们将组传递为{group1,group2},那么test1应该是唯一被触发的测试用例。目前,使用默认实现都test1test2触发,因为注释将两个组都考虑在“或”而不是“和”中

4

1 回答 1

0

当前的实现是这样的,如果测试方法中指定的任何组与套件文件中指定的任何组匹配,则包含测试以运行。

目前没有规定将这种“任何匹配”的实现更改为“所有匹配”。但是您可以使用IMethodInterceptor.

(此外,对于有问题的具体示例,您可以通过提及group3作为排除组来实现预期的结果。但这在许多其他情况下不起作用)。

import java.util.Arrays;

public class MyInterceptor implements IMethodInterceptor {
    
    @Override
    public List<IMethodInstance> intercept(List<IMethodInstance> methods, ITestContext context) {
        String[] includedGroups = context.getIncludedGroups();
        Arrays.sort(includedGroups);

        List<IMethodInstance> newList = new ArrayList<>();
        for(IMethodInstance m : methods) {
            String[] groups = m.getMethod().getGroups();
            Arrays.sort(groups);

            // logic could be changed here if exact match is not required.
            if(Arrays.equals(includedGroups, groups)) {
                newList.add(m);
            }
        }

        return newList;
    }
}    

然后在您的测试类之上,使用@Listeners注释。

@Listeners(value = MyInterceptor.class)
public class MyTestClass {
}
于 2021-07-21T06:55:50.307 回答