检索@Test描述表单testNG测试

时间:2014-05-26 13:39:54

标签: 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{
}

是否有办法实现可以读取所有测试描述的内容,并通过生成测试文档。 我试图以某种方式将ITestNGMethod getDescription()包含到afterInvocation(IInvokedMethod method, ITestResult testResult)中,因此在运行每个方法后,将返回描述,但没有成功。 有没有人尝试类似的东西?

5 个答案:

答案 0 :(得分:3)

最简单的方法是使用ITestResult。

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

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

答案 1 :(得分: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知道它。

答案 2 :(得分:1)

最简单的方法是:

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

这应该为您提供@Test注释的描述参数

答案 3 :(得分:0)

有一种更简单的方法可以做到这一点,而无需定义监听器拦截器,正如我在this GitHub project I made中所示。

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

答案 4 :(得分: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()); } }