How to group all the tests in your suite?

TestNG has multiple annotations and limitless options. Grouping the tests is one among them.

Scenario: A big basket is having different kind of mobiles. Let us say there are Apple phones, Moto G, MI and Lenova.

I want to run tests only for MI and Apple phones. Let's solve this with the below program.

package seleniumWithTestNG;

import org.testng.annotations.Test;

public class GroupTest {

    @Test (groups = { "Apple" })
    public void apple1() {
        System.out.println("Test Apple device 1");
    }

    @Test (groups = { "Apple" })
    public void apple2() {
        System.out.println("Test Apple device 2");
    }

    @Test (groups = { "MI" })
    public void mi1() {
        System.out.println("Test MI device 1");
    }

    @Test (groups = { "MI" })
    public void mi2() {
        System.out.println("Test MI device 2");
    }

    @Test (groups = { "Moto" })
    public void motog1() {
        System.out.println("Test Moto device 1");
    }

    @Test (groups = { "Moto" })
    public void motog2() {
        System.out.println("Test Moto device 2");
    }

    @Test (groups = { "Lenova" })
    public void lenova1() {
        System.out.println("Test Lenova device 1");
    }

    @Test (groups = { "Lenova" })
    public void lenova2() {
        System.out.println("Test Lenova device 2");
    }
}

Before proceeding copy and paste the below code in the testng.xml file.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Test Suite" verbose="2">
    <test name="Group Test">
        <groups>
            <run>
                <include name="Apple" />
                <include name="MI" />
            </run>
        </groups>        
        <classes>
            <class name="seleniumWithTestNG.GroupTest" />
        </classes>
    </test>
</suite>

Run the testng.xml file as TesNG Suite

Output:

If you observe the above output only Apple and MI tests ran. That is because of the groups tag that we use in the testng.xml. I presume the code is self explanatory as you have come across several chapters. If you have any issues in understanding leave a comment.

HomeWork:

We have seen so many examples using TestNG, please go through the different types of annotations. This link explains the differences between TestNG and Junit.

results matching ""

    No results matching ""