Question

I am trying to integrate TestLink with TestNG Approach is below

1>Write ITestListner with onTestFailure and onTestSuccess

2> get Annotation of the method(like testName which will be equivalent to test name in testlink) which is being failed/success in a variable

3>Make connection with TestLink using API available and update the test case.

However I am struggling to find method Annotation value in ITestListner and requirement is to get annotation values in ITestListner only so that correct test cases can be updated in Test_link

Can someone please help me how to get Test Method annotation value in ITestListner or any other approach in which i can integrate testlink update with TestNG

Was it helpful?

Solution

Hi Thanks niharika for help ,First of all you are correct in explaining use of TestNG but we are using TestNG for Selenium and already there are around 1000 test cases writen in test Methods and we have to live with that

Some how i have figured the solution ,we can still get the testName of the test method using two listners This is just work around I am not sure if this is the best approach but as of now solving my purpose

package com.automation.testng.listner;
import org.testng.*;


public class MyIInvokeMethodListner_TestName_TestLink implements IInvokedMethodListener    {

public static String testName;


public void afterInvocation(IInvokedMethod arg0, ITestResult arg1) {
    // TODO Auto-generated method stub

}

public void beforeInvocation(IInvokedMethod m, ITestResult tr) {
    // TODO Auto-generated method stub
    //This give the Annotation Test object
    org.testng.annotations.Test t=m.getTestMethod().getMethod().getAnnotation(org.testng.annotations.Test.class);

    MyIInvokeMethodListner_TestName_TestLink.testName = t.testName().toString();

}


}

MyITestListner goes like below

package com.automation.testng.listner;
import org.testng.*;
public class MyITestListner_TestLink extends TestListenerAdapter  {

/*IAnnotationTransformer at;
public Listner_1()
{
    this.at = new Annotation_listner();
}*/
@Override
public void onTestFailure(ITestResult tr)
{
    System.out.println("Hurray !I am being inboked from Test listner");
    MyIInvokeMethodListner_TestName_TestLink a = new MyIInvokeMethodListner_TestName_TestLink();
    System.out.println(MyIInvokeMethodListner_TestName_TestLink.testName);

    }

public void onTestSuccess(ITestResult tr)
{
    MyIInvokeMethodListner_TestName_TestLink a = new MyIInvokeMethodListner_TestName_TestLink();
    System.out.println(MyIInvokeMethodListner_TestName_TestLink.testName);
}

}

Basically we are getting the method and then using Test Annotation class setting the static variable which can be used in MyITestListner

OTHER TIPS

The ITestListener is the one which is used after <test> tag. For getting the method name and annotation specifics, you need to implement IInvokedMethodListener and in the after/before methods of this interface, and use something like method.getTestMethod().getMethodName() to get the executing method name.

If you are adding testName at the method level, I think you are doing it wrong since the help of testng mentions this "The name of the test this test class should be placed in. This attribute is ignore if @Test is not at the class level."

If you are indeed specifying the @Test at your class level then you can get it as below :

method.getTestMethod().getTestClass().getTestName()

A bit ugly and you probably want to wrap those parts in null checks in your code but this is how you get the testName specified in the annotation from the ITestResult:

iTestResult.getMethod().getConstructorOrMethod().getMethod().getAnnotation(Test.class).testName()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top