【问题标题】:Is it possible to put a condition to TestNG to run the test if that is member of two groups?如果 TestNG 是两个组的成员,是否可以为 TestNG 设置条件来运行测试?
【发布时间】:2014-07-04 09:30:51
【问题描述】:

我知道您可以在 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可以做这样的事情吗?

提前致谢

【问题讨论】:

    标签: java selenium selenium-webdriver testng


    【解决方案1】:

    你试过了吗?

    <test name="Test A|B">
    <groups>
           <run>
               <include name="A" />
                <include name="B" />
           </run>
        </groups>
    
        <classes>
            <class name="com.test.Test" />
        </classes>
    </test>
    

    如果这不起作用,您可以通过在您的类中使用 3 个测试方法(打开保留顺序)来“破解”它,并让前 2 个测试在条件上设置一个标志,然后是第 3 个测试仅在设置了两个条件时才运行。

    【讨论】:

    • 我没有尝试过,但根据定义,它应该运行第一次测试两次,第二次测试一次。不过我明天会试一试。第二种选择可能只有在进行几次测试时才有效。这是一个巨大的项目,我们有数百个测试用例。感谢您提出一些想法。
    • 您可以在 BaseClass 中使用 BeforeMethod 注释方法,该方法设置多个标志来控制在您的测试类中运行的内容。这样你就可以隐藏一些逻辑?只是一个想法。
    【解决方案2】:

    您可以创建一个实现 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;
        }
    }
    

    【讨论】:

    • 好方法!另一种选择也可以通过实现 iannotationtransformer。
    • @niharika_neo 据我了解,IAnnotationTransformer 不允许访问外部数据(例如 testng.xml 测试上下文)。因此,在这种特殊情况下,组名称应该是硬编码的,恕我直言,这不是一个很好的方法。如果我错了,请纠正我...
    • 谢谢大家。 @niharika_neo 我也会看看那个。
    • 这是正确的,当然它不应该被硬编码:) ..并为没有填写点而道歉..这也将涉及 itestlistener 获取测试上下文并设置组
    • 再想一想,这似乎是一个更好的解决方案,因为该解决方案不会分布在多个地方。 :)
    【解决方案3】:

    您可以使用 TestNG beanshell 支持来做到这一点: 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-19
      • 2020-06-03
      • 2015-11-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多