Monday, July 1, 2024

Convert classes to testNG.xml file and Run TestNG.xml

Convert classes to  testNG.xml file :

Steps:

Select some desired 2 or 3 classes > right click > TestNg >   Convert to TestNG > Click next  >  testng.xml got created in Project Level

open testng.xml : --> Click 'Source' tab at the bottom of the page if below code is not avaialble

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

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

<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">

<suite name="Suite">

  <test thread-count="5" name="Test">

    <classes>

      <class name="TestNgBasics1.TestAnnonation2"/>

      <class name="TestNgBasics1.AfterTestAnnonBasics"/>

    </classes>

  </test> <!-- Test -->

</suite> <!-- Suite -->

 

Run TestNg.xml file :

Right click in xml file  >   run as testNG suite  >   it runs  given classes in sequential order

TestAnnonation2

AfterTestAnnonBasics

 

o/p:

 [RemoteTestNG] detected TestNG version 7.0.0

stmt-1 from testA()

stmt-2 from testA()

stmt-1 from testA()

stmt-1 from testB()

@AfterTest gets executed only once after executing @Test Method

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

Suite

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

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

 

By using TestNG.xml file , we can run single class (or) multiple classes.

 

HW Convert 3 classes to testng.xml  and Run it ?

<class name="TestNgBasics.BeforeTestAnnotations2"/>

name =  Packagename.className1

            Packagename.className2

            Packagename.className3

Run package:

Replace  'class'  --> package in below xml file

 

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

<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">

<suite name="Suite">

  <test thread-count="5" name="Test">

 

  <!--   <classes>

                              <class name="TestNgBasics1.TestAnnonation2"/>

                              <class name="TestNgBasics1.AfterTestAnnonBasics"/>

    </classes> -->

   

     <packages>

                              <package name="TestNgBasics1"/>

                             

    </packages>

   

   

  </test> <!-- Test -->

</suite> <!-- Suite -->

Run as testNG :

2 ways to run Package:

1. through Testng.xml file

            Right click Run as test NG suite

runs all classes inside that package

2. go to Package >Rt click > Run as Test Ng Test

 

Create TestNG.xml file manually:

Steps:

  1. Select Project
  2. Right-click > New > Other
  3. Type xml in the search box
  4. Under XML, select 'XML File' > Next
  5. Give a meaningful name: testNGManual.xml
  6. The testNGManual.xml file is created at the project level

write below tags

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

<suite >

            <test>

                        <classes>

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

                       

                        </classes>      

           

            </test>

</suite>

 

run as TestNG .xml

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.

org.testng.TestNGException:

The <suite> tag must define the name attribute

            at org.testng.xml.TestNGContentHandler.xmlSuite(TestNGContentHandler.java:144)

           

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

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

<suite name= "My Suite">

            <test >

                        <classes>

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

                       

                        </classes>      

           

            </test>

</suite>

run testNg.xml

o/p:

org.testng.TestNGException:

The <test> tag must define the name attribute

 

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

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

<suite name= "My Suite">

            <test name="My Test ">

                        <classes>

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

                       

                        </classes>      

           

            </test>

</suite>

 

run as tetsng.xnl file:

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.

stmt-1 from testA()

stmt-2 from testA()

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

My Suite

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

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

Note :

Suite, test tags - we must define name attribute else it throws TestNGException

open xml file:

  click on testng.xml

testNG hierarchy :

Suite > test > Classes > Class

             > packages  > package

 

Run as testNG:

1. if we don't give package name --?

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

<suite name= "My Suite">

            <test name="My Test ">

                        <classes>

                                    <class name="TestAnnonation2" ></class>

                       

                        </classes>      

           

            </test>

</suite>

 

Run :

org.testng.TestNGException:

Cannot find class in classpath: TestAnnotations1

Fix : 

 always give "Package.className"

 

1. Error2: if we give invalid class name :

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

<suite name= "My Suite">

            <test name="My Test ">                                                                                                                                                                                                                                                           

                        <classes>

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

                       

                        </classes>      

           

            </test>

</suite>

 

run -->

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

org.testng.TestNGException:

Cannot find class in classpath: TestNgBasics1.TestAnnonation21

Fix :  check package name is correct or not

                 class name is correct or not

always Give Packagename.classname

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

Run as testNg :

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.

stmt-1 from testA()

stmt-2 from testA()

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

My Suite

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

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

 

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

My suite

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

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

 

HW  create Xml file manually   and run any 2 classses?

FAQ : Can you tell me testNG.xml  file hierarchy  (or) structure of testng.xml file ?

            <suite>

                        <test>

                                    <classes>

                                                <class name= "pkg.class1" >   </class>

                                                <class name = "pkg.class2" />  //  this is also ok

                                    </classes>

                        </test>

            </suite>

No comments:

Post a Comment

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