def get_usrid(self): array_usrid = [] try: access_token = AscToken().get_acstoken() url_usrid = excuteCFG.ConfigRead(cfgfile, 'userid', 'URL').CfgRead( ) + 'root/userinfos?access_token=' + access_token except BaseException, e: log.getErrorInfo('无法利用微服务token获取用户ID')
def get_noacstoken(self): '''随机伪造access_token''' try: log.getInfo('获取伪造微服务token') return ''.join(random.sample(string.ascii_letters+string.digits,8))+'-'+''.join(random.sample(string.ascii_letters+string.digits,4))\ +'-'+''.join(random.sample(string.digits,4))+'-'+''.join(random.sample(string.ascii_letters+string.digits,4))+'-'+''.join(random.sample(string.ascii_letters+string.digits,12)) except BaseException, e: log.getErrorInfo('无法获取伪造微服务token')
def get_departmentid(self): array_departmentid = [] access_token = AscToken().get_acstoken() data = {'sublevel': sublevel, 'access_token': access_token} try: response = ApiRequest.ApiTest('GET', url_departmentid, data) except BaseException, e: log.getErrorInfo('无法获取DepartmentID')
def get_notoken(self): '''伪造用户token''' try: log.getInfo('获取伪造用户token') return 'TGT' + '-' + str(random.randint(1, 10000)) + '-' + ''.join( random.sample(string.ascii_letters + string.digits, 50)) + '-' + 'cas' except BaseException, e: log.getErrorInfo('无法获取伪造用户token')
def input_content(path, index, x, y, str, styl=Style.default_style): try: workbook = xlrd.open_workbook(path, on_demand=True) workbook_new = copy(workbook) ws = workbook_new.get_sheet(index) ws.write(x, y, str, styl) workbook_new.save(path) except Exception, e: log.getErrorInfo(e)
def get_nodepartmentid(self): try: log.getInfo('获取伪造DepartmentID') return ''.join(random.sample(string.ascii_letters + string.digits, 8)) + '-' + ''.join( random.sample(string.ascii_letters + string.digits, 4)) \ + '-' + ''.join(random.sample(string.digits, 4)) + '-' + ''.join( random.sample(string.ascii_letters + string.digits, 4)) + '-' + ''.join( random.sample(string.ascii_letters + string.digits, 13)) except BaseException, e: log.getErrorInfo('无法获取伪造DepartmentID')
def CfgRead(self,cfgfile,item,section): try: self.cfgfile = cfgfile self.item = item self.section = section config = ConfigParser.ConfigParser() config.read(cfgfile) return config.get(item,section) except Exception,e: log.getErrorInfo("类型问题 %s", e)
def get_acstoken(self): data = { 'client_id': client_id, 'client_secret': client_secret, 'grant_type': grant_type } log.getInfo('获取微服务token参数:' + str(data)) try: response = ApiRequest.ApiTest('POST', url_access_token, data) except BaseException, e: log.getErrorInfo('无法获取微服务token')
def CfgWrite(self,cfgfile,section,item,value): try: self.cfgfile = cfgfilea self.item = item self.section = section self.value = value config = ConfigParser.ConfigParser() config.read(cfgfile) config.set(section,item,value) config.write(open(cfgfile,'w')) except Exception, e: log.getErrorInfo("类型问题 %s", e)
def get_token(self): data = { 'username': username, 'password': password, 'loginMode': loginmode, 'service': service } header = {'Content-Type': content_type, 'client_ip': client_ip} try: response = ApiRequest.ApiTest('POST', url_token, data) except BaseException, e: log.getErrorInfo('无法获取用户token')
def apicall(self, method, url, data): result = '' if method == 'GET': if data != '': try: result = requests.get(url, data, timeout=10) log.getDebug(result.status_code) log.getDebug(result.content) log.getInfo(result.status_code) log.getInfo(result.content) return result.status_code, result.content except requests.ConnectTimeout, e: log.getErrorInfo("错误信息:" + str(e)) except requests.RequestException, e: log.getErrorInfo("错误信息:" + str(e))
def test_circle(self): log.getInfo('开始执行测试') for i in range(1, row): try: URL = str(excel.get_content(XLS, index, i, url))[6:].replace("'", "") METHOD = str( str(excel.get_content(XLS, index, i, method))[6:].replace("'", "")) DATA = str(excel.get_content(XLS, index, i, data))[7:-1] '''加入标签,便于定位传参''' TAG = str(excel.get_content(XLS, index, i, tag)) except IOError, e: log.getErrorInfo('NO SUCH DIRECTORY' + e) except BaseException, e: log.getErrorInfo(e) '''获取组织结构列表get_orglist'''
class APIGetAdList(unittest.TestCase): def setUp(self): log.getInfo('初始化测试数据') def test_circle(self): log.getInfo('开始执行测试') for i in range(1, row): try: URL = str(excel.get_content(XLS, index, i, url))[6:].replace("'", "") METHOD = str( str(excel.get_content(XLS, index, i, method))[6:].replace("'", "")) DATA = str(excel.get_content(XLS, index, i, data))[7:-1] '''加入标签,便于定位传参''' TAG = str(excel.get_content(XLS, index, i, tag)) except IOError, e: log.getErrorInfo('NO SUCH DIRECTORY' + e) except BaseException, e: log.getErrorInfo(e) '''获取组织结构列表get_orglist''' if 'get_orglist' in TAG: URL_RE = URL.replace('{access_token}', BaseParam.AscToken().get_acstoken()) try: response = ApiRequest.ApiTest(METHOD, URL_RE, eval(DATA)) except BaseException, e: log.getErrorInfo(e) try: style = xlwt.easyxf( 'font:height 240, color-index red, bold on;align: wrap on, vert centre, horiz center' ) '''写入状态返回码''' excel.input_content(XLS, index, i, status_code, response.apicall()[0], style) '''补充增加实际返回结果,解决字符编码问题''' excel.input_content(XLS, index, i, result, response.apicall()[1].decode('utf-8'), style) except IOError, e: log.getErrorInfo('NO SUCH DIRECTORY')
class UserID(object): def __init__(self): pass def get_usrid(self): array_usrid = [] try: access_token = AscToken().get_acstoken() url_usrid = excuteCFG.ConfigRead(cfgfile, 'userid', 'URL').CfgRead( ) + 'root/userinfos?access_token=' + access_token except BaseException, e: log.getErrorInfo('无法利用微服务token获取用户ID') except IOError, e: log.getErrorInfo('无法获取配置文件信息')
# !/usr/bin/python # -*- coding: UTF-8 -*- import sys sys.path.append('./venv/lib/python2.7/site-packages') from HTMLTestRunner import HTMLTestRunner import unittest import os import time from src import info as log try: test_dir = os.path.abspath('.') reportfile = os.path.abspath( '.' ) + '/report' + '/%s_result.html' % time.strftime('%Y-%m-%d-%H-%M-%S') discover = unittest.defaultTestLoader.discover(test_dir, pattern='WebApi.py') except IOError, e: log.getErrorInfo("错误信息:" + str(e)) if __name__ == "__main__": filename = reportfile fp = open(filename, 'wb') runner = HTMLTestRunner(stream=fp) runner.run(discover) fp.close()
#读取配置文件信息 cfgfile = os.path.abspath('.') + '/config.ini' #配置文件位置 try: XLS = excuteCFG.ConfigRead(cfgfile, 'setting', 'XLS').CfgRead() url = int(excuteCFG.ConfigRead(cfgfile, 'setting', 'URL').CfgRead()) method = int(excuteCFG.ConfigRead(cfgfile, 'setting', 'METHOD').CfgRead()) data = int(excuteCFG.ConfigRead(cfgfile, 'setting', 'DATA').CfgRead()) index = int(excuteCFG.ConfigRead(cfgfile, 'setting', 'INDEX').CfgRead()) status_code = int( excuteCFG.ConfigRead(cfgfile, 'setting', 'STATUS_CODE').CfgRead()) result = int(excuteCFG.ConfigRead(cfgfile, 'setting', 'RESULT').CfgRead()) tag = int(excuteCFG.ConfigRead(cfgfile, 'setting', 'TAG').CfgRead()) row = excel.get_rows(XLS, 0) #表行数 except IOError, e: log.getErrorInfo(e) class APIGetAdList(unittest.TestCase): def setUp(self): log.getInfo('初始化测试数据') def test_circle(self): log.getInfo('开始执行测试') for i in range(1, row): try: URL = str(excel.get_content(XLS, index, i, url))[6:].replace("'", "") METHOD = str( str(excel.get_content(XLS, index, i, method))[6:].replace("'", ""))
cfgfile = './config.ini' file_path = os.path.abspath('.') result_file = os.listdir('./data') now = time.strftime("%Y-%m-%d-%H_%M_%S", time.localtime(time.time())) shutil.copyfile('./data/' + result_file[0], './data/' + now + '.xls') #shutil.copyfile('./data/'+result_file[0],'./data/example.xls') try: config = ConfigParser.ConfigParser() config.read(cfgfile) config.set('setting', 'xls_back', now + '.xls') config.write(open(cfgfile, 'w')) except IOError: log.getErrorInfo('写入配置文件异常') try: xls_back = excuteCFG.ConfigRead(cfgfile, 'setting', 'xls_back').CfgRead() except IOError: log.getErrorInfo('读取配置文件异常') ''' try: if os.listdir('./data'): shutil.move('./data/'+xls_back, file_path+'/TestResult/'+xls_back) except BaseException: log.getErrorInfo('测试结果拷贝异常') ''' try: config = ConfigParser.ConfigParser() config.read(cfgfile) config.set('setting', 'xls', './data/' + now + '.xls')
def get_cols(path, index): try: workbook = xlrd.open_workbook(path, on_demand=True) return workbook.sheet_by_index(index).ncols except Exception, e: log.getErrorInfo(e)
sublevel = excuteCFG.ConfigRead(cfgfile, 'departmentid', 'SUBLEVEL').CfgRead() url_departmentid = excuteCFG.ConfigRead(cfgfile, 'departmentid', 'URL').CfgRead() '''获取用户token''' url_token = excuteCFG.ConfigRead(cfgfile, 'token', 'URL').CfgRead() username = excuteCFG.ConfigRead(cfgfile, 'token', 'USERNAME').CfgRead() password = excuteCFG.ConfigRead(cfgfile, 'token', 'PASSWORD').CfgRead() loginmode = excuteCFG.ConfigRead(cfgfile, 'token', 'LOGINMODE').CfgRead() service = excuteCFG.ConfigRead(cfgfile, 'token', 'SERVICE').CfgRead() client_ip = excuteCFG.ConfigRead(cfgfile, 'token', 'CLIENT_IP').CfgRead() content_type = excuteCFG.ConfigRead(cfgfile, 'token', 'CONTENT_TYPE').CfgRead() except IOError, e: log.getErrorInfo('无法获取配置文件信息') class AscToken(object): def __init__(self): pass def get_acstoken(self): data = { 'client_id': client_id, 'client_secret': client_secret, 'grant_type': grant_type } log.getInfo('获取微服务token参数:' + str(data)) try: response = ApiRequest.ApiTest('POST', url_access_token, data)
def get_row_content(path, index, x): try: workbook = xlrd.open_workbook(path, on_demand=True) return workbook.sheet_by_index(index).row_values(x) except Exception, e: log.getErrorInfo(e)
def get_content(path, index, x, y): try: workbook = xlrd.open_workbook(path, on_demand=True) return workbook.sheet_by_index(index).cell(x, y) except Exception, e: log.getErrorInfo(e)