一、excel数据驱动
- excel文件内容
- excel数据驱动使用方法
import openpyxl
import pytestdef get_excel():excel_obj = openpyxl.load_workbook("../pytest结合数据驱动-excel/data.xlsx")sheet_obj = excel_obj["Sheet1"]values = sheet_obj.valuescase_list = []for row in values:data = []if row[0] != 'A':for cell in row:data.append(cell)case_list.append(data)print(case_list) return case_listdef my_add(x, y):result = x + yreturn resultclass TestWithExcel:@pytest.mark.parametrize('x,y,expected',get_excel())def test_add(self, x, y, expected):print(f"{x} + {y} = {expected}")assert my_add(int(x), int(y))== int(expected)if __name__ == '__main__':pytest.main(["-s", "test_caseexcel.py"])
- 运行结果