【问题标题】:Java Test Automation - Test Case Annotation to Extent reportJava Test Automation - Test Case Annotation to Extent 报告
【发布时间】:2018-07-17 17:55:30
【问题描述】:

我创建了一个新注释

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

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

........

【问题讨论】:

    标签: java annotations extentreports


    【解决方案1】:

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

    如果你用这个作为接口:

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

    希望这会有所帮助,

    【讨论】:

    • 方法method = result.getMethod().getMethod();应该是 Method method = result.getMethod().getConstructorOrMethod().getMethod();但我得到 valueFromInterface - null,因为我需要实例化变量,当然我在方法 @DisplayName("Test Title") 上设置了注释
    • 那回答可以吗?
    • 忘记添加@Retention(RetentionPolicy.RUNTIME),尝试将其添加到界面,它应该可以工作
    • 它正在运行 YEY,感谢您,我将更新解决方案!
    • 很高兴我能帮上忙,我没想到要添加答案 c/p 错误。
    猜你喜欢
    • 2013-10-24
    • 2022-12-01
    • 2017-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-18
    • 1970-01-01
    • 2017-08-14
    相关资源
    最近更新 更多