def test_bidload(self, case): logger.info("----测试用例:{}开始执行----".format(case.title)) case.data = Context().replace(case.data) if case.check_sql: money = self.DB.fetch_one(eval(case.check_sql)["sql1"]) print(money) invest_before = money['LeaveAmount'] resp = self.http_request.api(case.method, case.url, case.data) actual = resp.json()['code'] print(resp.text) print("actual", type(actual)) try: self.assertEqual(str(case.expected), actual) Do_Excel(contants_path.excel_dir, 4).write_excel(case.case_id + 1, resp.text, "PASS") if resp.json()['msg'] == '加标成功': sql = 'SELECT ID FROM future.loan WHERE memberId="93870" ORDER BY id DESC LIMIT 1' loan_id = self.DB.fetch_one(sql)['ID'] #返回的是字典,根据字典去对应的值 setattr(Context, "loan_id", str(loan_id)) #如果属性不存在会创建一个新的对象属性,并对属性赋值 if case.check_sql: #验证数据库 money = self.DB.fetch_one(eval(case.check_sql))['sql1'] invest_after = money['LeaveAmount'] amout = eval(case.data)['LeaveAmount'] #获取到需要充值的金额 self.assertEqual(float(amout), float(invest_before) - float(invest_after)) logger.info("----数据库验证成功!!!----") except AssertionError as e: Do_Excel(contants_path.excel_dir, 4).write_excel(case.case_id + 1, resp.text, "PASS") logger.error("----出错了:测试的预期结果!=实际结果---") raise e
class Runner: def __init__(self, sheet_cases): self.do_excel = Do_Excel(GetPath().get_datas_path()) self.sheet_names = self.do_excel.get_all_sheet_names() # 获取所有表单名字 self.sheet_cases = sheet_cases #指定需要执行的模块,可通过配置项进行修改 #获取需要执行表单数据 def get_execl_datas(self): #便利表单 for sheet_name in self.sheet_names: # 确认需要执行的表单 if sheet_name in self.sheet_cases: #返回表单数据->列表对象,以及表单名->字符串 return self.do_excel.get_cases(sheet_name), sheet_name
def test_success(self, case): logger.info("----测试用例:{}开始执行-----".format(case.title)) case.data = Context().replace(case.data) resp = self.http_request.api(case.method, case.url, case.data) try: self.assertEqual(case.expected, resp.text) Do_Excel(contants_path.excel_dir, 1).write_excel(case.case_id + 1, resp.text, "PASS") except AssertionError as e: Do_Excel(contants_path.excel_dir, 1).write_excel(case.case_id + 1, resp.text, "FAILED") logger.info("---测试预期结果和实际结果不一致!!---".format(e)) raise e logger.info("-----测试结束了------")
class Login_Test(unittest.TestCase): excel = Do_Excel(contants_path.excel_dir, 1) cases = excel.read_excel() @classmethod def setUpClass(cls): logger.info("----测试前置开始----") cls.http_request = Session_api() @data(*cases) def test_success(self, case): logger.info("----测试用例:{}开始执行-----".format(case.title)) case.data = Context().replace(case.data) resp = self.http_request.api(case.method, case.url, case.data) try: self.assertEqual(case.expected, resp.text) Do_Excel(contants_path.excel_dir, 1).write_excel(case.case_id + 1, resp.text, "PASS") except AssertionError as e: Do_Excel(contants_path.excel_dir, 1).write_excel(case.case_id + 1, resp.text, "FAILED") logger.info("---测试预期结果和实际结果不一致!!---".format(e)) raise e logger.info("-----测试结束了------") @classmethod def tearDownClass(cls): cls.http_request.close() #请求关闭 logger.info("----测试后置结束----")
def test_add(self,case): logger.info("----测试用例{}开始执行----".format(case.title)) case.data=Context().replace(case.data) print("casedata",case.data) resp = self.http_request.api(case.method,case.url,case.data) if case.check_sql: before_data=self.DB.fetch_one (eval((case.check_sql))['sql1']) try: self.assertEqual(case.expected,resp.json()['code']) Do_Excel(contants_path.excel_dir,5).write_excel(case.case_id+1,resp.text,"PASS") if case.check_sql: after_data=self.DB.fetch_one (eval((case.check_sql))['sql1']) self.assertEqual(before_data,after_data) logger.info("----数据库验证通过!!!-----") except AssertionError as e: Do_Excel(contants_path.excel_dir,5).write_excel(case.case_id+1,resp.text, "FAILED") logger.error("----测试出错了,预期结果!=实际结果") raise e logger.info("----测试结束了-----")
class Register_Test(unittest.TestCase): excel = Do_Excel(contants_path.excel_dir, 2) cases = excel.read_excel() @classmethod def setUpClass(cls): cls.http_request = Session_api() cls.DB = Do_Mysql() @data(*cases) def test_recharge(self, case): logger.info("----测试用例 {}开始执行----".format(case.title)) case.data = Context().replace(case.data) print(case.data) if case.check_sql: #获取充值前的金额 ---excel取出来的数据是字符串,要用eval函数转成字典后,获取字典的value值 money = self.DB.fetch_one(eval(case.check_sql)["sql1"]) before_money = money["LeaveAmount"] logger.info("充值前的金额{}".format(type(before_money))) resp = self.http_request.api(method=case.method, url=case.url, data=case.data) try: self.assertEqual(str(case.expected), resp.json()['code']) Do_Excel(contants_path.excel_dir, 2).write_excel(case.case_id + 1, resp.text, "PASS") if case.check_sql: # 获取充值前的金额 ---excel取出来的数据是字符串,要用eval函数转成字典后,获取字典的value值 money = self.DB.fetch_one(eval(case.check_sql)["sql1"]) after_money = money["LeaveAmount"] logger.info("充值后的金额{}".format(type(after_money))) amount = (eval(case.data))["amount"] #数据库的断言,用充值后的金额-充值前的金额=实际充值的金额 #amount转换后取到的类型是int型,转成float型,充值前的金额和充值后的金额全部转成float,然后再金额结果的比对 self.assertEqual(float(amount), (float(after_money) - float(before_money))) print("最后充值的金额是{}".format( float(after_money) - float(before_money))) #最后获取的金额的类型是浮点型 except AssertionError as e: Do_Excel(contants_path.excel_dir, 2).write_excel(case.case_id + 1, resp.text, "FAILED") logger.error("---测试预期结果和实际结果不一致!!---".format(e)) raise e logger.info("-----测试结束了-----") @classmethod def tearDownClass(cls): cls.http_request.close() cls.DB.close() logger.info("----测试后置结束----")
class Add_Test(unittest.TestCase): excel=Do_Excel(contants_path.excel_dir,5) cases=excel.read_excel() @classmethod def setUpClass(cls): logger.info("----测试前置开始----") cls.http_request=Session_api() cls.DB=Do_Mysql() @data(*cases) def test_add(self,case): logger.info("----测试用例{}开始执行----".format(case.title)) case.data=Context().replace(case.data) print("casedata",case.data) resp = self.http_request.api(case.method,case.url,case.data) if case.check_sql: before_data=self.DB.fetch_one (eval((case.check_sql))['sql1']) try: self.assertEqual(case.expected,resp.json()['code']) Do_Excel(contants_path.excel_dir,5).write_excel(case.case_id+1,resp.text,"PASS") if case.check_sql: after_data=self.DB.fetch_one (eval((case.check_sql))['sql1']) self.assertEqual(before_data,after_data) logger.info("----数据库验证通过!!!-----") except AssertionError as e: Do_Excel(contants_path.excel_dir,5).write_excel(case.case_id+1,resp.text, "FAILED") logger.error("----测试出错了,预期结果!=实际结果") raise e logger.info("----测试结束了-----") @classmethod def tearDownClass(cls): logger.info("----测试后置结束----") cls.http_request.close() cls.DB.close()
class InvestTest(unittest.TestCase): excel = Do_Excel(contants_path.excel_dir, 4) cases = excel.read_excel() @classmethod def setUpClass(cls): cls.http_request = Session_api() cls.DB = Do_Mysql() @data(*cases) def test_invest(self, case): print("开始执行测试:", case.title) # 在请求之前替换参数化的值 case.data = Context().replace(case.data) resp = self.http_request.api(case.method, case.url, case.data) try: self.assertEqual(str(case.expected), resp.json()['code']) self.excel.write_excel(case.case_id + 1, resp.text, 'PASS') # 判断加标成功之后,查询数据库,取到loan_id if resp.json()['msg'] == "加标成功": sql = { 'sql1': 'SELECT * FROM future.loan WHERE memberId="93870" ORDER BY id DESC LIMIT 1' } loan_id = self.DB.fetch_one(sql)['sql'] print('标的ID:', loan_id) # 保存到类属性里面 setattr(Context, "loan_id", str(loan_id)) except AssertionError as e: self.excel.write_excel(case.case_id + 1, resp.text, 'FAIL') raise e @classmethod def tearDownClass(cls): cls.http_request.close()
class Test_Search_Student: _do_excel = Do_Excel(username_dir, 'user_s') _cases = _do_excel.read_excel() _setup_datas = get_env() _working = _get_working() if _working == "port": def setup(self): ''' 開啓調試端口啓用 ''' self.main = Main() def teardown(self): ''' 開啓調試端口啓用 ''' try: self.main.close_drawer() except Exception as e: pass else: def setup_class(self): ''' 非調試端口用 ''' self.main = Main().goto_login(). \ username(self._setup_datas["username"]).password(self._setup_datas["password"]).save() def teardown(self): try: self.main.close_drawer() except Exception as e: pass def teardown_class(self): ''' 非調試端口啓用 ''' self.main.close() @pytest.mark.parametrize("data", _cases) def test_search_student_username(self, data): ''' 查找學生數量,驗證賬號存在 ''' result = self.main.goto_unified_data(). \ goto_user().goto_student(). \ search_user_s(data.username).\ get_the_fir_row_text(data.username) assert True == result @pytest.mark.parametrize("data", _cases) def test_get_staffNo_for_search_username(self, data): ''' 查找學生賬號,返回學生學號 ''' try: result = self.main.goto_unified_data(). \ goto_user().goto_student(). \ search_user_s(data.username).get_the_first_staffNo(data.username) db = shelve.open("staffNo_s") self._do_excel.write_excel(data.id + 1, 3,db["staffNo_s"]) db.close() assert True == result except AssertionError as e: self._do_excel.write_excel(data.id + 1, 3,'None') raise e
# @function : 充值模块 """ import json import unittest from ddt import ddt, data from common import request from common.main import Runner from common.loggers import Log from common.do_excel import Do_Excel from common.contants import GetPath from common.basic_data import Context # from testcase.mysql_datas import SelectPhone datas, sheet_name = Runner('recharge').get_execl_datas() get_do_excel = Do_Excel(GetPath().get_datas_path()) @ddt class TestRecharge(unittest.TestCase): # classmethod # def setUpClass(cls): # sql = 'SELECT * FROM future.member WHERE member.MobilePhone = 15680978629' # SelectPhone() @data(*datas) def test_recharge(self, item): python_dict = json.loads(item.request_data) try: # 调用url进行拼接
def __init__(self, sheet_cases): self.do_excel = Do_Excel(GetPath().get_datas_path()) self.sheet_names = self.do_excel.get_all_sheet_names() # 获取所有表单名字 self.sheet_cases = sheet_cases #指定需要执行的模块,可通过配置项进行修改
class Test_Search_Teacher: _do_excel = Do_Excel(username_dir, 'user_t') _cases = _do_excel.read_excel() _setup_datas = get_env() _working = _get_working() if _working == "port": def setup(self): ''' 開啓調試端口啓用 ''' self.main = Main() def teardown(self): ''' 開啓調試端口啓用 ''' try: self.main.close_drawer() except Exception as e: pass else: def setup_class(self): ''' 非調試端口用 ''' self.main = Main().goto_login(). \ username(self._setup_datas["username"]).password(self._setup_datas["password"]).save() def teardown(self): try: self.main.close_drawer() except Exception as e: pass def teardown_class(self): ''' 非調試端口啓用 ''' self.main.close() @pytest.mark.parametrize("data", _cases) def test_search_teacher_username(self, data): ''' 查找老師數量,驗證賬號存在 ''' result = self.main.goto_unified_data(). \ goto_user().goto_teacher(). \ search_user_t(data.username).\ get_the_fir_row_text(data.username) assert True == result @pytest.mark.parametrize("data", _cases) def test_get_post_for_search_username(self, data): ''' 查找老師賬號,返回老師崗位 ''' try: result = self.main.goto_unified_data(). \ goto_user().goto_teacher(). \ search_user_t(data.username).get_the_first_user_t_post(data.username) db = shelve.open("post_t") self._do_excel.write_excel(data.id + 1, 3, db["post_t"]) db.close() assert "崗位" in result except AssertionError as e: self._do_excel.write_excel(data.id + 1, 3, 'None') raise e @pytest.mark.parametrize("data", _cases) def test_get_emali_for_search_username(self, data): ''' 查找老師賬號,返回老師崗位 注意:uat打開抽屜定位到元素需要等待5s以上,若在獲取郵箱前等待,則獲取郵箱后無需等待 ''' try: result = self.main.goto_unified_data(). \ goto_user().goto_teacher(). \ search_user_t(data.username).view_the_first_user_t(data.username).\ wait_sleep(5).get_email().close_page().get_the_first_user_t_user(data.username) db = shelve.open("email_t") self._do_excel.write_excel(data.id + 1, 4, db["email_t"]) db.close() assert data.username in result except AssertionError as e: self._do_excel.write_excel(data.id + 1, 4, 'None') raise e