标签:url ror col 表头 too excel val end inf
# 1.get_data3.py """ 把表头写进excel,获取表头和数据 """ from openpyxl import load_workbook class DoExcel: def __init__(self, file, sheet): self.file = file self.sheet = sheet self.sheet_obj = load_workbook(self.file)[self.sheet] def get_header(self): header = [] for j in range(1, self.sheet_obj.max_column+1): header.append(self.sheet_obj.cell(1, j).value) return header def get_data(self): header = self.get_header() case_data = [] for i in range(2, self.sheet_obj.max_row+1): sub_data = {} for j in range(1, self.sheet_obj.max_column+1): # i=2, j=1\2\3\4 sub_data[header[j-1]] = self.sheet_obj.cell(i, j).value case_data.append(sub_data) return case_data if __name__ == "__main__": header = DoExcel("data_3.xlsx", "sh2").get_header() print(header, type(header)) case_data = DoExcel("data_3.xlsx", "sh2").get_data() print(case_data, type(case_data)) # 2. class_test3.py import unittest from API_AUTO.tools.http_request import HttpRequest class TestLogin(unittest.TestCase): def setUp(self): print("start testing...") def tearDown(self): print("case done") 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("there is an error in the case: {}".format(e)) raise e # 3. run.py import unittest from day_20191202.para_3.class_test3 import TestLogin from day_20191202.para_3.get_data3 import DoExcel import HTMLTestRunner suite = unittest.TestSuite() case_data = DoExcel("data_3.xlsx", "sh2").get_data() for item in case_data: suite.addTest(TestLogin("test_api", item["URL"], item["method"], eval(item["data"]), item["expected"])) print(eval(item["data"]), type(eval(item["data"]))) with open("login_3.html", "wb") as file: runner = HTMLTestRunner.HTMLTestRunner(stream=file, verbosity=2, title="登录3测试报告", description="加油,亲爱的自己") runner.run(suite) # runner = unittest.TextTestRunner(verbosity=2) # runner.run(suite)
表格测试数据如图:
标签:url ror col 表头 too excel val end inf
原文地址:https://www.cnblogs.com/come202011/p/12230898.html