1 package roger.testng; 2 3 import java.lang.reflect.Method; 4 5 import org.testng.annotations.DataProvider; 6 import org.testng.annotations.Test; 7 8 /* 9 * Method 參數 Demo 10 * 若是數據提供者的第一個參數是 java.lang.reflect.Method, 11 * testng 傳遞這個將調用的測試方法 12 * 如:調用的方法名爲 two, 數據提供者就返回2 13 */ 14 public class TestDataProviderMethod { 15 // Method 表示當前測試的方法 16 @DataProvider 17 public Object[][] provideNumbers(Method method) { 18 Object[][] result = null; 19 // 根據方法名稱判斷 20 if (method.getName().equals("one")) { 21 result = new Object[][] { new Object[] { 1 } }; 22 } else if (method.getName().equals("two")) { 23 result = new Object[][] { new Object[] { 2 } }; 24 } else { 25 result = new Object[][] { new Object[] { 3 } }; 26 } 27 return result; 28 } 29 30 @Test(dataProvider = "provideNumbers") 31 public void one(int param) { 32 System.out.println("one received: " + param); 33 } 34 35 @Test(dataProvider = "provideNumbers") 36 public void two(int param) { 37 System.out.println("Two received: " + param); 38 } 39 40 @Test(dataProvider = "provideNumbers") 41 public void three(int param) { 42 System.out.println("Three received: " + param); 43 } 44 45 }