java - Java Test Automation - Test Case Annotation to Extent 报告
问题描述
我创建了一个新注释
@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);
}
希望这可以帮助,
推荐阅读
- python - 如何创建表条目的实例但未在 ponyorm 中添加?
- node.js - 如何使用 AWS 开发工具包以编程方式扩展 ECS 集群
- amazon-web-services - 一种将数据从一个 S3 复制到另一个 S3 存储桶的自动方法
- c# - Listview getItemAt 返回 null
- javascript - How to use auto migration with loopback 4 for mysql datasource
- macos - 如何在 macOS 上安装 clang-tidy?
- python - Can i create a real time chart with Pygal?
- frontend - Noob question regarding databases and frontends
- javascript - Javascript / Webix将对象引用为数组
- .net - Is it possible to configure the file name of a RollingFileAppender to include date followed by process ID?