Akshay prakash
Akshay prakash

Reputation: 13

How to run multiple combinations of groups in a single TestNG test

I need to execute all 3 classes in single test with following requirements:

  1. Execute only test1 and test3 from Class1.
  2. Execute only test2 from Class2.
  3. Execute all tests from Class3.

    public class Class1
    {
    @Test(groups={"test1"})
    public void test1()
    {
    system.out.println("test1");
    }
    @Test(groups={"test2"})
    public void test2()
    {
    system.out.println("test2");
    }
    @Test(groups={"test3"})
    public void test3()
    {
    system.out.println("test3");
    }
    }
    

Class 2

    public class Class2
    {
    @Test(groups={"test1"})
    public void test_1()
    {
    system.out.println("test1");
    }
    @Test(groups={"test2"})
    public void test_2()
    {
    system.out.println("test2");
    }
    @Test(groups={"test3"})
    public void test_3()
    {
    system.out.println("test3");
    }
    }

Class 3

    public class Class3
    {
    @Test(alwaysrun="true")
    public void test()
    {
    system.out.println("test");
    }
    }

For this I used following testNg xml but all the tests in Class 1 and Class 3 are executed irrespective of whether I included the group or not.

    <test name="Testcase">
    <classes>
        <groups>
            <run>
                <include name="test1" />
                <exclude name="test2" />
                <include name="test3" />
                    <class name="Class1" />
            </run>
        </groups>
        <groups>
            <run>
                <exclude name="test1" />
                <include name="test2" />
                <exclude name="test3" />
                    <class name="Class2" />
            </run>
        </groups>
        <groups>
            <run>
            </run>
            <class name="Class3" />
        </groups>       
    </classes>
    </test>

Please suggest what should be the correct syntax of TestNg xml file if I wish execute class files as mentioned above.

Upvotes: 0

Views: 3085

Answers (1)

Gaurang Shah
Gaurang Shah

Reputation: 12950

Your XML file contains the problem: class should not be part of groups and groups is not part of classes.

Also your code has compilation issues. The simplest solution would be to annotate the test or test class (you can annotate the whole class as Test and provide a group name) and then run only that specific group.

Example:

Class 1

public class Class1
{
    @Test(groups={"smoke"})
    public void test1(){
        System.out.println("test1");
    }

    @Test(groups={"func"})
    public void test2(){
        System.out.println("test2");
    }

    @Test(groups={"smoke"})
    public void test3(){
        System.out.println("test3");
    }
}

Class 2

public class Class2
{
    @Test(groups={"func"})
    public void test_1(){
        System.out.println("test1");
    }
    @Test(groups={"smoke"})
    public void test_2(){
        System.out.println("test2");
    }
    @Test(groups={"func"})
    public void test_3(){
        System.out.println("test3");
    }
}

Class 3

@Test(groups = {"smoke"})
public class Class3
{
    @Test(alwaysRun=true)
    public void test(){
        System.out.println("test");
    }
}

testng.xml

<suite name="Regression" >

<test name="Test1">
    <groups>
        <run>
            <include name="smoke"/>
        </run>
    </groups>
    <classes>
        <class name="Class1"/>
        <class name="Class2"/>
        <class name="Class3"/>
    </classes>
</test>
</suite>

Upvotes: 1

Related Questions