本文介绍了如果它是两个组的成员,是否可以为TestNG运行测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道你可以在你的xml中定义你想要运行的组,但是我想知道是否可以说如果它们都是A组和A组的成员就运行这些方法。 B。

I know you can define in your xml which groups that you want to run, but I want to know whether it is possible to say run these methods if they are both member of groups A & B.

假设我有以下测试用例;

Let's say I have the following test cases;

@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组和A组的成员时才进行测试。 B。

This will run both testA and testB since they are both members of group B. I want to run the test only if it is member of both groups A & B.

是否可以使用TestNG进行此类操作?

Is it possible to do such thing with TestNG?

提前致谢

推荐答案

您可以创建一个实现IMethodInterceptor接口的侦听器。这将使您能够从@Test访问组列表,并根据需要管理测试执行列表。同时,ITestContext参数允许您从testNg xml访问数据。因此,您可以将组设置为以默认的testNg方式运行(套件xml文件);但根据您实施的算法运行它们。
类似于:

You can create a listener implementing IMethodInterceptor interface. Which will give you an ability to access groups list from your @Test and manage your "tests to execute list" as you need. At same time ITestContext parameter allows you to access the data from testNg xml. So, you can set groups to run in default testNg manner (suite xml file); but run them depending on algorithm you implement.Something like:

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;
    }
}

这篇关于如果它是两个组的成员,是否可以为TestNG运行测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 05:06