def time_sleep(seconds): log = MyLog().my_log() log.info('进入时间等待方法') if seconds.isdigit() or isinstance(seconds, int): time.sleep(int(seconds)) log.info('等待时间%s秒' % seconds) else: log.info('参数seconds输入错误')
class TestAccount(unittest.TestCase): def setUp(self): self.logger=MyLog() self.logger.info('开始测试') def tearDown(self): print('用例结束') self.logger.info('用例结束') @data(*test_data) def test_account(self,data_item): print('正在进行第{0}条用例:{1}'.format(data_item['id'], data_item['description'])) self.logger.info('正在进行第{0}条用例:{1}'.format(data_item['id'], data_item['description'])) params = eval(data_item['param']) params['access_token']=GetInfoData().get_token() print('测试数据为{0}'.format(params)) self.logger.info('测试数据为{0}'.format(params)) res = HttpRequtser().http_request(data_item['url'], params, data_item['HttpMethod'], verify=False, headers=GetInfoData().get_hander()) print('测试结果是{0}'.format(res.json())) self.logger.info('测试结果是{0}'.format(res.json())) try: self.assertEqual(str(data_item['ExpectedResult']),str(res.json()['error_code'])) self.assertEqual(res.status_code,200) print('用例通过') self.logger.info('用例通过') TestResult='PASS' Comment=None except AssertionError as e: print('用例失败错误是{}'.format(e)) self.logger.error('用例失败错误是{}'.format(e)) TestResult = 'Failed' Comment=res.json()['error_message'] raise e finally: DoExce(project_path.test_case_path).write_back('login',data_item['id']+1,res.json()['error_code'],TestResult,Comment)
class TestCommunity(unittest.TestCase): '''社区''' def setUp(self): self.logger=MyLog() self.logger.info('开始测试') @data(*test_data) def test_community(self, data_item): self.logger.info('正在进行第{0}条用例:{1}'.format(data_item['id'], data_item['description'])) #查找tid,cid,rid并替换 data_item=replace_data(data_item) # if data_item['param'].find('${tid}')!=-1: # data_item['param']=data_item['param'].replace('${tid}', str(getattr(GetInfoData,'RID'))) params = eval(data_item['param']) #添加token params['access_token'] = GetInfoData().get_token() self.logger.info('测试数据为{0}'.format(params)) #发送请求 res = HttpRequtser().http_request(data_item['url'],params , data_item['HttpMethod'], verify=False, headers=GetInfoData().get_hander()) self.logger.info('测试结果是{0}'.format(res.json())) try: #断言 self.assertEqual(str(data_item['ExpectedResult']),str(res.json()['error_code'])) self.assertEqual(res.status_code,200) self.logger.info('用例通过') TestResult='PASS' Comment=None if data_item['description'] == '发布动态': tid = res.json()['data']['tid'] # 替换info里的tid global RID,TID,CID setattr(GetInfoData,'TID',tid) except AssertionError as e: self.logger.error('用例失败错误是{}'.format(e)) TestResult = 'Failed' Comment=res.json()['error_message'] raise e finally: DoExce(project_path.test_case_path).write_back('community',data_item['id']+1,res.json()['error_code'],TestResult,Comment) def tearDown(self): self.logger.info('用例结束')
class TestLogin(unittest.TestCase): def setUp(self): self.logger = MyLog() self.logger.info('开始测试') print('开始测试') @data(*test_data) def test_login(self, data_item): global TOKEN print('正在进行第{0}条用例:{1}'.format(data_item['id'], data_item['description'])) self.logger.info('正在进行第{0}条用例:{1}'.format(data_item['id'], data_item['description'])) params = eval(data_item['param']) if data_item['module'] == '登录': if params['mobilecaptcha'] == 'code': #去数据库查询验证码替换 for i in [2]: sql = 'SELECT idstring FROM `pre_captcha_mobile` WHERE mobile={0} ORDER BY captchaid DESC LIMIT 1'.format( params['phone']) code = DoMysql().do_mysql(sql) params['mobilecaptcha'] = code[0][0] else: params['access_token'] = TOKEN print('测试数据为{0}'.format(params)) self.logger.info('测试数据为{0}'.format(params)) res = HttpRequtser().http_request(data_item['url'], params, data_item['HttpMethod'], verify=False, headers=header) print('测试结果是{0}'.format(res.json())) self.logger.info('测试结果是{0}'.format(res.json())) if data_item['module'] == '登录': if res.json( )['data']['access_token']: # 任何非空数据的布尔值都为True cookies是一个类字典的格式 TOKEN = res.json()['data'][ 'access_token'] # 如果cookies不为空 就替换全局变量的COOKIES 修改全局变量 try: self.assertEqual(str(data_item['ExpectedResult']), str(res.json()['error_code'])) self.assertEqual(res.status_code, 200) print('用例通过') self.logger.info('用例通过') TestResult = 'PASS' Comment = None except AssertionError as e: print('用例失败错误是{}'.format(e)) self.logger.error('用例失败错误是{}'.format(e)) TestResult = 'Failed' Comment = res.json()['error_message'] raise e finally: DoExce(project_path.test_case_path).write_back( 'login', data_item['id'] + 1, res.json()['error_code'], TestResult, Comment) def tearDown(self): print('用例结束') self.logger.info('用例结束')
class TestApi(unittest.TestCase): '''文章,背景音的接口''' def setUp(self): self.logger = MyLog() self.logger.info('开始测试') print('开始测试') @data(*test_data) def test_api(self, data_item): # data_item=Application_profiles(data_item) print('正在进行第{0}条用例:{1}'.format(data_item['id'], data_item['description'])) self.logger.info('正在进行第{0}条用例:{1}'.format(data_item['id'], data_item['description'])) print('测试数据为{0}'.format(data_item['param'])) self.logger.info('测试数据为{0}'.format(data_item['param'])) res = HttpRequtser().http_request(data_item['url'], eval(data_item['param']), data_item['HttpMethod'], verify=False, headers=GetInfoData().get_hander()) print('测试结果是{0}'.format(res.json())) self.logger.info('测试结果是{0}'.format(res.json())) # if res.cookies: # 任何非空数据的布尔值都为True cookies是一个类字典的格式 # COOKIES = res.cookies # 如果cookies不为空 就替换全局变量的COOKIES 修改全局变量 try: self.assertEqual(str(data_item['ExpectedResult']), str(res.json()['error_code'])) self.assertEqual(res.status_code, 200) print('用例通过') self.logger.info('用例通过') TestResult = 'PASS' Comment = None except AssertionError as e: print('用例失败错误是{}'.format(e)) self.logger.error('用例失败错误是{}'.format(e)) TestResult = 'Failed' Comment = res.json()['error_message'] raise e finally: DoExce(project_path.test_case_path).write_back( 'mindfulness', data_item['id'] + 1, res.json()['error_code'], TestResult, Comment) def tearDown(self): print('用例结束') self.logger.info('用例结束')