Sunday 15 March 2020

TestNG: Execute test cases from specific group or groups

You can execute test methods that belongs to specific group or groups using suite xml file.

By specifying the test cases and their groups information in an xml file, you can exclude or include tests of specific groups.

GroupTest.java
package com.sample.app.tests;

import org.testng.annotations.Test;

public class GroupTest {
        @Test(groups = { "group1" })
        public void group1A() {
                System.out.println("I am in group1 test A");
        }

        @Test(groups = { "group1" })
        public void group1B() {
                System.out.println("I am in group1 test B");
        }

        @Test(groups = { "group2" })
        public void group2A() {
                System.out.println("I am in group2 test A");
        }

        @Test(groups = { "group2" })
        public void group2B() {
                System.out.println("I am in group2 test B");
        }

        @Test(groups = { "group3" })
        public void group3A() {
                System.out.println("I am in group3 test A");
        }

        @Test(groups = { "group3" })
        public void group3B() {
                System.out.println("I am in group3 test B");
        }
}

Create an xml file ‘groups.xml’ in the same package where the test class is located.

groups.xml
<?xml version="1.0" encoding="UTF-8" ?> 
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
        <test name="Groups smoke test demo">
                <groups>
                        <run>
                                <include name="group1" />
                                <include name="group3" />
                        </run>
                </groups>

                <classes>
                        <class name="com.sample.app.tests.GroupTest" />
                </classes>
        </test>
</suite>        

How to run the tests from xml file?
Right click on groups.xml -> Run As -> TestNG Suite.




You will get below messages in console.

[RemoteTestNG] detected TestNG version 7.0.0
I am in group1 test A
I am in group1 test B
I am in group3 test A
I am in group3 test B

===============================================
Suite
Total tests run: 4, Passes: 4, Failures: 0, Skips: 0
===============================================

How to exclude group from testing?
Using ‘exclude’ element, you can exclude specific group or groups from testing.
For example, below statement exclude the test cases that belong to the group ‘group2’ from testing.

<exclude name="group2" />

groupsExclude.xml
<?xml version="1.0" encoding="UTF-8" ?> 
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
        <test name="Groups smoke test demo">
                <groups>
                        <run>
                                <exclude name="group2" />
                        </run>
                </groups>

                <classes>
                        <class name="com.sample.app.tests.GroupTest" />
                </classes>
        </test>
</suite>



Previous                                                    Next                                                    Home

No comments:

Post a Comment