Java 使用文件输入的 Junit 参数化测试

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21401504/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 08:29:09  来源:igfitidea点击:

Junit parameterized tests using file input

javaunit-testingjunitjunitparams

提问by Sourabh

I have a query about using parameterized tests for my unit testing of my APIs. Now instead of building an arraylist like

我有一个关于使用参数化测试进行 API 单元测试的查询。现在,而不是像建立一个数组列表

Arrays.asList(new Object[]{
   {1},{2},{3}
});

I want to read lines in the file one by one split them space and fill them in the array. This way everything would be generalized. Can anyone suggest me any such method with example?

我想一一读取文件中的行,将它们拆分空间并将它们填充到数组中。这样一切都将被概括。任何人都可以通过示例向我建议任何此类方法吗?

Also is there a way that I could be testing without declaring various arguments as the private members and initializing them in the constructor of the test unit?

还有一种方法可以在不将各种参数声明为私有成员并在测试单元的构造函数中初始化它们的情况下进行测试吗?

EDIT: Code as asked by Duncan

编辑:邓肯要求的代码

@RunWith(Parameterized.class)
public class JunitTest2 {

    SqlSession session;
    Integer num;
    Boolean expectedResult;
    static BufferedInputStream buffer = null;

    public JunitTest2(Integer num, Boolean expected){
        this.num = num;
        this.expectedResult = expected;
    }

    @Before
    public void setup() throws IOException{
        session = SessionUtil.getSqlSessionFactory(0).openSession();
        SessionUtil.setSqlSession(session);
        buffer = new BufferedInputStream(getClass().getResourceAsStream("input.txt"));
        System.out.println("SETUP!");
    }

    @Test
    public void test() {
        assertEquals(expectedResult, num > 0);
        System.out.println("TESTED!");
    }

    @Parameterized.Parameters
    public static Collection getNum() throws IOException{
        //I want my code to read input.txt line by line and feed the input in an arraylist so that it returns an equivalent of the code below

        return Arrays.asList(new Object[][]{
            {2, true},
            {3, true},
            {-1, false}
        });
    }
    @After
    public void tearDown() throws IOException{
        session.close();
        buffer.close();
        System.out.println("TEARDOWN!");
    }
}

Also my input.txt will be as follows:

我的 input.txt 也将如下:

2 true
3 true
-1 false

采纳答案by Narendra Pathai

@RunWith(JUnitParamsRunner.class)
public class FileParamsTest {

    @Test
    @FileParameters("src/test/resources/test.csv")
    public void loadParamsFromFileWithIdentityMapper(int age, String name) {
        assertTrue(age > 0);
    }

}

JUnitParamshas support for loading the data from a CSV file.

JUnitParams支持从 CSV 文件加载数据。

CSV File will contain

CSV 文件将包含

1,true
2,false

回答by blalasaadri

Have a look at the junitparamsproject, especially this example. It will show you how to use a CSV file for parameter input. Here's a short example:

看看junitparams项目,尤其是这个例子。它将向您展示如何使用 CSV 文件进行参数输入。这是一个简短的例子:

My test.csv file:

我的 test.csv 文件:

1, one
2, two
3, three

My test:

我的测试:

package com.stackoverflow.sourabh.example;

import static org.junit.Assert.assertTrue;
import junitparams.FileParameters;
import junitparams.JUnitParamsRunner;

import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(JUnitParamsRunner.class)
public class FileReadingParameterizedTest {

    @Test
    @FileParameters("src/test/resources/test.csv")
    public void testWithCSV(int number, String name) {
        assertTrue(name + " is not at least two", number >= 2);
    }

}

Obviously the first test will fail, producing the error message one is not at least two.

显然第一个测试会失败,产生错误消息one is not at least two