TestNG通过在.txt文件中读取参数,测试加法

public class Exercise {

//读取文件的函数

public static Map> readFile(File file) throws NumberFormatException, IOException {

HashMap<Integer,ArrayList<Integer>> map=new HashMap<Integer,ArrayList<Integer>>();

BufferedReader br = new BufferedReader((new InputStreamReader(new FileInputStream(file), "UTF-8")));

String lineTxt = null;

int y = 0;  //定义Map的Key值

while ((lineTxt = br.readLine()) != null) {ArrayListlist = new ArrayList();

System.out.println("文件行数为:" + lineTxt);

if (lineTxt.length() == 1)

continue;

for (Integer i = 0; i < 3; i++) {

String[] names = lineTxt.split(",");

Integer[] namesInt = new Integer[names.length];

namesInt[i] = Integer.parseInt(names[i].trim());

list.add(namesInt[i]); //将文本中的每一行读取到list中

}

map.put(y, list);   //将List作为Value放入map中

y++;

}

br.close();

return map;

}

//判断后两个值相加是否等于第一个值

public void function1(int expected, int param1, int param2) {

Calculator1 calculator1 = new Calculator1();

int result;

result = calculator1.Add(param1, param2);

System.out.println("getResult:" + result);

if (expected == result) {

System.out.println("测试通过");

} else {

System.out.println("测试未通过");

}

}


@Test

public void testAdd() {

int expected = 0;

int param1 = 0;

int param2 = 0;

Exercise exercise = new Exercise();

File file = new File("d:\\a.txt");

HashMap<Integer,ArrayList<Integer>> map1=new HashMap<Integer, ArrayList<Integer>>();

try {

map1 = readFile(file);

} catch (Exception e) {

e.printStackTrace();

System.out.println("文件读取失败");

}

for (int key : map1.keySet()) {ArrayListvalue = map1.get(key);List ActualValue = value;

System.out.println("key值为:" + key);

expected = ActualValue.get(0);  //list中第一个值为预期结果

param1 = ActualValue.get(1);  //list中第二个值为加法的第一个参数

param2 = ActualValue.get(2); //list中第三个值为加法的第二个参数

System.out.println("expected: " + expected + "param1: " + param1 + "param2: " + param2);

exercise.function1(expected, param1, param2);      

}

}

}


加法类为

public class Calculator1 {

int result;

public int Add(int A,int B){

result=A+B;

return result;

}

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容