Junit测试一些参数不同而其他参数保持不变

时间:2017-11-21 11:52:40

标签: java junit junit-runner

我有一个junit测试的以下输入参数。

基本上我需要测试一个算法,该算法将inputFile和其他少量参数作为输入并产生一些数据。此数据需要与referenceData进行比较(referenceData文件也是测试的输入参数之一)。如果算法生成的数据与参考数据相同,则测试通过,否则失败。

inputFile // .xml File - is different for each test. there are total five.
param 1   //remains same
param 2   //remains same
param 3   //remains same
param 4   //remains same
param 5   //remains same
ReferenceData // .csv File - is different for each test. there are total five

我的困惑是:

1)参数化的jUnit是否适合这种情况?如果是的话,有些人可以提供一些指导方针,我应该如何实施呢? #

2)如果jUnit不适合这种情况,那我还能用什么?

3)我应该在junit测试的setUp方法中从.properties文件中读取这些参数吗?这是一个好习惯吗?

1 个答案:

答案 0 :(得分:2)

您可以使用JUnitParams lib。

来实现此目的

将xml和csv文件放入项目的/src/test/resources文件夹中(对maven / gradle项目有效)。

并在测试中使用它们:

@RunWith(JUnitParamsRunner.class)
public class ServiceTest {

    @Test
    @Parameters({
            "first.xml, first.csv",
            "second.xml, second.csv",
            "third.xml, third.csv"
    })
    public void shouldServe(String xmlFilePath, String csvFilePath) {
        String xmlFileContent = readContent(xmlFilePath);
        String csvFileContent = readContent(csvFilePath);

        // call your business method here passing 
        // dynamic xml, csv and static params
    }
}

其中readContent是从文本文件中读取内容的方法。

相关问题