python自动化接口测试测试用例读取_python - 接口自动化测试 - TestLogin - 登录接口测试用例...
#-*- coding:utf-8 -*-'''@project: ApiAutoTest@author: Jimmy@file: test_login.py@ide: PyCharm Community Edition@time: 2018-12-22 09:33@blog: https://www.cnblogs.com/gotesting/'''importunittestimportosf
#-*- coding:utf-8 -*-
'''@project: ApiAutoTest
@author: Jimmy
@file: test_login.py
@ide: PyCharm Community Edition
@time: 2018-12-22 09:33
@blog: https://www.cnblogs.com/gotesting/'''
importunittestimportosfrom ddt importddt,datafrom Common.http_request importHttpRequestfrom Common.read_excel importReadExcelfrom Common.read_config importReadConfigfrom Common.basic_data importDoRegex,Contextfrom Common.contants import *
from Common.get_logger importGetLogimportjson#读取配置文件,获取当前URL前缀,用于灵活更换测试服务器地址
read_config =ReadConfig()
url_pre= read_config.get_config_str('api','url_pre')#读取excel,获取login测试数据
data_dir = os.path.join(data_dir,'test_data.xlsx')
read_excel=ReadExcel(data_dir)
login_cases= read_excel.get_cases('login')
get_log=GetLog()
@ddtclassTestLogin(unittest.TestCase):defsetUp(self):print('Test Start')deftearDown(self):print('Test End')
@data(*login_cases)deftest_login(self,case):
url= url_pre +case.url#通过正则表达式 查找/替换,将excel读出来的${normal_user}及${normal_pwd}替换为配置文件中读取出的normal_user及normal_pwd的值
data =DoRegex.replace(case.data)#将替换后的字符串转换成字典
data =json.loads(data)#记录当前测试case信息
get_log.log_info('''Test Case Info:
case_id : {0}
title : {1}
method : {2}
url : {3}
data : {4}
expected: {5}'''.format(case.case_id,case.title,case.method,url,data,case.expected))
response= HttpRequest(method=case.method,url=url,data=data)
actual= response.get_json()['msg']#记录当前测试case接口响应信息
get_log.log_info('''Test Case Request Response Result:
response : {0}
actual : {1}'''.format(response.get_json(),actual))try:
self.assertEquals(case.expected,actual)
read_excel.write_result('login',case.case_id,actual,'Pass')
get_log.log_info('Test Result is Passed ! case_id is {0},title is {1}'.format(case.case_id,case.title))exceptException as e:
read_excel.write_result('login',case.case_id,actual,'Fail')
get_log.log_info('Test Result is Failed ! case_id is {0},title is {1}'.format(case.case_id,case.title))
get_log.log_error('Error msg :{0}'.format(e))raise e
更多推荐




所有评论(0)