在TestNG中,DataProviders为测试方法提供参数,使它们能够使用不同的数据集运行相同的测试。但是,如果我们需要从测试方法向DataProvider传递参数呢?我们如何才能做到这一点?
有几种方法可以实现这一目标,其中之一就是使用注释。
那么,什么是注解?注释作为元数据提供有关类、接口、变量、方法或字段的补充信息。但是,它们不会直接影响它们所应用的代码的功能。
注释以“@"开头。TestNG中有很多注释,比如@Test、@BeforeMethod、@DataProvider等。
那么,注释是如何将数据从测试方法传递到DataProvider的呢?
它可能无法独立工作,但当与java.lang.reflect.Method结合使用时,它会变得非常有效。因此,让我们深入研究java.lang.reflect.Method和annotations的概念。然后,我们可以将它们串联起来,展示如何将数据从方法传递到数据提供者。
理解java.lang.reflect.方法
根据文件-
它提供了有关类或接口上的单个方法的信息,并提供了对这些方法的访问。
或者简单地说,它提供有关方法的信息。让我们看一个例子。
import java.lang.reflect.Method;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class CodekruTest {
@DataProvider(name = "dataprovider_name")
public Object[][] dataProviderMethod(Method m) {
System.out.println("Method name: " + m.getName());
return new Object[][] {
{ "first", 2, 3 },
{ "second", 5, 6 },
{ "third", 8, 9 },
{ "fourth", 11, 12 }
};
}
@Test(dataProvider = "dataprovider_name")
public void test(String str, int a, int b) {
System.out.println("str = " + str + ", a = " + a + ", b = " + b);
}
}
在运行上述测试方法时,我们将得到以下输出:
Method name: test
str = first, a = 2, b = 3
str = second, a = 5, b = 6
str = third, a = 8, b = 9
str = fourth, a = 11, b = 12
PASSED: test("third", 8, 9)
PASSED: test("first", 2, 3)
PASSED: test("fourth", 11, 12)
PASSED: test("second", 5, 6)
您可能已经注意到测试方法的名称显示在数据提供程序中。这允许您访问DataProvider中的方法。
我们可以使用许多其他有用的方法来访问数据提供程序中的方法及其信息。
制作自定义注释
让我们创建自己的注释,名为“CodekruTestAnnotation”
如下所示,使用@interface关键字声明注释。
public @interface CodekruTestAnnotation {
}
下一步是定义注释的范围。
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
public @interface CodekruTestAnnotation {
}
这为我们的注释提供了运行时可见性。
现在,让我们添加一个字段“网站”。
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
public @interface CodekruTestAnnotation {
public String website() default "";
}
我们声明了一个名为“website”的String参数和一个空字符串作为默认值。
将注释与方法组合以将数据发送到DataProvider
我们将@CodekruTestAnnotation放在我们的测试方法上,如下所示。
@Test(dataProvider = "dataprovider_name")
@CodekruTestAnnotation(website = "Codekru")
public void test(String str, int a, int b) {
System.out.println("str = " + str + ", a = " + a + ", b = " + b);
}
我们已经将“website”的属性值设置为“Codekru”,现在将尝试在数据提供程序中检索此值。
@DataProvider(name = "dataprovider_name")
public Object[][] dataProviderMethod(Method m) {
System.out.println("Method name: " + m.getName());
Annotation annotation = m.getAnnotation(CodekruTestAnnotation.class);
CodekruTestAnnotation annotationObj = (CodekruTestAnnotation) annotation;
System.out.println("Website name: " + annotationObj.website());
return new Object[][] { { "first", 2, 3 }, { "second", 5, 6 }, { "third", 8, 9 }, { "fourth", 11, 12 } };
}
下面是整个代码
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class CodekruTest {
@DataProvider(name = "dataprovider_name")
public Object[][] dataProviderMethod(Method m) {
System.out.println("Method name: " + m.getName());
Annotation annotation = m.getAnnotation(CodekruTestAnnotation.class);
CodekruTestAnnotation annotationObj = (CodekruTestAnnotation) annotation;
System.out.println("Website name: " + annotationObj.website());
return new Object[][] { { "first", 2, 3 }, { "second", 5, 6 }, { "third", 8, 9 }, { "fourth", 11, 12 } };
}
@Test(dataProvider = "dataprovider_name")
@CodekruTestAnnotation(website = "Codekru")
public void test(String str, int a, int b) {
System.out.println("str = " + str + ", a = " + a + ", b = " + b);
}
}
测试方法后的输出
Method name: test
Website name: Codekru
str = first, a = 2, b = 3
str = second, a = 5, b = 6
str = third, a = 8, b = 9
str = fourth, a = 11, b = 12
PASSED: test("fourth", 11, 12)
PASSED: test("third", 8, 9)
PASSED: test("first", 2, 3)
PASSED: test("second", 5, 6)
您可以看到打印出了网站名称,这就是我们如何将值从测试方法传输到TestNG中的数据提供程序。