python做数据驱动

python代码如下:

import unittest
from openpyxl import load_workbook
from openpyxl.styles import Font
from openpyxl.styles.colors import RED, GREEN


wb = load_workbook(r"C:\Users\test\Desktop\q.xlsx")
ws = wb.get_sheet_by_name('testData')
rows = ws.max_row

red = Font(name='Calibri', size=14, bold=False, italic=False, vertAlign=None, underline='none', strike=False, color=RED)

Gr = Font(name='Calibri', size=14, color=GREEN)


def add(num1, num2):
return num1 + num2


class TestGetIP(unittest.TestCase):
total_count = 0
success_count = 0
fail_count = 0

def testRun(self):
for row in range(2, rows+1):
self.total_count += 1
name = ws.cell(row=row, column=1).value
param1 = ws.cell(row=row, column=2).value
param2 = ws.cell(row=row, column=3).value
ep = ws.cell(row=row, column=4).value
with self.subTest(msg=name):
try:
self.assertEqual(ep, add(param1, param2))
ws.cell(row=row, column=5).value = "pass"
ws.cell(row=row, column=5).font = Gr
self.success_count += 1
except Exception as e:
ws.cell(row=row, column=5).value = str(e)
ws.cell(row=row, column=5).font = red
self.fail_count += 1
result = "用例执行结果:" \
"共执行%d条用例,成功%d条,失败%d条" % (self.total_count, self.success_count, self.fail_count)
ws.cell(row=rows + 1, column=6).value = result
print()
print(result)
wb.save(r"C:\Users\h\Desktop\q.xlsx")

运行结果如下:

 

本地数据如下:

 

 


posted @ 2018-03-30 00:53  &阿岩  Views(548)  Comments(0Edit  收藏  举报