检索@Test描述表单testNG测试

gan*_*ool 5 java testng automation

我的testNG测试中有以下格式:

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

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

Arr*_*han 5

最简单的方法是使用 ITestResult。

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

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


Man*_*anu 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());
}
Run Code Online (Sandbox Code Playgroud)


use*_*106 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;
    }

}
Run Code Online (Sandbox Code Playgroud)

将实现的类添加到您的侦听器列表中。让 TestNG 知道这一点。