参数化
**@DataProvider **
如果@DataProvider和@Test注解的方法不在同一个类或属于其基类的话,需通过dataProviderClass属性来指定@DataProvider类位置,并且@DataProvider注解的方法保持静态(static),举例如下
public class StaticProvider {
@DataProvider(name = "create")public static Object[][] createData() {
return new Object[][] {
new Object[] {
new Integer(42) }};}
}public class MyTest {
@Test(dataProvider = "create", dataProviderClass = StaticProvider.class)public void test(Integer n) {
// ...}
}```
@Parameters
@Parameters(value = “para”)参数个数须与注解方法的入参个数保持一致。
import org.testng.Assert;
import org.testng.annotations.*;public class TestNGHelloWorld1 {
@BeforeTestpublic void bfTest() {
System.out.println("TestNGHelloWorld1 beforTest!");}@Test(expectedExceptions = ArithmeticException.class, expectedExceptionsMessageRegExp = ".*zero")public void helloWorldTest1() {
System.out.println("TestNGHelloWorld1 Test1!");int c = 1 / 0;Assert.assertEquals("1", "1");}@Test()@Parameters(value = "para")public void helloWorldTest2(String str) {
Assert.assertEquals("1", "1");System.out.println("TestNGHelloWorld1 Test2! "+ str);}@AfterTestpublic void AfTest() {
System.out.println("TestNGHelloWorld1 AfterTest!");}
}