Java 测试自动化 - 测试用例注释到范围报告

标签 java annotations extentreports

我创建了一个新注释

@Target(ElementType.METHOD)
public @interface DisplayName {
    String value() ; 
}

我想使用它来定义范围报告中的测试用例名称。在测试用例上:

@Test
@DisplayName("testcase title")
public void TestCase_1() throws InterruptedException {...}

在 TestListener 中,我现在设法使用描述字段设置测试用例的标题。

    @Override
public void onTestStart(ITestResult iTestResult) {
    System.out.println("I am in onTestStart method " + getTestMethodName(iTestResult) + " start");
    // Start operation for extentreports.
    ExtentTestManager.startTest(iTestResult.getMethod().getDescription(), iTestResult.getMethod().getDescription());
}

我想使用@DisplayName注释作为测试用例标题,但我不知道如何在TestListener中引入注释值。

提前致谢!

解决方案__________________在@Kovacic的大力帮助下____解决方案

最终结果:

注解类:

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface DisplayName {
    String value();
}

测试监听器类:

........
@Override
    public void onTestStart(ITestResult iTestResult) {

        String valueFromInterface = null;

        Method method = iTestResult.getMethod().getConstructorOrMethod().getMethod();

        if (method.isAnnotationPresent(DisplayName.class)) {
            DisplayName displayName = method.getAnnotation(DisplayName.class);
            if (displayName != null) {
              valueFromInterface = displayName.value();
            }
        }

        ExtentTestManager.startTest(valueFromInterface, iTestResult.getMethod().getDescription());
    }

........

最佳答案

我希望我理解这个问题,这是解决方案

如果您使用它作为界面:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface ITestrail {
    public @interface DisplayName {
        String value() ; 
    }

您还需要添加到您的界面(如下行):

@Retention(RetentionPolicy.RUNTIME)

试试这个:

@Override
public void onTestStart(ITestResult result) {

    String valueFromInterface;

    Method method = result.getMethod().getMethod();

    if (method.isAnnotationPresent(DisplayName.class)) {
        DisplayName displayName = method.getAnnotation(DisplayName.class);
        if (displayName != null) {
          valueFromInterface = displayName.value();
        }
    }

    ExtentTestManager.startTest(iTestResult.getMethod().getDescription(), valueFromInterface);
}

希望这有帮助,

关于Java 测试自动化 - 测试用例注释到范围报告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51101469/

相关文章:

java使用注解和aop发送电子邮件

java - 二维数组无法正确输入

java - 检测传递给构造函数的给定对象中的当前注释

java - cucumber :输出文件夹中未生成范围报告

java - Extentreport 日志行正在重复

java - 使用 selenium webdriver 在新选项卡上打开打开的页面后如何在打开的页面上工作?

java - 从控制台运行java

java - ExtentReports - 单独的驱动程序实例的单独步骤

java - Hibernate - 学生评分系统。需要注释方面的帮助

java - Mockito @Before注释: variable might not have been initialized