我已经实现了一个测试用例,以检查Input和期望的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);
}
为了传递不同的文件名,我创建了类似于上面的新测试类和测试方法。它按预期工作。为了更好的配置,我现在计划在单节课中实现它。通过传递输入和期望的Json不同的文件,例如来自单个类的 Input2.json Expected2.json 这样的动态文件。
我需要将每个文件名作为参数传递给 BeforeAll 方法(类似于循环),类似于参数化测试。
有人建议实现这一目标吗?
我不确定为什么要使用@BeforeAll
方法来实现该测试。
我很想让该方法成为一个带有两个参数(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];
.....
}