Saturday, July 6, 2024

Define Groups and Include at "Suite" and "Test" Levels in testng.xml File:

 Define Groups and Include at "Suite" and "Test" Levels in testng.xml File:

You can define group names for @Test methods and run tests based on these group names using TestNG.

For example, you can run tests for "smoke" or "regression" groups.

ex:   run tests - smoketest

                           regression test  --   regression

 

            package TestNGBasics1;

import org.testng.annotations.Test;

//

public class TestNGWithGroupName

{

            package TestNgBasics1;

import org.testng.annotations.Test;

//TestNgBasics1.TestNGWithGroupName

public class TestNGWithGroupName

{

            // Define group name= "smoke" for testA method

                        @Test(groups = "smoke")

                        public void testA()

                        {

                                    System.out.println("smoke test A - smt-1");

                                    System.out.println("smoke test A - smt-2");

                        }

                       

                        // Define group name= "smoke" for testB method

                        @Test(groups = "smoke")

                        public void testB()

                        {

                                    System.out.println("smoke  test B - smt-1");

                                    System.out.println("smoke  test B - smt-2");

                        }

                       

                        // Define group name= "regression" for testC method

                        @Test(groups = "regression")

                        public void testC()

                        {

                                    System.out.println("regression test C - smt-1");

                                    System.out.println("regression test C - smt-2");

                        }

           

}

}

           

           

o/p:

--------------------------------

Execute  'smoke' tests only  from Testng.xml :

--------------------------------------------

or Defining "Groups" tag at "suite" level :

 

<?xml version="1.0" encoding="UTF-8"?>

<suite name= "My suite">

  <groups>

            <run>

                        <include name ="smoke"></include>

            </run>

  </groups>

            <test name="My test">

                        <classes>

                                    <class name="TestNgBasics1.TestNGWithGroupName">  </class>

                        </classes>      

            </test>

</suite>

 

o/p:

[RemoteTestNG] detected TestNG version 7.0.0

[TestNGContentHandler] [WARN] It is strongly recommended to add "<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >" at the top of your file, otherwise TestNG may fail or not work as expected.

smoke test A - smt-1

smoke test A - smt-2

smoke  test B - smt-1

smoke  test B - smt-2

===============================================

My suite

Total tests run: 2, Passes: 2, Failures: 0, Skips: 0

===============================================

Execute Test methods based on group name = "Regression"?

<?xml version="1.0" encoding="UTF-8"?>

<suite name= "My suite">

  <groups>

            <run>

                        <include name ="regression"></include>

            </run>

  </groups>

            <test name="My test">

                        <classes>

                                    <class name="TestNgBasics1.TestNGWithGroupName">  </class>

                        </classes>      

            </test>

</suite>

 

o/p:

[RemoteTestNG] detected TestNG version 7.0.0

[TestNGContentHandler] [WARN] It is strongly recommended to add "<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >" at the top of your file, otherwise TestNG may fail or not work as expected.

regression test C - smt-1

regression test C - smt-2

===============================================

My suite

Total tests run: 1, Passes: 1, Failures: 0, Skips: 0

===============================================

// HW Define class with different  group names  and create testng.xml file and Run specific groups from testng.xml file ?

 

Defining group name ="smoke" in 2 classes:

 

package TestNGBasics1;

import org.testng.annotations.Test;

//

public class TestNGWithGroupName2

{

            // Define group name = "smoke"

            @Test

            public void testA()

            {

                        System.out.println("TestNGWithGroupName2 -smoke test- smt-1");

                        System.out.println("TestNGWithGroupName2 -smoke test- smt-2");

            }

           

            // Define group name = "smoke"      

            @Test

            public void testB()

            {

                        System.out.println("TestNGWithGroupName2 smoke  test B - smt-1");

                        System.out.println("TestNGWithGroupName2 smoke  test B - smt-2");

            }

                       

            // Define group name = "regression"

            @Test

            public void testC()

            {

                        System.out.println("TestNGWithGroupName2 regression test C - smt-1");

                        System.out.println("TestNGWithGroupName2 regression test C - smt-2");

            }

           

}

           

           

o/p:

run smoke group  from 2 classes:

<?xml version="1.0" encoding="UTF-8"?>

<suite name= "My suite">

  <groups>

            <run>

                        <include name ="smoke"></include>

            </run>

  </groups>

            <test name="My test">

                        <classes>

                                    <class name="TestNgBasics1.TestNGWithGroupName">  </class>

                                    <class name="TestNgBasics1.TestNGWithGroupName2"> </class>

                                   

                        </classes>      

            </test>

</suite>

o/p:

----

[RemoteTestNG] detected TestNG version 7.0.0

[TestNGContentHandler] [WARN] It is strongly recommended to add "<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >" at the top of your file, otherwise TestNG may fail or not work as expected.

smoke test A - smt-1

smoke test A - smt-2

smoke  test B - smt-1

smoke  test B - smt-2

TestNGWithGroupName2 -smoke test- smt-1

TestNGWithGroupName2 -smoke test- smt-2

TestNGWithGroupName2 smoke  test B - smt-1

TestNGWithGroupName2 smoke  test B - smt-2

===============================================

My suite

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

reviewed

HW Run test method with Group name = "Regression" from 'TestWithGroups1' class and 'TestWithGroups2' class

rum from testng.xml file ?

Groups

  run

            include

Exclude 'smoke' test at 'suite' Level:

testng.xml

Groups

  run

            exclude

--------------

<?xml version="1.0" encoding="UTF-8"?>

<suite name= "My suite">

  <groups>

            <run>

                        <exclude name ="smoke"></exclude>

            </run>

  </groups>

            <test name="My test">

                        <classes>

                                    <class name="TestNgBasics1.TestNGWithGroupName">  </class>

                                    <!-- <class name="TestNgBasics1.TestNGWithGroupName2"> </class> -->

                                   

                        </classes>      

            </test>

</suite>

o/p:

[RemoteTestNG] detected TestNG version 7.0.0

[TestNGContentHandler] [WARN] It is strongly recommended to add "<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >" at the top of your file, otherwise TestNG may fail or not work as expected.

regression test C - smt-1

regression test C - smt-2

TestNGWithGroupName2 regression test C - smt-1

TestNGWithGroupName2 regression test C - smt-2

===============================================

My suite

Total tests run: 2, Passes: 2, Failures: 0, Skips: 0

===============================================

HW Exclude 'regression' test at "suite" Level:

--------------------------------------

smoke   --  Run only

Regression

Groups "include" at 'Test'tag  Level:

-----------------------------

testng.xml

-------------

<?xml version="1.0" encoding="UTF-8"?>

<suite name="My suite">

 

            <test name="My test">

                        <groups>

                                    <run>

                                                <include name="smoke"></include>

                                    </run>

                        </groups>

                       

                        <classes>

                                    <class name="TestNgBasics1.TestNGWithGroupName">

                                    </class>

                                    <!-- <class name="TestNgBasics1.TestNGWithGroupName2"> </class> -->

                        </classes>

            </test>

</suite>

o/p:

----

[RemoteTestNG] detected TestNG version 7.0.0

[TestNGContentHandler] [WARN] It is strongly recommended to add "<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >" at the top of your file, otherwise TestNG may fail or not work as expected.

smoke test A - smt-1

smoke test A - smt-2

smoke  test B - smt-1

smoke  test B - smt-2

===============================================

My suite

Total tests run: 2, Passes: 2, Failures: 0, Skips: 0

===============================================

Run "Smoke" test group name from class-TestNGWithGroupName  and  run "regression" group from TestNGWithGroupName2?

 

<?xml version="1.0" encoding="UTF-8"?>

<suite name="My suite">

            <test name="My test-1">

                        <groups>

                                    <run>

                                                <include name="smoke"></include>

                                    </run>

                        </groups>

                       

                        <classes>

                                    <class name="TestNgBasics1.TestNGWithGroupName">

                                    </class>

           

                        </classes>

            </test>

           

            <test name="My test-2">

                        <groups>

                                    <run>

                                                <include name="regression"></include>

                                                                      

                                    </run>

                        </groups>

                       

                        <classes>

                                    <class name="TestNgBasics1.TestNGWithGroupName2">                          </class>

                                   

                        </classes>

            </test>

           

</suite>

run :

o/p:

[RemoteTestNG] detected TestNG version 7.0.0

[TestNGContentHandler] [WARN] It is strongly recommended to add "<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >" at the top of your file, otherwise TestNG may fail or not work as expected.

smoke test A - smt-1

smoke test A - smt-2

smoke  test B - smt-1

smoke  test B - smt-2

TestNGWithGroupName2 regression test C - smt-1

TestNGWithGroupName2 regression test C - smt-2

===============================================

My suite

Total tests run: 3, Passes: 3, Failures: 0, Skips: 0

===============================================

Run Specific "Test method" in Class: 

 

  package TestNGBasics;

import org.testng.annotations.Test;

public class TestNGWithMethods

{

             //

                        @Test

                        public void testA()

                        {

                                    System.out.println("test A - stmt1- ");

                        }          

                       

                        @Test

                        public void MobileTest1()

                        {

                                    System.out.println("MobileTest 1  - stmt1- ");

                        }

                       

                        @Test

                        public void MobileTest2()

                        {

                                    System.out.println("MobileTest 2  - stmt1- ");

                        }

                       

           

}

--------------------------

create xml file manually :

Run "MobileTest1" method from class :

Note:

1. Define the <methods> tag at the <class> level.

 

<!--      <methods>

                                    <incude name="MobileTest1">         </incude>                  

                        </methods> -->

2. Do not write the <methods> tag at the <suite> level.

            <!--      <methods>

                                    <incude name="MobileTest1">         </incude>                  

                        </methods> -->

                       

3. Do not write the <methods> tag at the <test> level.           

-------------------------

<?xml version="1.0" encoding="UTF-8"?>

<suite name="My suite">

            <test name="My Test ">

                        <classes>

                                    <class name="TestNgBasics1.TestNGWithMethods">

                                                <methods>

                                                            <include name="MobileTest1">

                                                            </include>

                                                </methods>

                                    </class>

                        </classes>

            </test>

</suite>

run as :

[RemoteTestNG] detected TestNG version 7.0.0

[TestNGContentHandler] [WARN] It is strongly recommended to add "<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >" at the top of your file, otherwise TestNG may fail or not work as expected.

MobileTest 1  - stmt1-

===============================================

My suite

Total tests run: 1, Passes: 1, Failures: 0, Skips: 0

===============================================

 

 

HW Run testA() from from given Class from testng.xml file :

 

<?xml version="1.0" encoding="UTF-8"?>

<suite name="My suite">

            <test name= "My tests">

                        <classes>                    

                                    <class name="TestNGBasics.TestNGWithMethods">

                            <--  run specific methods   -->

                                               

                                   

                                    </class>

                        </classes>

                       

            </test>

</suite>

o/p:

HW Run specific 'MobileTest2' Test method  from testng.xml file :

<?xml version="1.0" encoding="UTF-8"?>

<suite name="My suite">

            <test name= "My tests">

                        <classes>                    

                                    <class name="TestNGBasics.TestNGWithMethods">

                                                <!--run  MobileTest1  -->                              

                                   

                                    </class>

                        </classes>

                       

            </test>

</suite>

o/p:

 

We can run 2 specific methods from given class:

i.e MobileTest1  ,MobileTest2

 

testng.xml:

 

<?xml version="1.0" encoding="UTF-8"?>

<suite name="My suite">

            <test name="My Test ">

                        <classes>

                                    <class name="TestNgBasics1.TestNGWithMethods">

                                                <methods>

                                                            <include name="MobileTest1"> </include>

                                                            <include name="MobileTest2"> </include>

                                                </methods>

                                    </class>

                        </classes>

            </test>

</suite>

o/p:

----

[RemoteTestNG] detected TestNG version 7.0.0

[TestNGContentHandler] [WARN] It is strongly recommended to add "<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >" at the top of your file, otherwise TestNG may fail or not work as expected.

MobileTest 1  - stmt1-

MobileTest 2  - stmt1-

===============================================

My suite

Total tests run: 2, Passes: 2, Failures: 0, Skips: 0

===============================================

HW Run MobileTest1()  and testA()  from given class in testng.xml file  ?

HW Run MobileTest2()  and testA()  from given class in testng.xml file  ?

Note :

1. The <methods> tag must be defined inside the <class> tag only, not inside <suite>, <test>, or <classes> tags.

 

ex: invalid

 

<?xml version="1.0" encoding="UTF-8"?>

<suite name="My suite">

            <test name= "My tests">

                        <classes>        

                                                <methods>

                                                           

                                                            <include name ="testA"></include>

                                                            <include name ="MobileTest1"></include>

                                                            <!-- <include name ="MobileTest12321321"></include> -->                                          

                                                </methods>   

                                    <class name="TestNGBasics.TestNGWithMethods">

                                               

                                   

                                    </class>

                        </classes>

                       

            </test>

</suite>

 

if we write "methods" tag in "suite" tag level, it throws some  TestNGException or Nullpointer exception

if we write "methods" tag in "test" tag level, it throws some  TestNGException or Nullpointer exception.

if we write "methods" tag in "classes" tag level, it throws some  TestNGException or Nullpointer exception.

 

2. Dont  make any spelling mistakes for tag names

            <inlcude>        - wrong  ---

            <include> ---  right

if something is not working properly from testng.xml file -check tags properly are written .else it behaves/works differently

 

Regular expression methods include  .* :

Run 'tests'  which starts from given  'test name' = MobilTest :

 

package TestNGBasics1;

import org.testng.annotations.Test;

public class TestNGMobileAndroidTest

{

             //

                        @Test

                        public void testA()

                        {

                                    System.out.println("test A - stmt1- ");

                        }          

                       

                        @Test

                        public void MobileTest1()

                        {

                                    System.out.println("MobileTest 1  - stmt1- ");

                        }

                       

                        @Test

                        public void MobileTest2()

                        {

                                    System.out.println("MobileTest 2  - stmt1- ");

                        }

                       

                       

                        @Test

                        public void MobileTestAndroid()

                        {

                                    System.out.println("MobileTestAndroid - stmt1- ");

                        }

                       

}

Run 'tests'  which starts from given  'test name' = MobilTest :

<?xml version="1.0" encoding="UTF-8"?>

<suite name="My suite">

            <test name="My Test ">

                        <classes>

                                    <class name="TestNgBasics1.TestNGMobileAndroidTest">

                                                <methods>

                                                            <include name="MobileTest.*"> </include>

                                                </methods>

                                    </class>

                        </classes>

            </test>

</suite>

o/p:

[RemoteTestNG] detected TestNG version 7.0.0

[TestNGContentHandler] [WARN] It is strongly recommended to add "<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >" at the top of your file, otherwise TestNG may fail or not work as expected.

MobileTest 1  - stmt1-

MobileTest 2  - stmt1-

MobileTestAndroid - stmt1-

===============================================

My suite

Total tests run: 3, Passes: 3, Failures: 0, Skips: 0

===============================================

HW Define Some test Metods in class .  AndoidTest1(), AndoidTest2()  IosTest1() IosTest2()

. Run Test method names which start with 'Android'

. Run Test method names which start with 'Ios' using regular expression .*?

git commands MCQ

 Here are some multiple-choice questions (MCQs) on Git commands relevant for Selenium: 1. Which Git command is used to clone a remote reposi...