使用不同参数进行测试

时间:2018-01-19 10:48:48

标签: java unit-testing junit parameterized-unit-test

我有以下测试方案。我想知道哪种测试框架最符合我的要求。

场景1) Param1,Param2,Param3,Param4,Param5

我将使用参数数字 1,2,3 ..直到20传递上述参数。

每个数字(1到20)将生成一个文件,即测试输出数据。我需要将此输出数据与预期数据(也是文件)进行比较,如果两个文件(测试输出文件和预期数据文件)相同,则根据结果生成True,否则为false。

测试输入如下: Param1,Param2,Param3,Param4,Param5,Number,期望数据文件(测试输出将与之比较)

场景2)param1,param2,param3,param4,param5

这里将为上述变量分配不同的值,并将这些值再次传递给测试20次,每次生成不同的Test outputfile(总共20个outputfile),然后将其与预期的数据文件进行比较。 (预期数据也有20个文件。)

我有这15个场景。哪个测试框架最适合这里?参数化Junit是否合适?请提供一些指导原则,以便使用推荐的框架。

3 个答案:

答案 0 :(得分:0)

作为Spock测试的一个例子,其中所有参数和数字可以不同:

@Unroll
def "scenario with different parameters"() {
    given:
    def service = new MyService()

    when:
    def actualDataFile = service.doSomething(param1, param2, param3, param4, param5, number)

    then:
    readFileAsString(actualDataFile) == readFileAsString(expectedDataFileName)

    where:
    param1 | param2 | param3 | param4 | param5 | number | expectedDataFileName
    'any'  | 'aaa'  | 'any'  | 'any'  | 'any'  | 1      | 'expectedA.txt'
    'any'  | 'aay'  | '0ny'  | 'any'  | 'any'  | 2      | 'expectedB.txt'
    'any'  | 'ady'  | '3ny'  | 'a__'  | 'a3y'  | 3      | 'expectedC.txt'
    'any'  | 'ady'  | '3ny'  | 'a__'  | 'a3y'  | 4      | 'expectedD.txt'
    'any'  | 'ady'  | '3ny'  | 'a__'  | 'a3y'  | 5      | 'expectedE.txt'
    'any'  | 'ady'  | '3ny'  | 'a__'  | 'a3y'  | 6      | 'expectedF.txt'
    'any'  | 'ady'  | '3ny'  | 'a__'  | 'a3y'  | 7      | 'expectedG.txt'
    'any'  | 'ady'  | '3ny'  | 'a__'  | 'a3y'  | 8      | 'expectedH.txt'
    // etc
}

答案 1 :(得分:0)

1.哪种测试框架最适合这里?
答:Class Parameterized

2.Will参数化Junit是否合适?
答:是的,你是对的

3.请提供一些指导方针,以便使用推荐的框架 Ans:用于断言实际和预期的结果。
假设您要按字符检查文件char的内容,可以使用FileUtils.contentEquals(file1, file2) commons apache io

让我们看看这个例子:

public class Calc{

    public static int add(int a, int b) {
        return a + b;
    }

}

<强>的Junit

@RunWith(value = Parameterized.class)
public class ParameterizedTest {

    private int numberA;
    private int numberB;
    private int expected;

    // Inject via constructor
    // for {8, 2, 10}, numberA = 8, numberB = 2, expected = 10
    public ParameterizedTest(int numberA, int numberB, int expected) {
        this.numberA = numberA;
        this.numberB = numberB;
        this.expected = expected;
    }

    // name attribute is optional, provide an unique name for test
    // multiple parameters, uses Collection<Object[]>
    @Parameters(name = "{index}: testAdd({0}+{1}) = {2}")
    public static Collection<Object[]> data() {
        return Arrays.asList(new Object[][]{
                {1, 1, 2},
                {2, 2, 4},
                {8, 2, 10},
                {4, 5, 9},
                {5, 5, 10}
        });
    }

    @Test
    public void test_addTwoNumbes() {
        assertThat(Calc.add(numberA, numberB), is(expected));
    }

}

Reference

答案 2 :(得分:0)

通过将Junit参数化测试与某些YAML解析结合起来,我能够 创建可读的参数表。 每个表行将被解析为一个Map,每个值都将使用yaml解析器进行解析。 这样,每个映射都包含类型化的实例。甚至在这种情况下也列出。

@RunWith(Parameterized.class)
public class AnotherParameterizedTest {

    private final HashMap row;

    @Parameterized.Parameters(name="Reverse Lists Tests # {index}:")
    public static List<Map<String, Object>> data() {
        final TestData testData = new TestData(""+
             "|   ID   |       List         |  Expected   |                \n"+
             "|   0    |    [1, 2, 3]       |  [3, 2, 1]  |                \n"+
             "|   1    |    [2, 3, 5]       |  [5, 3, 2]  |                \n"+
             "|   2    |    [5, 6, 7]       |  [ 7, 6, 5] |                \n"
        );
        // parsing each row using simple YAML parser and create map per row
        return testData.getDataTable();
    }

    public AnotherParameterizedTest(HashMap obj) {
        this.row = obj;
    }

    @Test
    public void test() throws Exception {
        List orgListReversed = new ArrayList((List) row.get("List"));
        Collections.reverse(orgListReversed);
        assertEquals((List) row.get("Expected"), orgListReversed);
    }

}

Junit Test Results

相关问题