我创建了一个新注释
@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/