一、excel数据驱动
- excel文件内容
- excel数据驱动使用方法
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)
return case_list
def my_add(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(f"{x} + {y} = {expected}")
assert my_add(int(x), int(y))== int(expected)
if __name__ == '__main__':
pytest.main(["-s", "test_caseexcel.py"])
- 运行结果