6

I have following testng test methods.

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

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

Please help.

Thanks, Mike.

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

<run>
<include name="tsg1.0"/>   
<include name="smoke"/>  
</run>
rajesh
  • 3,247
  • 5
  • 31
  • 56
Mike
  • 899
  • 9
  • 27
  • 45

4 Answers4

5

You can actually do this "semi off the shelf": 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?

Community
  • 1
  • 1
mac
  • 2,672
  • 4
  • 31
  • 43
3

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

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

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

niharika_neo
  • 8,441
  • 1
  • 19
  • 31
  • Thanks. Is there a list of scenarios which are not supported by testng, like the one asked by the OP ? It would be nice to know what testng can do and can't do. Maybe even come up with cookbook kind of solutions to make testng support those scenarios. – MasterJoe May 13 '19 at 16:24
2

You can have a 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>
rajesh
  • 3,247
  • 5
  • 31
  • 56
  • 1
    You are right. But I don't think this solves my question. 'functest' will execute tests with group name: windows or linux... I am looking for something that will help me to execute windows AND linux. – Mike Apr 10 '13 at 07:12
  • @Mike This is the correct answer. That way you can execute both windows AND linux. – Lyuboslav Feb 06 '19 at 08:28
1

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

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

mvn test -DGROUPS=groups1,groups2,groups3

It will working fine...

Radadiya Nikunj
  • 988
  • 11
  • 10