gpt4 book ai didi

java - TestNG 中的 IAnnotationTransformer 方法说明

转载 作者:行者123 更新时间:2023-12-01 16:20:49 26 4
gpt4 key购买 nike

在TestNG中实现IAnnotationTransfer接口(interface)时,有一个名为annotation的参数,它是将从测试类中读取的注释。现在,有一些简单的方法,而我无法理解的各种注释方法(例如 getAttributes)。有人可以向我指出这些方法的示例用法(描述),以便我可以了解如何使用其中一些方法。

具体来说,getAttributes 返回什么?

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

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

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

最佳答案

下面是一个示例,展示了如何使用CustomAttribute。其他方法都是不言自明的。

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

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");
}
}

更改此值的自定义注释转换器:

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 如下所示:

<?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>

输出如下:

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

关于java - TestNG 中的 IAnnotationTransformer 方法说明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62287783/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com