Java 从testNG测试中检索@Test描述

Java 从testNG测试中检索@Test描述,java,automation,testng,Java,Automation,Testng,我的testNG测试采用以下格式: @Test(alwaysRun = true, dependsOnMethods = "testStep_1", description = "Enter the names, and verify that they are appearing correctly ") public void testStep_2() throws Exception{ } 是否有一种方法可以实现可以读取所有测试描述的内容,并通过生成测试文档来实现。 我试图以某种方式将I

我的testNG测试采用以下格式:

@Test(alwaysRun = true, dependsOnMethods = "testStep_1", description = "Enter the names, and verify that they are appearing correctly ")
public void testStep_2() throws Exception{
}
是否有一种方法可以实现可以读取所有测试描述的内容,并通过生成测试文档来实现。 我试图以某种方式将
ITestNGMethod getDescription()
包含到调用后的
中(IInvokedMethod,ITestResult testResult)
因此,在运行每个方法后,都会返回描述,但没有成功。
有人尝试过类似的方法吗?

IMethodInterceptor实现允许您访问所有测试注释及其参数

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;
    }

}
import java.util.List;
导入org.testng.IMethodInstance;
导入org.testng.IMethodInterceptor;
导入org.testng.ITestContext;
导入org.testng.annotations.Test;
公共类拦截器实现IMethodInterceptor
{
@凌驾
公共列表截取(列表方法、ITestContext上下文)
{
int methCount=methods.size();
对于(int i=0;i

将实现的类添加到侦听器列表中。这样TestNG就知道了。

有一种更简单的方法可以做到这一点,而无需定义侦听器拦截器,如中所示

基本上,定义一个TestBase类,如下所示:

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();
    }

}
@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());
    }
    ...
}
然后,在测试中,只需如下注释:

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();
    }

}
@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());
    }
    ...
}

最简单的方法是使用ITestResult

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

第二个sysout将返回所调用测试方法的(字符串)描述。

公共类ReportSet\u MethodListener实现IInvokedMethodListener{
@凌驾
调用后公共无效(IInvokedMethod IInvokedMethod,ITestResult ITestResult){
if(iInvokedMethod.getTestMethod().isTest()){
System.out.println(“TestCaseName:+iInvokedMethod.getTestMethod().getDescription());
}

}

我们尝试了类似的方法。下面是打印每个方法的testng测试名称和测试描述的方法

@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());
}

最简单的方法是:

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

这应该为您提供@Test注释的description参数,只是一些代码不是问题的答案。请添加一些解释。