一、excel数据驱动
- excel文件内容
- excel数据驱动使用方法
python
import openpyxl
import pytest
def get_excel():
excel_obj = openpyxl.load_workbook("../pytest结合数据驱动-excel/data.xlsx")
sheet_obj = excel_obj["Sheet1"]
values = sheet_obj.values
case_list = []
for row in values:
data = []
if row[0] != 'A':
for cell in row:
data.append(cell)
case_list.append(data)
print(case_list) #[[1, 1, 2], [3, 6, 9], [100, 200, 300]]
return case_list
def my_add(x, y):
# print(x,y)
result = x + y
return result
class TestWithExcel:
@pytest.mark.parametrize('x,y,expected',get_excel())
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_caseexcel.py"])
- 运行结果