7

I have a doubt in TestNG with Java. I am completly new to TestNG. My doubt is, How all the test cases are executing using TestNG in java without having main() method? Please suggest me if you have any ideas. Following code is the example of a sample test case using TestNG in java. But if you notice, you can find one thing that there is no main() method in the code. Then, how does the testcases are executing?

I have another doubt. Is main() method needed for selenium Webdriver and TestNG combination to execute a script? Or can we execute testcases without main() method? If we can execute testcases without main(), then how does it is possible?

package com.first.example;
import org.testng.annotations.Test;
public class demoOne {
    @Test
    public void firstTestCase()
    {
        System.out.println("im in first test case from demoOne Class");
    }

    @Test
    public void secondTestCase()
    {
        System.out.println("im in second test case from demoOne Class");
    }
}
Manu
  • 2,251
  • 19
  • 30
Anitha
  • 1,045
  • 3
  • 18
  • 31
  • What happend when you started it? – Stefan Jul 30 '15 at 07:34
  • 1
    TestNG is a testing framework. It provides the `main` method so you don't need to: you just need to tell it which of your test classes you want it to run. – Andy Turner Jul 30 '15 at 07:36
  • what is the name of main method in TestNG? – Anitha Jul 30 '15 at 07:43
  • @Stefan when I started working with TestNG, everything was going good. But I had a doubt. thats why I asked – Anitha Jul 30 '15 at 07:44
  • 1
    Here you can find the different ways to [run tests with TestNG](http://testng.org/doc/documentation-main.html#running-testng). As Andy Turner said, you are starting the framework (TestNg class) and it will look up, either xml configuration files or scan for classes annotated with @Test. – Stefan Jul 30 '15 at 08:38

3 Answers3

7

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.

Community
  • 1
  • 1
Manu
  • 2,251
  • 19
  • 30
  • @anithav do accept one of the answers that solved your query. – Manu Jul 31 '15 at 06:10
  • Hi Manu , I have main method and testng.xml both in my maven project framework. I need to run main method first. How can I do this? – Sagar007 Nov 27 '15 at 09:03
  • 1
    While using TestNg, entry point is TestNg annotations and not the main method. So, you won't be able to run main method first. Better is to avoid main and write its code in BeforeClass/BeforeTest/BeforeSuite – Manu Nov 27 '15 at 09:35
  • They are using reflections, TestNG find the class where TestNG annotations is used for class,method or constructor. – Pavan T May 11 '17 at 12:13
  • We are running our script from cmd we are using below statement, java org.testng.TestNG testng1.xml public static void main(String[] argv) { TestNG testng = privateMain(argv, null); System.exit(testng.getStatus()); } – Pavan T May 11 '17 at 12:19
0

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());
  }
Pavan T
  • 716
  • 9
  • 12
0

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);
   }
}
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352