我知道您可以在 xml 中定义要运行的组,但我想知道如果它们都是组 A 和 B 的成员,是否可以说运行这些方法。
假设我有以下测试用例;
@Test(groups={"A","B"})
public testA() {}
@Test(groups={"B","C"})
public testB(){}
和以下配置;
<test name="Test A|B">
<groups>
<run>
<include name="B" />
</run>
</groups>
<classes>
<class name="com.test.Test" />
</classes>
</test>
这将同时运行 testA 和 testB,因为它们都是 B 组的成员。我只想在它同时是 A 组和 B 组的成员时运行测试。
有可能用 TestNG 做这样的事情吗?
提前致谢
最佳答案
您可以创建一个实现 IMethodInterceptor 接口(interface)的监听器。这将使您能够从 @Test 访问组列表并根据需要管理“要执行的测试列表”。同时 ITestContext 参数允许您访问来自 testNg xml 的数据。因此,您可以将组设置为以默认的 testNg 方式(套件 xml 文件)运行;但是根据您实现的算法运行它们。 像这样的东西:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.testng.IMethodInstance;
import org.testng.IMethodInterceptor;
import org.testng.ITestContext;
import org.testng.annotations.Test;
public class Interceptor implements IMethodInterceptor
{
@Override
public List<IMethodInstance> intercept(List<IMethodInstance> methods, ITestContext context)
{
int methCount = methods.size();
List<IMethodInstance> result = new ArrayList<IMethodInstance>();
for (int i = 0; i < methCount; i++)
{
IMethodInstance instns = methods.get(i);
List<String> grps = Arrays.asList(instns.getMethod().getConstructorOrMethod().getMethod().getAnnotation(Test.class).groups());
//get these groups from testng.xml via context method parameter
if (grps.contains("A") && grps.contains("B"))
{
result.add(instns);
}
}
return result;
}
}
关于java - 如果它是两个组的成员,是否可以为 TestNG 设置一个条件来运行测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23686636/