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

public class Exercise {

//读取文件的函数

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

HashMap> map=new HashMap>();

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> map1=new HashMap>();

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;

}

}

你可能感兴趣的:(TestNG通过在.txt文件中读取参数,测试加法)