There are three possible ways to Run the TestNG script:
  • By using testng.xml
  • With Ant
  • From command line

By using testng.xml:
First user needs to create a testng.xml file in Java Project, please follow the below mentioned steps

Example : In Eclipse, create a New Class under your desired Java Project like below
  • Right click on “Java Project” >> “New” >> Class
  • Provide some Class Name like “Example : SampleTest"
Complete Code like below:




import org.testng.annotations.Test;


public class SampleTest {


@Test


public void test12()


 {


     System.out.println("Executing tes12........");



  }


}





Step1 :
In eclipse create a java project and right click on project >>New>> other options as show in screenshot



 Step 2:
After clicking on other option, User can see below window



By scroll down,User can see the XML option>>expand that>> select XML File and click on Next button

Step 3:
User Select the desired Java Project and provide xml name as "testng.xml" as in the below screenshot




Click on finish button then user can see the testng.xml file under java project as shown below screenshot



Step 4:
Now Right click on testng.xml, select “Open with” option and select Text editor then it will open like below screenshot


Step 5:
Copy the below xml code and paste it in your testng.xml file(shown in the above the step)
<?xml version="1.0" encoding="UTF-8"?>
<suite name="Suite1" verbose="1" >
      <test name="test1" >
           <classes>
               <class name="SampleTest" />
          </classes>
     </test>
        </suite>


Description of above xml:
    1. A Test suite is a collection of test cases that are intended to test a behavior or set of behaviors of software program.
    2. In TestNG, we cannot define a suite in testing source code, but it is represented by one XML file as suite is the feature of execution.
    3. This also allows flexible configuration of the tests to be run. A suite can contain one or more tests and is defined by the <suite> tag.
    • <suite> is a root tag of your testng.xml. It describes a test suite, which in turn is made of several <test> sections.
    • <test> tag is to test the test scenario, name =”test1” for name attribute we can give any name which describes test suite functionality
    • <classes> tags we are giving class name=”Sample Test” in the sense we are giving our java class name “SampleTest”
    • Every tag will close with </tagname> in example, end with </suite> and </test> ,</classes> etc
  1. Save the testng.xml and
  2. Right click on testng.xml file and
  3. Select Run As>>TestNg.suite



Step 6:
Finally the @Test method will execute the and show the results in eclipse console as below





Categories: