简体   繁体   English

每个junit测试的具体设置

[英]Specific setup for each junit test

I have a sequence of tests which have to be fed an input data in the form of a file.However,the exact data content to be fed into each would be specific. 我有一系列测试,必须以文件的形式提供输入数据。但是,要输入到每个测试中的确切数据内容将是特定的。
I intend to use temporary files to achieve this. 我打算用临时文件来实现这个目的。
The Setup method does not take a parameter. Setup方法不接受参数。
SO ,what could be done so that the setup can be made to read a specific fragment for each specific test. 那么,可以做什么以便可以设置为每个特定测试读取特定片段。
The actual set of steps in Setup would be same - creating a temporary file,but with a specific tailored piece of data. 安装程序中的实际步骤集将相同 - 创建临时文件,但具有特定的定制数据。

Setup methods (ie, methods annotated with @Before ) are designed for running the same steps before every test case. 设置方法(即使用@Before注释的方法)设计用于在每个测试用例之前运行相同的步骤。 If this isn't the behavior you need, just don't use them. 如果这不是您需要的行为,请不要使用它们。

At the end of the day, a JUnit test is just Java - you could just have a method that takes a parameter and sets up the test accordingly and call it explicitly with the different arguments you need: 在一天结束时,JUnit测试只是Java - 您可以使用一个方法来获取参数并相应地设置测试并使用您需要的不同参数显式调用它:

public class MyTest {

    private void init(String fileName) {
        // Reads data from the file and sets up the test
    }

    @Test
    public testSomething() {
        init("/path/to/some/file");
        // Perform the test and assert the result
    }

    @Test
    public testSomethingElse() {
        init("/path/to/another/file");
        // Perform the test and assert the result
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM