簡體   English   中英

如何在 Junit 5 中動態傳遞多個輸入和 Output 測試文件名?

[英]How to Pass multiple Input and Output test file names dynamically In Junit 5?

我需要將多個文件一個一個地傳遞給下面的 BeforeAll 方法。 我可以通過重復相同代碼的不同測試類來實現它。 我想使用單個 class 對其進行優化。

@BeforeAll
static void setUp() throws IOException {
    inputList = readInput(CommonTestConstants.FilePath + "/Input1.json");
    expectedList = readExpected(CommonTestConstants.FilePath + "/Expected1.json");
    assertEquals("Checking size of both list", inputList.size(), expectedList.size());
}

static Stream<Arguments> Arguments() {
    return IntStream.range(0, inputList.size())
                .mapToObj(i -> Arguments.of(inputList.get(i), expectedList.get(i)));
}

@ParameterizedTest
@DisplayName("Parameterized Test For First Input")
@MethodSource("Arguments")
void testFact(Object object, ExpectedObject expected) throws Exception {
    Outcome outcome = processExpectedJson(object);
    assertEquals(expected, outcome);
}

任何人請建議實現這一目標?

參考如何為 BeforeAll 方法動態傳遞輸入和預期文件名 | Junit 5

您可以創建文件名的Map然后迭代它,如下所示,

Map<String, String> files = Map.of("/Input1.json", "/Expected1.json",
                                    "Input1.json", "/Expected2.json");

files.forEach((k,v)->{
    inputList = readInput(CommonTestConstants.FilePath + k);
    expectedList = readExpected(CommonTestConstants.FilePath + v);
    assertEquals("Checking size of both list",
            inputList.size(), expectedList.size());
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM