@DataProvider
Method參數
數據提供者的第一個參數是java.lang.reflect.Method,TestNG傳遞這個將調用的測試方法。如果您希望數據提供者根據不同的測試方法返回不同的數據,那么這種做法就非常有用。
package com.test.jwen.httpApiAuto; import java.lang.reflect.Method; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; public class TestNg2 { @DataProvider public Object[][] provideNumbers(Method method){ String methodName = method.getName(); if (methodName.equals("two")){ return new Object[][]{new Object[] {2}}; } if (methodName.equals("three")){ return new Object[][]{new Object[] {3}}; } return null; } @Test(dataProvider = "provideNumbers") public void two(int param){ System.out.println("Two received : " + param); } @Test(dataProvider = "provideNumbers") public void three(int param){ System.out.println("Three received : " + param); } }