英文:
How to modify TestNG/Allure (@Test(description), @Description, @TmsLink) values based on DataProvider supplied test parameters
问题
考虑到一个带有数据提供程序和用于报告的 TestNG 测试类,需要修改 Allure 报告,以根据 DataProvider 设置 (@Test(description),@TmsLink,@Description) 值。
是否有一种简单的方法来实现这个?
注意:
我尝试过使用 ITest 接口更改测试名称,但对 Allure 报告没有影响,我需要修改 TestNG 测试描述以及 Allure 的 @Description 和 @TmsLink 值。
英文:
Given a TestNG test class with dataProvider and Allure for reporting, it is required to modify Allure's report to have a (@Test(description), @TmsLink, @Description) values depending on DataProvider.
Is there a simple way to do it?
Note:
I tried changing test name by using ITest interface, but has no effect on Allure report, I need TestNG test description and Allure @Decription & @TmsLink values modified.
答案1
得分: 1
@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", "<从testData获取>");
changeAnnotationValue(description, "value", "<从testData获取>");
changeAnnotationValue(test, "description", "<从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。但是在改变 TestNG @Test.description 之前,Allure 仍然会获取它,所以我仍然需要在 Allure 捕获之前更新 @Test.description。
英文:
@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;
}
I managed to change Allure @Description
and @TmsLink
and TestNG @Test.description
, but Allure still gets TestNG @Test.description
before changing it, so I still need to update @Test.description before Allure captures it .
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论