java - 如果它是两个组的成员,是否可以为 TestNG 设置一个条件来运行测试?

标签 java selenium selenium-webdriver testng

我知道您可以在 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/

相关文章:

java - Netty - 如何最好地为具有大量 POJO 的桌面应用程序构建客户端/服务器

python - 如何配置 pytest 在生成测试时生成有用的名称?

Python selenium 在显示时单击元素

python - Selenium python find_element_by_class_name() 从 v 2.2 到 2.21 停止工作——不能使用 'Compound Class Name'

java - JAVA PriorityQueue 顺序不正确

java - 是否可以将这两行写为三元运算符?

java - 如何在 Spring MVC 中使用自定义日期属性编辑器验证日期

java - 使用相同的 Firefox 窗口在 Selenium WebDriver (Java) 中运行多个测试

python - 使用 Python 使用 Headless Selenium 浏览器提取数据

python - 消息: unknown error: Chrome failed to start: exited abnormally on AWS Cloud9 with Linux 4. 9.85-38.58.amzn1.x86_64 x86_64