How does all annotations work in TestNg without main() method How does all annotations work in TestNg without main() method selenium selenium

How does all annotations work in TestNg without main() method


This is a valid doubt many testers have. Because the main() method is needed to run the Java program and while writing tests in TestNg we don't use main() method, and we use Annotations instead.

Annotations in TestNG are lines of code that can control how the method below them will be executed. So, in short you don't need to write main() method, TestNg do that by itself. Refer the code at the end in Annotations documentation to get the idea how it happens.

As rightly pointed out in this answer: https://stackoverflow.com/a/1918154/3619412

Annotations are meta-meta-objects which can be used to describe other meta-objects. Meta-objects are classes, fields and methods. Asking an object for its meta-object (e.g. anObj.getClass() ) is called introspection. The introspection can go further and we can ask a meta-object what are its annotations (e.g. aClass.getAnnotations). Introspection and annotations belong to what is called reflection and meta-programming.

Also, it's not necessary to have main() method in your tests, but you can use main() method to run the TestNg tests if you want. Refer this.


to run script from cmd prompt we use below statement,

java org.testng.TestNG testng1.xml

main method in TestNG.java class how accept the command line argument,

 public static void main(String[] argv) {    TestNG testng = privateMain(argv, null);    System.exit(testng.getStatus());  }


You saw it right. Test-cases get executed through , the testing framework which was inspired from without having the main() method but extensively uses annotations.


Annotations

As per the documentation in Annotations majority of the APIs require a huge amount of boilerplate code. To write a web service you need to provide a paired interface and implementation. This boilerplate could be automatically generated by a tool if the program can be decorated with annotations indicating which methods were remotely accessible. Annotations doesn't affects the program semantics directly but they do affect the way programs are treated by tools and libraries, which can in turn affect the semantics of the running program.


TestNG

TestNG is a simple annotation-based test framework which uses a marker annotation type to indicate that a method is a test method and should be run by the testing tool. As an example:

import org.testng.annotations.Test;    @Test    public void foo() {    System.out.println("With in foo test");    }    

The testing tool which is being used is as follows:

import java.lang.reflect.*;public class RunTests {   public static void main(String[] args) throws Exception {      int passed = 0, failed = 0;      for (Method m : Class.forName(args[0]).getMethods()) {     if (m.isAnnotationPresent(Test.class)) {        try {           m.invoke(null);           passed++;        } catch (Throwable ex) {           System.out.printf("Test %s failed: %s %n", m, ex.getCause());           failed++;        }     }      }      System.out.printf("Passed: %d, Failed %d%n", passed, failed);   }}