如何动态传递BeforeAll方法的输入文件和期望文件名Junit 5 [英] How to pass a Input and Expected file name dynamically for BeforeAll Method | Junit 5

查看:139
本文介绍了如何动态传递BeforeAll方法的输入文件和期望文件名Junit 5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了一个测试用例,以检查Input和期望的Json文件是否相同.

I have Implemented a test cases to check the Input and expected Json files are same.

 @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 .

For passing the different file names, I have created new test classes and test methods similar like above. It's working as expected. For better configuration now I planned to achieve it in single class. By passing input and expected Json different files dynamically like Input2.json Expected2.json from the single class.

我需要将每个文件名作为参数传递给 BeforeAll 方法(类似于循环),类似于参数化测试.

I need to pass each file names as a parameter to BeforeAll method(Like looping), similar to a parameterized test.

任何人都可以建议实现这一目标吗?

Any one can advise to achieve this?

推荐答案

我不确定为什么要在@BeforeAll方法中实现该测试.

I'm not sure why you are implementing that test in a @BeforeAll method.

我很想让该方法成为一个带有两个参数(inputFile,expectedResultsFile)的私有方法,然后编写调用该方法的测试

I'd be tempted to make that method a private method that takes two arguments ( inputFile, expectedResultsFile ) and then write tests that called that method

类似

@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());
}

这篇关于如何动态传递BeforeAll方法的输入文件和期望文件名Junit 5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆