TestNG Groups:我们可以包含两个组名并创建一个组来运行测试吗?

时间:2013-04-10 07:01:39

标签: java selenium selenium-webdriver selenium-rc testng

我有以下testng测试方法。

    @Test(groups = {"tsg1.0","smoke"})
public void testLoginWithInvalidCredentials(String usernameValue, String passwordValue){        
    /*Print something*/
}


@Test(groups = {"tsg1.0"})          
public void testLoginWithUserIdOnly(String username) {       
    /*Print something*/
}



@Test(groups = {"tsg1.0"})          
public void testLoginWithPasswordOnly(String password) {         
    /*Print something*/
}

以下是用于测试上述方法的testng xml。

<suite name="Suite" thread-count="1" verbose="10"> 
  <test name="Test">
  <groups>
<run>  
 <include name="tsg1.0"/>    
</run>
</groups>
<packages>  
<package name="<test package name>"/>       
 </packages> 
</test>  
</suite>

有没有办法可以创建一个包含“TSG1.0”和“SMOKE”组测试的xml。我想在这种情况下只运行第一个测试(testLoginWithInvalidCredentials)。

请帮忙。

谢谢, 麦克

PS:以下内容不起作用,因为它将包含tsg1.0或smoke。我想要一个条件......

<run>
<include name="tsg1.0"/>   
<include name="smoke"/>  
</run>

4 个答案:

答案 0 :(得分:4)

你实际上可以做到这一点&#34;半现成的&#34;: http://testng.org/doc/documentation-main.html#beanshell

在你的特定情况下,它会是这样的:

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

在这里更详细地回答了类似的问题: Is it possible to put a condition to TestNG to run the test if that is member of two groups?

答案 1 :(得分:3)

AFAIK在testng中没有上架。你可以做的就是编写你的methodinterceptor并只返回属于这两个组的thsoe方法列表。

您还可以从拦截方法的testcontext参数中获取包含的组。

您可以在此处参考更多信息:http://testng.org/doc/documentation-main.html#methodinterceptors

答案 2 :(得分:2)

您可以拥有Group of groups

Groups can also include other groups. These groups are called "MetaGroups".
For example, you might want to define a group "all" that includes "checkintest" 
and "functest"."functest" itself will contain the groups "windows" and "linux" 
while "checkintest will only contain "windows". 

属性文件示例:

<groups>
    <define name="functest">
      <include name="windows"/>
      <include name="linux"/>
    </define>

    <define name="all">
      <include name="functest"/>
      <include name="checkintest"/>
    </define>

    <run>
      <include name="all"/>
    </run>
  </groups>

答案 3 :(得分:1)

对于从cmd执行的多个TestNG组,您可以在TestNG.xml文件中使用以下脚本。

<method-selectors>
        <method-selector>
            <script language="beanshell"><![CDATA[
            Boolean runTest = false;
            String groupsName = System.getProperty("GROUPS");

            if (groupsName != null && groupsName != ""){
                StringTokenizer groupsTagList = new StringTokenizer(groupsName, ",");
                while (groupsTagList.hasMoreTokens()) {
                    if(groups.containsKey(groupsTagList.nextToken().trim()){
                        runTest = true;
                        break;
                    }
                }
            }else{
                runTest = true;
            } 
            return runTest;
            ]]>
            </script>
        </method-selector>
    </method-selectors>

从Maven执行:

mvn test -DGROUPS = groups1,groups2,groups3

它会正常工作......