Create First TestNG Program and Execute.

Hello Welcome to TestNG tutorial, today we will see how to write testng script in Selenium. Before moving forward make Sure TestNG and Eclipse is ready is Installed.


Q1-Difference between Java Program(main() method) and TestNg Script?

Ans- When we execute Java program and TestNG script then functionality wise nothing will change because our script is performing the same functionality but using TestNG you will get some additional functionality.

Some benefit of TestNG Script
  1. For even single test case you will get 3 reports. These reports generated by TestNG
  2. You can check execution time i.e. How much time test case has taken
  3. Parallel execution etc
Let’s write 1 simple java program and 1 TestNG script then will compare



Basic Java Program

public class TestJava {
    public static void main(String[] args) {
     
        System.out.println("Basic Java Program");
     
    }

Steps to Write testng script


import org.testng.annotations.Test;
public class TestScript {
    @Test
    public  void appTest() {
     
        System.out.println("testng script");
     
    }
}

How we can write TestNG Script?

To execute TestNG script we don’t have to write the separate class. We can use simple java class but here we will not write public static void main(String []args) because we are not going to execute this from JVM.

TestNG works with Annotations and annotation can be represented by @ symbol

@Test- is this the main annotation from where TestRunner will start execution.
In other words, you can say @Test in entry point
  • Step 1- Select your project
  • Step 2- Go to src and create a new package and give any name (I gave demo)
  • Step 3- Now select above created package and create a new class and give any name (I gave TestScript1)
  • Step 4- create a simple java method and before that method use @ Test
Complete program write testng script

import org.testng.annotations.Test;
public class TestScript1 {
    @Test
    public  void appTest() {
     
        System.out.println("Hello Selenium");
     
    }
}

Step 5- Once you completed the script simply right click on above created program and select run as> TestNG Test.

Check Official documentation of TestNG for more info