【问题标题】:Retrieving @Test description form testNG tests检索@Test 描述表单 testNG 测试
【发布时间】:2017-10-23 16:57:39
【问题描述】:

我的 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) 中,所以在每个方法运行后,都会返回描述,但没有成功。 有人试过类似的吗?

【问题讨论】:

    标签: java automation testng


    【解决方案1】:

    最简单的方法是使用 ITestResult。

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

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

    【讨论】:

      【解决方案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());
      }
      

      【讨论】:

        【解决方案3】:

        最简单的方法是:

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

        这应该给你@Test注解的描述参数

        【讨论】:

          【解决方案4】:

          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 知道。

          【讨论】:

            【解决方案5】:

            有一种更简单的方法可以做到这一点,无需定义侦听器拦截器,正如我在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());
                }
                ...
            }
            

            【讨论】:

              【解决方案6】:

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

              【讨论】:

              • 只是一些代码不是问题的答案。请添加一些解释。
              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2011-10-30
              • 2012-01-25
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多