java - 如何根据 DataProvider 提供的测试参数修改 TestNG/Allure (@Test(description)、@Description、@TmsLink) 值

标签 java reflection testng allure testng-dataprovider

给定带有 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/

相关文章:

Scala-如何获取 Vector 的包含类?

java - 在 TestNG 中覆盖 @BeforeMethod

java - 通用 3D 演示软件的建议框架

java - 无法看到从 Java 类执行的 Perl 输出

java - 动态实例化类?

android - 使用 android :exported=false 从 APK 加载服务

java - 断言应该放在测试用例中还是验证方法中?

java - Selenium - 多浏览器和多用户登录

java - JTable Actionlistener 单击带有参数的行

java - 如果编辑文本是 "word"将编辑文本变为绿色