[英]How to Pass multiple Input and Output test file names dynamically In Junit 5?
[英]How to pass a Input and Expected file name dynamically for BeforeAll Method | Junit 5
我已經實現了一個測試用例來檢查輸入和預期的 Json 文件是相同的。
@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);
}
為了傳遞不同的文件名,我創建了類似於上面的新測試類和測試方法。 它按預期工作。 現在為了更好的配置,我計划在單個 class 中實現它。 通過動態傳遞輸入和預期 Json 不同的文件,如Input2.json Expected2.json從單個 ZA2F2ED4F8EBC2CBBDZC2ADC1。
我需要將每個文件名作為參數傳遞給BeforeAll方法(如循環),類似於參數化測試。
任何人都可以建議實現這一目標?
我不確定您為什么要在@BeforeAll
方法中實施該測試。
我很想將該方法設為私有方法,該方法需要兩個 arguments ( inputFile,expectedResultsFile ),然后編寫調用該方法的測試
就像是
@Test
public void test1(){
checkFilesIdentical("inputFile1", "expectedResults1")
}
@Test
public void test1(){
checkFilesIdentical("inputFile2", "expectedResults2")
}
private void checkFilesIdentical( String inputFileName, String expectedResulsFileName ) throws IOException {
inputList = readInput(CommonTestConstants.FilePath + "/" + inputFileName +"json");
expectedList = readExpected(CommonTestConstants.FilePath + "/" + expectedResulsFileName + " .json");
assertEquals("Input and outcome fact lists must be of the same size",
inputList.size(), expectedList.size());
}
使用ParameterizedTest
如下:
@ParameterizedTest
@ValueSource(strings = {"inputFile1:expectedResults1", "inputFile2:expectedResults2"})
void checkIdentical(String files) {
String[] x = files.split(":");
String inputFile = x[0];
String expectedResult = x[1];
.....
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.