接口测试 junit 参数化测试的问题

围城 · 2017年07月11日 · 最后由 围城 回复于 2017年07月11日 · 1681 次阅读

我这有 4 个 ArrayList 的参数,expCode,expMessage,actCode,actMessage
怎么在 junit 里让这 4 个参数里每个值一一对应去测试,网上找了 junit 参数化测试都 2 个参数的对比

共收到 6 条回复 时间 点赞

是哪个方法啊?

(iexpCode 对比 actCode) && ( expMessage 对比 actMessage) 两个都为 true 的话就是通过 不是吗?

恒温 回复
@RunWith(Parameterized.class)
public class ResultParamTest {
    private int actCode;
    private String actMessage;
    private int expCode;
    private String expMessage;

    public ResultParamTest(int expCode, int actCode, String expMessage, String actMessage) {
        this.expCode = expCode;
        this.actCode = actCode;
        this.expMessage = expMessage;
        this.actMessage = actMessage;
    }

    @Parameterized.Parameters
    public static Collection prepareData() throws IOException {
        ResultParam resultParam = new ResultParam("test_case.xlsx", 0);
        ArrayList<Integer> a = resultParam.expCode();
        ArrayList<Integer> b = resultParam.actCode();
        ArrayList<String> c = resultParam.expMessage();
        ArrayList<String> d = resultParam.actMessage();
        Object[][] objects = {{a.get(0), b.get(0), c.get(0), d.get(0)}, {a.get(1), b.get(1), c.get(1), d.get(1)},........{}....{}};
        return Arrays.asList(objects);
    }

    @Test
    public void testResultParam() throws IOException {
        Assert.assertEquals(expCode, actCode);
        Assert.assertEquals(expMessage, actMessage);
    }
}

求大神告诉下,我要怎么把 a,b,c,d 这些值给赋值到 objects 这个二维数组里去,实在找不到方法了

RunWith(Parameterized.class)
public class ResultParamTest {
    private int actCode;
    private String actMessage;
    private int expCode;
    private String expMessage;

    public ResultParamTest(int expCode, int actCode, String expMessage, String actMessage) {
        this.expCode = expCode;
        this.actCode = actCode;
        this.expMessage = expMessage;
        this.actMessage = actMessage;
    }

    @Parameterized.Parameters
    public static Collection prepareData() throws IOException {
        ResultParam resultParam = new ResultParam("test_case.xlsx", 0);
        ArrayList<Integer> a = resultParam.expCode();
        ArrayList<Integer> b = resultParam.actCode();
        ArrayList<String> c = resultParam.expMessage();
        ArrayList<String> d = resultParam.actMessage();
        Object[][] objects = {{a.get(0), b.get(0), c.get(0), d.get(0)}, {a.get(1), b.get(1), c.get(1), d.get(1)},........{}....{}};
        return Arrays.asList(objects);
    }

    @Test
    public void testResultParam() throws IOException {
        Assert.assertEquals(expCode, actCode);
        Assert.assertEquals(expMessage, actMessage);
    }
}

求大神告诉下,我要怎么把 a,b,c,d 这些值给赋值到 objects 这个二维数组里去,实在找不到方法了

我已经写出来了,你参考下 https://testerhome.com/topics/9306

非常感谢,谢谢大神

围城 关闭了讨论 07月11日 17:23
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册