class TestLogin(unittest.TestCase): def setUp(self): self.t = DoExcel(DATA_PATH, "login") self.logger = MyLog("root") @data(*test_date) def test_login(self, item): self.logger.info("******************************") self.logger.info("正在执行的用例是 {}".format(item["title"])) self.logger.info("请求的数据是:{0}".format(item["data"])) res = HttpRequest().http_request(item["url"], eval(item["data"]), item["method"], item["type"]) try: self.assertEqual(item["ExpectResult"], res.json()["message"]) TestResult = "PASS" # 如果不报错,测试通过 except AssertionError as e: print("接口错误,错误是{}".format(e)) TestResult = "Fail" # 如果报错了,测试不通过 finally: # 不管测试结果是否正确,都把结果写入文件 self.logger.info("*********开始写入结果********") self.t.write_back(item["case_id"] + 1, 8, str(res.json()["message"])) # 写入实际结果 self.t.write_back(item["case_id"] + 1, 9, TestResult) # 写入测试结果 self.logger.info("*********结束写入数据********") # print(res.text) def tearDown(self): pass
class TestBayonet(unittest.TestCase): def setUp(self): self.BayonetDate = DoExcel(DATA_PATH, "bayonet") self.logger = MyLog("root") @data(*Data) def test_bayonet(self, item): self.logger.info("***********************") self.logger.info("正在执行的用例是{}".format(item["title"])) self.logger.info("请求的数据是:{}".format(item["data"])) res = requests.post(item["url"], json=eval(item["data"]), headers=headers) self.logger.info("接口返回的结果是{}".format(res.json())) try: self.assertEqual(item["ExpectResult"], res.json()["message"]) TestResult = "PASS" # 如果不报错,测试通过 except AssertionError as e: print("接口错误,错误是{}".format(e)) TestResult = "Fail" # 如果报错了,测试不通过 finally: # 不管测试结果是否正确,都把结果写入文件 self.logger.info("*********开始写入结果********") self.BayonetDate.write_back(item["case_id"] + 1, 8, str(res.json()["message"])) # 写入实际结果 self.BayonetDate.write_back(item["case_id"] + 1, 9, TestResult) # 写入测试结果 self.logger.info("*********结束写入数据********") # print(res.text) def tearDown(self): pass
class TestHttpRequest(unittest.TestCase): def setUp(self): self.t = DoExcel(pro_path.test_data_path, 'Sheet2') #创建一个实例 @data(*test_data) def test_api(self, item): global COOKIES #声明全局变量,必要的时候去更新从cookie的值 logging.info('正在执行第{0}条用例:{1}'.format(item['CaseId'], item['Title'])) logging.info('发起请求的地址是:{0}'.format(item['URL'])) logging.info('发起请求的参数是:{0}'.format(item['Param'])) res = HttpRequest().http_request(item["URL"], eval(item["Param"]), item["Method"], cookies=COOKIES) #登录请求之后会产生一个cookie if res.cookies != {}: #如果cookie不为空 就对全局变量进行修改 COOKIES = res.cookies # 断言 try: self.assertEqual(item['ExpectedResult'], res.json()['Message']) TestResult = 'PASS' #存储测试用例的执行结果 通过是PASS 失败是fail except Exception as e: logging.error('出错了,错误是:{0}'.format(e)) TestResult = 'FAIL' raise e finally: self.t.write_back(item['CaseId'] + 1, 7, str( res.json())) #因为Excel里面只能支持传递字符串和整数,所以这里要强制转换一下 self.t.write_back(item['CaseId'] + 1, 8, TestResult)
def test_api(self, item): passorfail = None #请求之前完成loanid的替换 if item['data'].find('${loanid}') != -1: if getattr(GetData, 'loanid') == None: id = DoMysql.cnn_db( 'select max(id) from loan where memberid=82')[0] item['data'] = str(item['data']).replace('${loanid}', str(id)) setattr(GetData, 'loanId', id) #利用反射去存储结果 else: item['data'] = str(item['data']).replace( '${loanid}', str(getattr(GetData, 'LoanId'))) #判断sql语句 if item['sql'] == None: #没有sql语句时 print('正在执行的用例是:第{}条用例'.format(item['case_id'])) res = HttpRequest().http_request(item['url'], eval(item['data']), item['http_method'], getattr(GetData, 'COOKIE')) else: #有sql语句时 print('正在执行的用例是:第{}条用例'.format(item['case_id'])) sql = eval(item['sql'])['sql'] #http请求前 before_amount = DoMysql.cnn_db(sql)[0] res = HttpRequest().http_request(item['url'], eval(item['data']), item['http_method'], getattr(GetData, 'COOKIE')) #http请求后 after_amount = DoMysql.cnn_db(sql)[0] amount = abs(after_amount - before_amount) if str(amount) == str(eval(item['data'])['amount']): check_res = '数据库校验正确' else: check_res = '数据库校验失败' DoExcel.write_checkres(testcase_path, item['interface'], item['case_id'], check_res) print(res.json()) if res.cookies: setattr(GetData, 'COOKIE', res.cookies) try: self.assertEqual(str(item['excepted']), res.json()['code']) # 断言,没有断言默认不管对错测试用例都执行通过 passorfail = "成功" except Exception as e: print('失败用例{},{}:{}'.format(item['interface'], item['case_id'], res.json())) passorfail = "失败" raise e finally: DoExcel.write_back(testcase_path, item['interface'], item['case_id'], str(res.json()), passorfail)