Is it possible to put a TestNG condition for running a test if it is a member of two groups?

I know that you can define in your xml which groups you want to run, but I want to know if these methods can be run if they are both members of groups A and B.

Let's say I have the following test cases:

@Test(groups={"A","B"})
public testA() {}

@Test(groups={"B","C"})
public testB(){}

and the following configuration;

<test name="Test A|B">
<groups>
       <run>
           <include name="B" />
       </run>
    </groups>

    <classes>
        <class name="com.test.Test" />
    </classes>
</test>

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

Can this be done with TestNG?

Thanks in advance

+4
source share
3 answers

, IMethodInterceptor. @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;
    }
}
+4

Beanshell TestNG: http://testng.org/doc/documentation-main.html#beanshell ( , testngMethod )

:

<method-selectors>
  <method-selector>
    <script language="beanshell"><![CDATA[
       return groups.containsKey("A") && groups.containsKey("B");
     ]]></script>
  </method-selector>
</method-selectors>

System.out.println(), .

, "-" "". - , "-" "" ( ), .

System.out.println() , , , :

<method-selectors>
  <method-selector>
    <script language="beanshell"><![CDATA[
      runMethod = groups.containsKey("A") && groups.containsKey("B");
      System.out.println("run '" + testngMethod.getMethodName() + "'? -> " + runMethod);
      return runMethod;
    ]]></script>
  </method-selector>
</method-selectors>
+3

?

<test name="Test A|B">
<groups>
       <run>
           <include name="A" />
            <include name="B" />
       </run>
    </groups>

    <classes>
        <class name="com.test.Test" />
    </classes>
</test>

, "" , 3 ( ), , , .

0
source

Source: https://habr.com/ru/post/1540773/


All Articles