Sunday 15 March 2020

TestNG: Group of groups

In testing, a group can include other groups. For example, we can combine all memory and performance related test cases to ‘stressTest’ group,  and all unit, service layer tests to ‘regressionTest’ group.

Example
<groups>

        <define name="stressTest">
                <include name="performanceTest" />
                <include name="memoryTest" />
        </define>

        <define name="regressionTest">
                <include name="serviceTest" />
                <include name="unitTest" />
        </define>

        <run>
                <include name="stressTest" />
        </run>
</groups>

Find the below working application.

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

import org.testng.annotations.Test;

public class GoupOfGroupTest {
        @Test(groups = { "performanceTest" })
        public void performanceTest_1() {
                System.out.println("performanceTest 1");
        }

        @Test(groups = { "performanceTest" })
        public void performanceTest_2() {
                System.out.println("performanceTest 2");
        }

        @Test(groups = { "unitTest" })
        public void unitTest_1() {
                System.out.println("unitTest 1");
        }

        @Test(groups = { "unitTest" })
        public void unitTest_2() {
                System.out.println("unitTest 2");
        }

        @Test(groups = { "serviceTest" })
        public void serviceTest_1() {
                System.out.println("serviceTest 1");
        }

        @Test(groups = { "serviceTest" })
        public void serviceTest_2() {
                System.out.println("serviceTest 2");
        }

        @Test(groups = { "memoryTest" })
        public void memoryTest_1() {
                System.out.println("memoryTest 1");
        }

        @Test(groups = { "memoryTest" })
        public void memoryTest_2() {
                System.out.println("memoryTest 2");
        }

}

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

                        <define name="stressTest">
                                <include name="performanceTest" />
                                <include name="memoryTest" />
                        </define>

                        <define name="regressionTest">
                                <include name="serviceTest" />
                                <include name="unitTest" />
                        </define>

                        <run>
                                <include name="stressTest" />
                        </run>
                </groups>

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

How to run the application?
Right click on ‘groupOfGroups.xml’ file -> Run As -> TestNG Suite.


You will get below messages in console.

[RemoteTestNG] detected TestNG version 7.0.0
memoryTest 1
memoryTest 2
performanceTest 1
performanceTest 2

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


Previous                                                    Next                                                    Home

No comments:

Post a Comment