这是 JUnit 参数化测试的示例。我想这就是你所要求的。
@RunWith(Parameterized.class)
public class DataProviderTest extends WebDriverUtils {
private static String testName, searchString, ddMatch;
public DataProviderTest( String tName, String sString, String dMatch ) {
testName = tName;
searchString = sString;
ddMatch = dMatch;
}
@Before
public void setUp() {
System.out.println("setUp");
}
@Parameters(name = "{0}: {1}: {2}")
public static List<String[]> loadParams() {
File tFile = loadGradleResource( System.getProperty("user.dir") + separator + "build" +
separator + "resources" + separator + "test" + separator + "testdata2.csv" );
List<String[]> rows = null;
if ( tFile.exists() ) {
CSVReader reader = null;
try {
reader = new CSVReader( new FileReader( tFile ), ',' );
rows = reader.readAll();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
//String[][] csvMatrix = rows.toArray(new String[rows.size()][]);
System.out.println("Finished loadParams()");
return rows;
}
@Test
public void testParams() {
System.out.println("Param '{}' being run...", testName );
System.out.println("Search string: " + searchString );
System.out.println("ddMatch: " + ddMatch );
System.out.println("Test '{}' is done.", testName );
}
@After
public void cleanUp() {
System.out.println("Finished cleanUp");
}
}