1

I'm confused with the background of @test annotation. How it works? By "for" loop? Or other methods? I can't find the relative source code in the git, can someone points out ?

rtruszk
  • 3,902
  • 13
  • 36
  • 53
Jack
  • 21
  • 5

2 Answers2

1

TestNG Class is the entry point for running tests in TestNg Framework. Users can create their own TestNG object and invoke it in many different ways: On an existing testng.xml On a synthetic testng.xml, created entirely from Java By directly setting the test classes

TestNG first creates an object of the TestNG.Class as below code

/** Default constructor. Setting also usage of default listeners/reporters. */

public TestNG() {
  init(true);
}




private void init(boolean useDefaultListeners) {
  m_instance = this;

  m_useDefaultListeners = useDefaultListeners;
  m_configuration = new Configuration();
}

/**

  • Set the test classes to be run by this TestNG object. This method will create a dummy suite

  • that will wrap these classes called "Command Line Test".

  • If used together with threadCount, parallel, groups, excludedGroups than this one must be

  • set first.

  • @param classes An array of classes that contain TestNG annotations. */

    public void setTestClasses(Class[] classes) { m_suites.clear(); m_commandLineTestClasses = classes; }

    ITestNGListener iTestListener= new TestNgListenersTest(); testng.addListener(iTestListener);

If we want to add Listener in our test method above code is used by TestNG which is nothing but an method accepting ITestNGListener interface variable. This method checks what instance is passed in your ITestNGListener variable and based on that decide what to implement in Test.

public void addListener(ITestNGListener listener) {
  if (listener == null) {
    return;
  }
  if (listener instanceof IExecutionVisualiser) {
    IExecutionVisualiser visualiser = (IExecutionVisualiser) listener;
    maybeAddListener(m_executionVisualisers, visualiser);
  }
  if (listener instanceof ISuiteListener) {
    ISuiteListener suite = (ISuiteListener) listener;
    maybeAddListener(m_suiteListeners, suite);
  }
  if (listener instanceof ITestListener) {
    ITestListener test = (ITestListener) listener;
    maybeAddListener(m_testListeners, test);
  }
  if (listener instanceof IClassListener) {
    IClassListener clazz = (IClassListener) listener;
    maybeAddListener(m_classListeners, clazz);
  }
  if (listener instanceof IReporter) {
    IReporter reporter = (IReporter) listener;
    maybeAddListener(m_reporters, reporter);
  }
  if (listener instanceof IAnnotationTransformer) {
    setAnnotationTransformer((IAnnotationTransformer) listener);
  }
  if (listener instanceof IMethodInterceptor) {
    m_methodInterceptors.add((IMethodInterceptor) listener);
  }
  if (listener instanceof IInvokedMethodListener) {
    IInvokedMethodListener method = (IInvokedMethodListener) listener;
    maybeAddListener(m_invokedMethodListeners, method);
  }
  if (listener instanceof IHookable) {
    setHookable((IHookable) listener);
  }
  if (listener instanceof IConfigurable) {
    setConfigurable((IConfigurable) listener);
  }
  if (listener instanceof IExecutionListener) {
    m_configuration.addExecutionListenerIfAbsent((IExecutionListener) listener);
  }
  if (listener instanceof IConfigurationListener) {
    m_configuration.addConfigurationListener((IConfigurationListener) listener);
  }
  if (listener instanceof IAlterSuiteListener) {
    IAlterSuiteListener alter = (IAlterSuiteListener) listener;
    maybeAddListener(m_alterSuiteListeners, alter);
  }
  if (listener instanceof IDataProviderListener) {
    IDataProviderListener dataProvider = (IDataProviderListener) listener;
    maybeAddListener(m_dataProviderListeners, dataProvider);
  }
  if (listener instanceof IDataProviderInterceptor) {
    IDataProviderInterceptor interceptor = (IDataProviderInterceptor) listener;
    maybeAddListener(m_dataProviderInterceptors, interceptor);
  }
}

Final call to run the Testcases and Test Suite is performed by the run() method. TestNG call the run method in which there are multiple calls to other method like sanityCheck(),runExecutionListeners(),List suiteRunners = runSuites(); etc..

/** Run TestNG. */
public void run() {
  initializeEverything();
  sanityCheck();

  runExecutionListeners(true /* start */);

  runSuiteAlterationListeners();

  m_start = System.currentTimeMillis();
  List<ISuite> suiteRunners = runSuites();

  m_end = System.currentTimeMillis();

  if (null != suiteRunners) {
    generateReports(suiteRunners);
  }

  runExecutionListeners(false /* finish */);
  exitCode = this.exitCodeListener.getStatus();

  if (exitCodeListener.noTestsFound()) {
    if (TestRunner.getVerbose() > 1) {
      System.err.println("[TestNG] No tests found. Nothing was run");
      usage();
    }
  }

  m_instance = null;
  m_jCommander = null;
}

generateReports() method is also very important method call from the run() method and I want to highlight this as this method receives a List of ISuite Interface which is used to Generate report at the end of the Suite run

private void generateReports(List<ISuite> suiteRunners) {
  for (IReporter reporter : m_reporters.values()) {
    try {
      long start = System.currentTimeMillis();
      reporter.generateReport(m_suites, suiteRunners, m_outputDir);
      Utils.log(
          "TestNG",
          2,
          "Time taken by " + reporter + ": " + (System.currentTimeMillis() - start) + " ms");
    } catch (Exception ex) {
      System.err.println("[TestNG] Reporter " + reporter + " failed");
      ex.printStackTrace(System.err);
    }
  }
}
0

The @test annotation in TestNG marks a class or a method as a part of the test. In the following link you can find more information about different functionalities of @Test in TestNG:

http://testng.org/doc/documentation-main.html

juanmajmjr
  • 1,055
  • 7
  • 11
  • Thanks for your answer, but the link above still doesn't solve my problem, I want to know the inner implementation of @Test, other than how to use it – Jack Jul 21 '15 at 08:47