How to modify TestNG/Allure (@Test(description), @Description, @TmsLink) values based on DataProvider supplied test parameters

huangapple go评论84阅读模式
英文:

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, &quot;value&quot;, &quot;&lt;GET FROM testData&gt;&quot;);
changeAnnotationValue(description, &quot;value&quot;, &quot;&lt;GET FROM testData&gt;&quot;);
changeAnnotationValue(test, &quot;description&quot;, &quot;&lt;GET FROM testData&gt;&quot;);
}
@SuppressWarnings(&quot;unchecked&quot;)
public static Object changeAnnotationValue(Annotation annotation, String key, Object newValue){
System.out.println(&quot;BEFORE annotation: &quot; + annotation);
Object handler = Proxy.getInvocationHandler(annotation);
Field f;
try {
f = handler.getClass().getDeclaredField(&quot;memberValues&quot;);
} catch (NoSuchFieldException | SecurityException e) {
throw new IllegalStateException(e);
}
f.setAccessible(true);
Map&lt;String, Object&gt; memberValues;
try {
memberValues = (Map&lt;String, Object&gt;) 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(&quot;AFTER annotation: &quot; + 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 .

huangapple
  • 本文由 发表于 2020年3月16日 20:15:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/60705852.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定