java - TestNg 分组
问题描述
我们使用 testNg 作为我们的测试框架,其中我们使用 @groups 注释来运行特定的测试。我们如何自定义此注释以使构建目标中指定的组在“与”而不是“或”中考虑。
@groups({'group1', 'group2'})
public void test1
@groups({'group1', 'group3'})
public void test2
如果我们将组传递为{group1,group2}
,那么test1
应该是唯一被触发的测试用例。目前,使用默认实现都test1
被test2
触发,因为注释将两个组都考虑在“或”而不是“和”中
解决方案
当前的实现是这样的,如果测试方法中指定的任何组与套件文件中指定的任何组匹配,则包含测试以运行。
目前没有规定将这种“任何匹配”的实现更改为“所有匹配”。但是您可以使用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 {
}
推荐阅读
- go - 纵梁工具是否有“反向”功能?
- java - 应用程序完全关闭时如何在android中保存暗模式状态
- javascript - 如何在javascript中更改滑块拇指的背景颜色?
- python - 如何使用 rpy2 抑制 python 打印代码以汇总模型
- c++ - 不同对象但相同父对象的向量
- algorithm - 在格子上随机布局对象的算法
- list - 有条件地附加到 Elixir 中的列表?
- c++ - mkoctfile 找不到包含 c++ 库头文件
- javascript - 尝试将 MERN 应用程序部署到 heroku 时出现错误
- module - 为什么我的主管使用 undef 在 start_child 上失败?