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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15919091/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 21:17:27  来源:igfitidea点击:

TestNG Groups: Can we include two group names and create one group to run tests?

javaseleniumselenium-webdriverselenium-rctestng

提问by Mike

I have following testng test methods.

我有以下 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*/
}

Here is the testng xml used to test the above methods.

这里是用于测试上述方法的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>

Is there a way where in I can create one xml which will include tests with Groups "TSG1.0" AND "SMOKE". I want only the first test(testLoginWithInvalidCredentials) to be run in this case.

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

Please help.

请帮忙。

Thanks, Mike.

谢谢,迈克。

PS: The following won't work as It will include tsg1.0 or smoke. I want an and condition here...

PS:以下将不起作用,因为它将包括 tsg1.0 或烟雾。我想要一个和条件在这里...

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

回答by mac

You can actually do this "semi off the shelf": http://testng.org/doc/documentation-main.html#beanshell

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

In your particular case, it would be something like this:

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

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

Just answered a similar question in more detail here: Is it possible to put a condition to TestNG to run the test if that is member of two groups?

刚刚在这里更详细地回答了一个类似的问题: 如果它是两个组的成员,是否可以向 TestNG 设置一个条件来运行测试?

回答by niharika_neo

AFAIK that doesn't come off the shelf in testng. What you can probably do is write your methodinterceptor and return only the list of thsoe methods which fall in both the groups..

AFAIK 在 testng 中没有下架。您可能可以做的是编写您的方法拦截器并仅返回属于这两个组的方法列表。

You can also get the includedgroups from the testcontext param of the intercept method.

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

You can refer here for more : http://testng.org/doc/documentation-main.html#methodinterceptors

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

回答by rajesh

You can have a Group of groups.

你可以有一个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". 

A property file sample:

属性文件示例:

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

回答by Radadiya Nikunj

For Multiple TestNG groups execution from cmd you can use following script in your TestNG.xml file.

对于从 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>

Execute From Maven:

从 Maven 执行:

mvn test -DGROUPS=groups1,groups2,groups3

mvn test -DGROUPS=groups1,groups2,groups3

It will working fine...

它会正常工作...