12

I have the following format on my testNG tests:

@Test(alwaysRun = true, dependsOnMethods = "testStep_1", description = "Enter the names, and verify that they are appearing correctly ")
public void testStep_2() throws Exception{
}

Is there a way to implement something that could read all test descriptions, and by that generating a test documentation. I tried to somehow include ITestNGMethod getDescription() to a afterInvocation(IInvokedMethod method, ITestResult testResult) so after each method is run, the description is returned, but with no success. Has anyone tried something similar?

gandalf_the_cool
  • 659
  • 2
  • 9
  • 23

6 Answers6

7

The simplest way is by using ITestResult.

    @Override
    public void afterInvocation(IInvokedMethod arg, ITestResult arg1) { 
      System.out.println(arg.getTestMethod().getMethodName());
      System.out.println(arg1.getMethod().getDescription());
    }

The second sysout will return a (String) description of the invoked test method.

ArrchanaMohan
  • 2,314
  • 4
  • 36
  • 84
5

We tried something similar. Below is the method that print the testng test name and test description for each method.

@BeforeMethod
public void beforeMethod(Method method) {
    Test test = method.getAnnotation(Test.class);
    System.out.println("Test name is " + test.testName());
    System.out.println("Test description is " + test.description());
}
Manu
  • 2,251
  • 19
  • 30
3

The easiest way to do it is:

public void onTestStart(ITestResult result) {
System.out.prinln(result.getMethod().getDescription());
}

This should give you the description parameter of the @Test annotation

Sidhant
  • 675
  • 1
  • 7
  • 13
2

IMethodInterceptor implementation allows you to access all your tests annotations and their parameters.

import java.util.List;

import org.testng.IMethodInstance;
import org.testng.IMethodInterceptor;
import org.testng.ITestContext;
import org.testng.annotations.Test;

public class Interceptor implements IMethodInterceptor
{

    @Override
    public List<IMethodInstance> intercept(List<IMethodInstance> methods, ITestContext context)
    {
        int methCount = methods.size();

        for (int i = 0; i < methCount; i++)
        {
            IMethodInstance instns = methods.get(i);
            System.out.println(instns.getMethod().getConstructorOrMethod().getMethod().getAnnotation(Test.class)
                    .description());
        }

        return methods;
    }

}

Add implemented class to your listeners list. So that TestNG know about it.

user1058106
  • 530
  • 3
  • 12
0

There is an easier way to do this, without defining a listener interceptor, as I show in this GitHub project I made.

Basically, define a TestBase class like this:

public abstract class NGTestBase extends AbstractTestNGSpringContextTests
{
    ....
    private String testDescription;

    @BeforeMethod
    public void setUp(Method ngMethod)
    {
        ...
        setTestDescription(ngMethod);
        ...
    }

    ....

    public String getTestDescription()
    {
        return this.testDescription;
    }

    private void setTestDescription(Method methodInstance)
    {
        this.testDescription = methodInstance.getAnnotation(Test.class).description();
    }

}

Then, in your test, just annotate them like this:

@Test(description = "Test printing out all the Spring beans.")
public void printAllBeansTest(Method ngMethod)
{
    ...
    String[] beanNames = applicationContext.getBeanDefinitionNames();
    ...
    for (String beanName : beanNames)
    {
        test.log(LogStatus.INFO, "BEAN[" + beanName + "] : " + applicationContext.getBean(beanName).getClass().toString());
    }
    ...
}
djangofan
  • 28,471
  • 61
  • 196
  • 289
0

public class ReportSet_MethodListener implements IInvokedMethodListener { @Override public void afterInvocation(IInvokedMethod iInvokedMethod, ITestResult iTestResult) { if (iInvokedMethod.getTestMethod().isTest()){ System.out.println("TestCaseName:" + iInvokedMethod.getTestMethod().getDescription()); } }