一、csv数据驱动
- csv文件内容
1,1,2
3,6,9
100,200,3000
- csv数据驱动使用方法
import csv
import pytestdef get_csv():with open("data.csv") as file:raw = csv.reader(file)data = []for line in raw:data.append(line)# print(data) #[['1', '1', '2'], ['3', '6', '9'], ['100', '200', '3000']]return datadef my_add(x, y):result = x + yreturn resultclass TestWithCsv:@pytest.mark.parametrize('x,y,expected',get_csv())def test_add(self, x, y, expected):# print(x,y,expected)print(f"{x} + {y} = {expected}")assert my_add(int(x), int(y))== int(expected)if __name__ == '__main__':pytest.main(["-s", "test_csv.py"])
- 运行结果