问题的描述来自:
https://testerhome.com/topics/9295
问题的描述是无法获取参数化, 我也心血来潮,自己花了几分钟改正问题
@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);
}
}
改正后的代码:
package com.mj;
import static org.junit.Assert.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import junit.framework.Assert;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
@RunWith(Parameterized.class)
public class CalculatorTest {
private int actCode;
private String actMessage;
private int expCode;
private String expMessage;
public CalculatorTest(int expCode, int actCode, String expMessage,
String actMessage) {
this.expCode = expCode;
this.actCode = actCode;
this.expMessage = expMessage;
this.actMessage = actMessage;
}
@user5ters
public static Collection prepareData() {
ArrayList<Integer> a = new ArrayList<Integer>();
ArrayList<Integer> b = new ArrayList<Integer>();
ArrayList<String> c = new ArrayList<String>();
ArrayList<String> d = new ArrayList<String>();
// 五个正常用例
for (int i = 0; i < 5; i++) {
a.add(i);
b.add(i);
c.add("正确" + i);
d.add("正确" + i);
}
// 五个非正常用例
for (int i = 0; i < 5; i++) {
a.add(i);
b.add(i + 1);
c.add("正确" + i);
d.add("错误" + (i + 1));
}
int size = a.size() - 1;
Object[][] objList = new Object[size][4];
for (int i = 0; i < objList.length; i++) {
objList[i][0] = a.get(i);
objList[i][1] = b.get(i);
objList[i][2] = c.get(i);
objList[i][3] = d.get(i);
}
return Arrays.asList(objList);
}
@Test
public void test() {
System.err.println("expCode is " + expCode + " actCode is " + actCode
+ " expMessage is " + expMessage + " actMessage is "
+ actMessage);
assertEquals(expCode, actCode);
assertEquals(expMessage, actMessage);
}
}
运行打印数据:
expCode is 0 actCode is 0 expMessage is 正确 0 actMessage is 正确 0
expCode is 1 actCode is 1 expMessage is 正确 1 actMessage is 正确 1
expCode is 2 actCode is 2 expMessage is 正确 2 actMessage is 正确 2
expCode is 3 actCode is 3 expMessage is 正确 3 actMessage is 正确 3
expCode is 4 actCode is 4 expMessage is 正确 4 actMessage is 正确 4
expCode is 0 actCode is 1 expMessage is 正确 0 actMessage is 错误 1
expCode is 1 actCode is 2 expMessage is 正确 1 actMessage is 错误 2
expCode is 2 actCode is 3 expMessage is 正确 2 actMessage is 错误 3
expCode is 3 actCode is 4 expMessage is 正确 3 actMessage is 错误 4
因为我对群友的业务不熟悉,加上我没有 ResultParam resultParam = new ResultParam("test_case.xlsx", 0); 这个类的代码,我就自己制作了点数据。
其实这里我只是简单的测试,应该用等价类,边界值加上点数据的,只是在上班时间,顺带赶紧记录下。