Java 测试自动化 - 测试用例注释范围报告
Java Test Automation - Test Case Annotation to Extent report
我创建了一个新注释
@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 中引入注释值。
提前致谢!
SOLUTION__________________With 来自@Kovacic__________________SOLUTION
的大力帮助
最终结果:
注解class:
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();
}
测试监听器Class:
........
@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);
}
希望这对您有所帮助,
我创建了一个新注释
@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 中引入注释值。
提前致谢!
SOLUTION__________________With 来自@Kovacic__________________SOLUTION
的大力帮助最终结果:
注解class:
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();
}
测试监听器Class:
........
@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);
}
希望这对您有所帮助,