現在,我的解釋器有大約 107 個測驗輸入用例,我將 JUnit 測驗器設定為獨立手動處理每個用例,以免將它們混在一起。也就是說,如果我使用回圈來迭代測驗檔案
for (int i = 0; i < NUM_TESTS; i ) {
String fileName = "file_" (i 1) ".in";
testFile(fileName);
}
JUnit 將為所有 107 個測驗創建一個巨大的測驗結果,這意味著如果一個失敗,整個測驗都會失敗,這是我不想要的。正如我所說,現在我有類似的東西
@Test
public static void test001() {
testFile("file1.in");
}
@Test
public static void test002() {
testFile("file2.in");
}
雖然這可行,但我想有一個更好的解決方案來獲得我所追求的。
uj5u.com熱心網友回復:
您可以使用@ParameterizedTest注釋@MethodSource。
舉個例子 :
@ParameterizedTest
@MethodSource("fileNameSource")
void test(final String fileName) {
testFile(fileName);
}
private static Stream<String> fileNameSource() {
return IntStream.range(0,NUM_TESTS).mapToObj(i -> "file_" (i 1) ".in");
}
檢查https://junit.org/junit5/docs/current/user-guide/#writing-tests-parameterized-tests上的檔案
對于由 回傳的每個引數fileNameSource(),相應的測驗將被視為不同的情況。
uj5u.com熱心網友回復:
您必須根據需要定義自己的結構,這是一種在如下值串列中定義對 json 檔案的輸入的方法。
{
[
"value1",
"value2"
]
}
當你在測驗用例的幫助下執行時讀取這個值object mapper。
objectMapper.readValue(fixture("filePathName.json"),CustomInput.class);
CustomInput 如下所示。
public class CustomInput {
List<String> values;
}
您可以在 json 中保持增加和減少輸入。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/411814.html
標籤:
上一篇:Cypress/VueJS選擇在另一個動作之后動態添加的元素
下一篇:Chai-http請求回傳未定義
