Retrieving @ Test Test Test Results

I have the following format in 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 the test descriptions and thereby create test documentation. I tried to somehow include ITestNGMethod getDescription()in afterInvocation(IInvokedMethod method, ITestResult testResult), so after running each method, the description returns, but without success. Has anyone tried something like this?

+4
source share
6 answers

Implementing IMethodInterceptor allows you to access all of your test 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 an embedded class to your list of listeners. So TestNG knows about this.

+2
source

- ITestResult.

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

sysout (String) .

+2

. , 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());
}
+1

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

0

:

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

@Test

0

Source: https://habr.com/ru/post/1542044/


All Articles