从testNG测试中获取@Test描述

12

我的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()包含到afterInvocation(IInvokedMethod method,ITestResult testResult)中,以便在运行每个方法后返回描述,但是没有成功。是否有人尝试过类似的方法?

6个回答

7
最简单的方法是使用ITestResult。
    @Override
    public void afterInvocation(IInvokedMethod arg, ITestResult arg1) { 
      System.out.println(arg.getTestMethod().getMethodName());
      System.out.println(arg1.getMethod().getDescription());
    }

第二个sysout将返回被调用的测试方法的(String)描述。

5
我们尝试了类似的方法。下面是打印每个方法的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());
}

3
最简单的方法是:
public void onTestStart(ITestResult result) {
System.out.prinln(result.getMethod().getDescription());
}

这应该帮助你获取@Test注解的描述参数。

2

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

}

将已实现的类添加到你的监听器列表中,这样TestNG就会知道它。

0

有一种更简单的方法可以做到这一点,而无需定义一个监听器拦截器,就像我在我创建的这个GitHub项目中展示的那样。

基本上,像这样定义一个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());
    }
    ...
}

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


仅仅提供代码并不是回答问题的方式,请添加一些解释。 - Nander Speerstra

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接