首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >IAnnotationTransformer方法的TestNG描述

IAnnotationTransformer方法的TestNG描述
EN

Stack Overflow用户
提问于 2020-06-09 16:50:22
回答 1查看 828关注 0票数 0

在实现IAnnotationTransfer接口时,有一个名为TestNG的参数,它是从测试类中读取的注释。现在,很少有方法是直接的,而不同的注释方法我无法理解(比如getAttributes)。有人能告诉我这些方法的用法(描述),这样我就可以知道如何使用这些方法了吗?

具体来说,什么是getAttributes返回的?

我尝试使用它(CustomAttribute[] cs = annotation.getAttributes();),但是在cs变量中没有得到任何信息。

IAnnotation接口中的所有方法都可以在下面访问:

https://javadoc.io/doc/org.testng/testng/7.1.0/org/testng/annotations/ITestAnnotation.html

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-06-10 04:29:22

下面是演示如何使用CustomAttribute的示例。其他方法都是不言自明的。

使用CustomAttribute 的测试类如下所示:

代码语言:javascript
运行
复制
import org.testng.Reporter;
import org.testng.annotations.CustomAttribute;
import org.testng.annotations.Test;

import java.util.Arrays;
import java.util.Optional;

public class SampleTestClass {

    @Test(attributes = {
            @CustomAttribute(name = "functionality", values = {"login", "oauth"}),
            @CustomAttribute(name = "flavor", values = {"bvt", "regression"})
    })
    public void customAttributeEnabledTestMethod() {
        System.err.println("Printing the custom attributes from test method");
        CustomAttribute[] attributes = Optional
                .ofNullable(
                        Reporter.getCurrentTestResult().getMethod().getAttributes())
                .orElse(new CustomAttribute[]{});
        Arrays.stream(attributes).forEach(attribute -> {
            System.err.println("Attribute Name : " + attribute.name());
            System.err.println("Attribute values : " + Arrays.toString(attribute.values()));
        });
    }

    @Test
    public void plainTestMethod() {
        System.err.println("Hello world again");
    }
}

--自定义注释转换器,用于更改以下内容的值:

代码语言:javascript
运行
复制
import org.testng.IAnnotationTransformer;
import org.testng.annotations.CustomAttribute;
import org.testng.annotations.ITestAnnotation;

import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Optional;

public class DemoAnnotationTransformer implements IAnnotationTransformer {

    @Override
    public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) {

        CustomAttribute[] attributes = annotation.getAttributes();
        //We need this filtering logic because there's currently a bug in TestNG 7.1.0
        //which causes the annotation transfomer to be invoked multiple times.
        //This should be resolved in TestNG v7.2.0
        //The defect to refer to : https://github.com/cbeust/testng/issues/2312
        Optional<CustomAttribute> found = Arrays.stream(attributes)
                .filter(each -> each.name().equalsIgnoreCase("supported-browsers"))
                .findAny();

        if (found.isPresent()) {
            return;
        }

        int size = attributes.length + 1;
        CustomAttribute[] copied = new CustomAttribute[size];
        System.arraycopy(attributes, 0, copied, 0, attributes.length);
        copied[attributes.length] = new CustomAttribute() {

            @Override
            public Class<? extends Annotation> annotationType() {
                return CustomAttribute.class;
            }

            @Override
            public String name() {
                return "supported-browsers";
            }

            @Override
            public String[] values() {
                return new String[]{"firefox", "chrome"};
            }
        };
        annotation.setAttributes(copied);
    }
}

下面是xml套件的样子:

代码语言:javascript
运行
复制
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="qn_62287783_suite" parallel="false" configfailurepolicy="continue">
    <listeners>
        <listener class-name="com.rationaleemotions.stackoverflow.qn62287783.DemoAnnotationTransformer"/>
    </listeners>
    <test name="qn_62287783_test" verbose="2">
        <classes>
            <class name="com.rationaleemotions.stackoverflow.qn62287783.SampleTestClass"/>
        </classes>
    </test>
</suite>

,这是输出的样子:

代码语言:javascript
运行
复制
Printing the custom attributes from test method
Attribute Name : functionality
Attribute values : [login, oauth]
Attribute Name : flavor
Attribute values : [bvt, regression]
Attribute Name : supported-browsers
Attribute values : [firefox, chrome]


Hello world again

PASSED: customAttributeEnabledTestMethod
PASSED: plainTestMethod

===============================================
    qn_62287783_test
    Tests run: 2, Failures: 0, Skips: 0
===============================================


===============================================
qn_62287783_suite
Total tests run: 2, Passes: 2, Failures: 0, Skips: 0
===============================================


Process finished with exit code 0
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62287783

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档