1 回答

TA貢獻1921條經驗 獲得超9個贊
您可以使用參數化測試。您需要創建一個注釋的方法,@Parameterized.Parameters您可以在其中加載集合中的所有數據(基本上是每次運行需要傳遞的參數)。
然后創建一個構造函數來傳遞參數,并且該構造函數參數將在每次運行時從此集合中傳遞
例如
@RunWith(Parameterized.class)
public class RepeatableTests {
private String name;
public RepeatableTests(String name) {
this.name = name;
}
@Parameterized.Parameters
public static List<String> data() {
return Arrays.asList(new String[]{"Jon","Johny","Rob"});
}
@Test
public void runTest() {
System.out.println("run --> "+ name);
}
}
或者,如果您不想使用構造函數注入,您可以使用@Parameter注釋來綁定值
@RunWith(Parameterized.class)
public class RepeatableTests {
@Parameter
public String name;
@Parameterized.Parameters(name="name")
public static List<String> data() {
return Arrays.asList(new String[]{"Jon","Johny","Rob"});
}
@Test
public void runTest() {
System.out.println("run --> "+ name);
}
}
添加回答
舉報