标签:run python 从表 处理异常 定位 stream 单元 add imp
# 方法1:把所有数据一次都读到字典中,方便后期处理,推荐使用 # 一次性读取所有数据,对内存的要求会高一些 # 1.get_data.py from openpyxl import load_workbook class DoExcel: def __init__(self, file, sheet): self.file = file self.sheet = sheet def get_data(self): # 打开工作簿 wb = load_workbook(self.file) # 定位表单 sheet = wb[self.sheet] # 把每一行的数据放到字典中,再把字典放在列表中 case_data = [] for i in range(1, sheet.max_row+1): sub_data = {} sub_data["url"] = sheet.cell(i, 1).value sub_data["method"] = sheet.cell(i, 2).value sub_data["data"] = eval(sheet.cell(i, 3).value) sub_data["expected"] = sheet.cell(i, 4).value case_data.append(sub_data) return case_data if __name__ == ‘__main__‘: print(DoExcel("data_1.xlsx", "sh2").get_data()) # 2. class_test_1.py import unittest from API_AUTO.tools.http_request import HttpRequest class TestLogin(unittest.TestCase): def setUp(self): print("开始执行测试用例") def tearDown(self): print("用例执行完毕") def __init__(self, methodName, url, method, data, expected): super(TestLogin, self).__init__(methodName) self.url = url self.method = method self.data = data self.expected = expected def test_api(self): res = HttpRequest().http_request(self.url, self.method, self.data) r = res.json()["info"] try: self.assertEqual(r, self.expected) except AssertionError as e: print("test_case‘s error is {}".format(e)) raise e # 处理异常后抛出去,才能被检测到用例失败了 if __name__ == ‘__main__‘: unittest.main() # 3.run.py # 测试套件 import unittest import HTMLTestRunner from class_test_1 import TestLogin from get_data import DoExcel suite = unittest.TestSuite() data = DoExcel("data_1.xlsx", "sh2").get_data() for item in data: suite.addTest(TestLogin("test_api", item["url"], item["method"], item["data"], item["expected"])) with open("login_1.html", "wb") as file: runner = HTMLTestRunner.HTMLTestRunner(stream=file, verbosity=2, title="登录测试报告1", description="加油,胜利的曙光就在眼前") runner.run(suite)
表格的测试数据如图:
标签:run python 从表 处理异常 定位 stream 单元 add imp
原文地址:https://www.cnblogs.com/come202011/p/12230840.html