Java测试自动化-扩展报告的测试用例注释

Java测试自动化-扩展报告的测试用例注释,java,annotations,extentreports,Java,Annotations,Extentreports,我创建了一个新的注释 @Target(ElementType.METHOD) public @interface DisplayName { String value() ; } 我想用它来定义扩展报告中的测试用例名称。关于测试用例: @Test @DisplayName("testcase title") public void TestCase_1() throws InterruptedException {...} 在TestListener中,我现在使用descripti

我创建了一个新的注释

@Target(ElementType.METHOD)
public @interface DisplayName {
    String value() ; 
}
我想用它来定义扩展报告中的测试用例名称。关于测试用例:

@Test
@DisplayName("testcase title")
public void TestCase_1() throws InterruptedException {...}
在TestListener中,我现在使用description字段设置了测试用例的标题

    @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\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu

最终结果:

注释类:

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

........
@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() ; 
    }
此外,您还需要将以下内容添加到界面(下面的行):

试试这个:

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

希望这有帮助,

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

如果将此用作接口:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface ITestrail {
    public @interface DisplayName {
        String value() ; 
    }
此外,您还需要将以下内容添加到界面(下面的行):

试试这个:

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

希望这有帮助,

methodmethod=result.getMethod().getMethod();应该是Method=result.getMethod().getConstructorOrMethod().getMethod();但是我得到了valueFromInterface-null,因为我需要实例化变量,当然我在@DisplayName(“测试标题”)方法上设置了注释,那么答案是否正确?忘记添加@Retention(RetentionPolicy.RUNTIME),尝试将其添加到接口,它应该可以工作了。是的,我将更新解决方案,感谢您!很高兴我能帮上忙,我忘了加上c/p错误的答案;应该是Method=result.getMethod().getConstructorOrMethod().getMethod();但是我得到了valueFromInterface-null,因为我需要实例化变量,当然我在@DisplayName(“测试标题”)方法上设置了注释,那么答案是否正确?忘记添加@Retention(RetentionPolicy.RUNTIME),尝试将其添加到接口,它应该可以工作了。是的,我将更新解决方案,感谢您!很高兴我能帮忙,我忘了加上c/p错误的答案。