def test_add_loan(self, case): method = case["Method"] url = case["Url"] param = eval(get_data.re_p(case["Params"])) print(param) my_log.info('-------正在测试{}模块里面第{}条测试用例:{}'.format(case['Module'], case['CaseId'], case['Title'])) my_log.info('测试数据是:{}'.format(case)) res = HttpRequest().http_request(method, url, param, cookies=getattr(GetData, "COOKIES")) if case["sql"] is not None: loan_id = DoMysql().do_mysql(eval(case["sql"])["sql"], 1) setattr(GetData, "loan_id", str(loan_id[0])) # loanid与Excel中的保持一致。从数据库查到的loan_id是int, 因为要用到正则表达式替换,所以改成str # 但是为啥这里是loan_id,而gte_data.py里面的是LOANID也可以? excel、这里、还有get_data.py保持一致,似乎不分大小写 if res.cookies: setattr(GetData, "COOKIES", res.cookies) try: self.assertEqual(json.loads(case["ExpectedResult"]), res.json()) test_result = "Pass" # except Exception as e: except AssertionError as e: test_result = "Faild" my_log.error("http请求出错了,错误是{}".format(e)) raise e finally: self.t.write_back(case["CaseId"] + 1, 9, res.text) self.t.write_back(case["CaseId"] + 1, 10, test_result) my_log.info('实际结果:{}'.format(res.json())) # http发送请求拿到的实际返回值
def test_login(self, case): method = case['Method'] url = case['Url'] param = eval(case['Params']) res = HttpRequest().http_request(method, url, param, cookies=None) try: # self.assertEqual(case["ExpectedResult"], res.json()) # self.assertEqual(json.loads(case["ExpectedResult"]), res.json()) # 用了json.loads()包括Excel都不要用单引号,全部用双引号 self.assertEqual(json.loads(case["ExpectedResult"]), res.json()) test_result = "Pass" except Exception as e: my_log.error("http请求出错了,错误是:{}".format(e)) test_result = "Failed" raise e # 抛出错误,否则测试报告那里全部都是通过 finally: self.t.write_back(case["CaseId"] + 1, 9, res.text) self.t.write_back(case["CaseId"] + 1, 10, test_result) my_log.info('实际结果是:{}'.format(res.json()))
def test_recharge(self, case): method = case["Method"] url = case["Url"] param = eval(get_data.re_p(case["Params"])) my_log.info('-------正在测试{}模块里面第{}条测试用例:{}'.format( case['Module'], case['CaseId'], case['Title'])) my_log.info('测试数据是:{}'.format(case)) if case["sql"] is not None: sql = eval(case["sql"])["sql"] before_amount = DoMysql().do_mysql(sql, 1)[0] res = HttpRequest().http_request(method, url, param, getattr(GetData, "COOKIES")) if res.cookies: setattr(GetData, "COOKIES", res.cookies) if case["sql"] is not None: sql = eval(case["sql"])["sql"] after_amount = DoMysql().do_mysql(sql, 1)[0] recharge_amount = int(param["amount"]) expect_amount = before_amount + recharge_amount self.assertEqual(expect_amount, after_amount) if case["ExpectedResult"].find("exp_amount") > -1: # case["ExpectedResult"].replace("exp_amount", str(expect_amount)) #错了 case['ExpectedResult'] = case['ExpectedResult'].replace( 'exp_amount', str(expect_amount)) try: self.assertEqual(json.loads(case["ExpectedResult"]), res.json()) TestResult = "Pass" except AssertionError as e: TestResult = "Failed" raise e finally: self.t.write_back(case["CaseId"] + 1, 9, res.text) self.t.write_back(case["CaseId"] + 1, 10, TestResult) my_log.info('实际结果是:{}'.format(res.json()))
def tearDown(self) -> None: my_log.info("一条测试用例执行完毕")
def setUp(self) -> None: self.t = do_excel.DoExcel(project_path.case_path, "login") my_log.info("开始执行新一条测试用例")