给定带有 dataProvider 和 Allure 进行报告的 TestNG 测试类,需要根据 DataProvider 修改 Allure 的报告以具有 (@Test(description)、@TmsLink、@Description) 值。
有简单的方法吗?
注意: 我尝试使用 ITest 接口(interface)更改测试名称,但对 Allure 报告没有影响,我需要修改 TestNG 测试描述和 Allure @Decription & @TmsLink 值。
最佳答案
@BeforeMethod(alwaysRun = true)
public void BeforeMethod(Method method, Object[] testData){
TmsLink tmsLink = method.getAnnotation(TmsLink.class);
Description description = method.getAnnotation(Description.class);
Test test = method.getAnnotation(Test.class);
changeAnnotationValue(tmsLink, "value", "<GET FROM testData>");
changeAnnotationValue(description, "value", "<GET FROM testData>");
changeAnnotationValue(test, "description", "<GET FROM testData>");
}
@SuppressWarnings("unchecked")
public static Object changeAnnotationValue(Annotation annotation, String key, Object newValue){
System.out.println("BEFORE annotation: " + annotation);
Object handler = Proxy.getInvocationHandler(annotation);
Field f;
try {
f = handler.getClass().getDeclaredField("memberValues");
} catch (NoSuchFieldException | SecurityException e) {
throw new IllegalStateException(e);
}
f.setAccessible(true);
Map<String, Object> memberValues;
try {
memberValues = (Map<String, Object>) f.get(handler);
} catch (IllegalArgumentException | IllegalAccessException e) {
throw new IllegalStateException(e);
}
Object oldValue = memberValues.get(key);
if (oldValue == null || oldValue.getClass() != newValue.getClass()) {
throw new IllegalArgumentException();
}
memberValues.put(key,newValue);
System.out.println("AFTER annotation: " + annotation);
return oldValue;
}
我设法更改了 Allure @Description
和 @TmsLink
以及 TestNG @Test.description
,但是 Allure 在更改之前仍然获取 TestNG @Test.description
,因此我仍然需要在 Allure 捕获之前更新 @Test.description 。
关于java - 如何根据 DataProvider 提供的测试参数修改 TestNG/Allure (@Test(description)、@Description、@TmsLink) 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60705852/