def __init__(self, testcase_time_id, result="testcases"): self.testcase_time = TestCaseStartTimes.query.get(testcase_time_id) print('testcase_time_id: ', testcase_time_id, self.testcase_time) if result == 'testcases': testcase_results_query_sql = 'select test_case_result.testcase_name,test_case_result.testcase_url,' \ 'test_case_result.testcase_method,test_case_result.testcase_data,' \ 'test_case_result.response_body,test_case_result.' \ 'testcase_hope_result,test_case_result.' \ 'old_sql_value,test_case_result.new_sql_value,test_case_result.' \ 'testcase_test_result,test_case_result.old_sql_value_result,test_case_result.new_sql_value_result' \ ' from testcases,test_case_result where testcases.id=' \ 'test_case_result.testcase_id and testcases.testcase_scene_id isnull and ' \ 'test_case_result.testcase_start_time_id=%s' \ % testcase_time_id self.testcase_results = cdb().query_db(testcase_results_query_sql) print('self.testcase_results:', self.testcase_results) elif result == 'scene_testcases': testcase_results_query_sql = 'select test_case_result.testcase_name,test_case_result.testcase_url,' \ 'test_case_result.testcase_method,test_case_result.testcase_data,' \ 'test_case_result.response_body,test_case_result.testcase_hope_result,' \ 'test_case_result.old_sql_value,test_case_result.new_sql_value,' \ 'test_case_result.testcase_test_result,test_case_result.old_sql_value_result,' \ 'test_case_result.new_sql_value_result,testcases.testcase_scene_id' \ ' from testcases, test_case_result ' \ 'where testcases.id=test_case_result.testcase_id and ' \ 'testcases.testcase_scene_id not null and ' \ 'test_case_result.testcase_start_time_id=%s' \ % testcase_time_id self.testcase_results = cdb().query_db(testcase_results_query_sql) print('self.testcase_results:', self.testcase_results)
def post(self, id=-1): name, description = request_get_values('name', 'description') case_group_update_sql = 'update case_group set name=?,description=? where id=?' cdb().opeat_db(case_group_update_sql, (name, description, id)) FrontLogs('编辑测试用例分组 name:%s 成功' % name).add_to_front_log() # app.logger.info('message:update case_group success, name: %s' % name) return redirect(url_for('case_group_blueprint.case_group_list'))
def post(self, id=-1): name, value, description = request_get_values('name', 'value', 'description') variable_update_sql = 'update variables set name=?,value=?,description=? where id=?' cdb().opeat_db(variable_update_sql, (name, value, description, id)) # app.logger.info('message:update variables success, name: %s' % name) FrontLogs('编辑全局变量 name: %s 成功' % name).add_to_front_log() return redirect(url_for('variables_blueprint.variable_list'))
def get(self, id=-1): delete_request_headers_sql = 'delete from request_headers where id=?' cdb().opeat_db(delete_request_headers_sql, (id, )) FrontLogs('删除请求头部 id: %s 成功' % id).add_to_front_log() # app.logger.info('message:delete request_headers success, id: %s' % id) return redirect( url_for('request_headers_blueprint.request_headers_list'))
def get(self, id=-1): page, scene_page = request_get_values('page', 'scene_page') testcase_scene_id = request.args.get('testcase_scene_id', None) delete_test_case_sql = 'delete from testcases where id=?' cdb().opeat_db(delete_test_case_sql, (id, )) FrontLogs('删除测试用例 id: %s 成功' % id).add_to_front_log() # app.logger.info('message:delete testcases success, id: %s' % id) if testcase_scene_id not in (None, "None"): return redirect( url_for( 'testcase_scene_blueprint.testcase_scene_testcase_list', page=scene_page)) return redirect(url_for('testcase_blueprint.test_case_list', page=page))
def analysis_params(self, params, is_change=None): if params in ("", None): params = "" return params while 1: print('analysis_before:', type(params), params) res = r'\${([^\${}]+)}' words = re.findall(re.compile(res), params) print('需要解析的变量:%s, 筛选出的变量: %s' % (params, words)) if len(words) == 0: return params in_variables_num = 0 for word in words: if '随机' in word: params = RangName(params).rand_str() if (word,) in self.variables: in_variables_num += 1 variable_value_query_sql = 'select value from variables where name=?' variable_value = cdb().query_db(variable_value_query_sql, (word,), True)[0] print('variable_value: ${%s}' % word, variable_value) if is_change == "headers": params = params.replace('${%s}' % word, '"%s"' % variable_value) params = params.replace('${%s}' % word, variable_value) print('解析后的参数为:', params, type(params)) if in_variables_num == 0: return params
def post(self, id=-1): page, scene_page, name, url, method, data, group_id, request_headers_id, regist_variable, regular \ , hope_result, testcase_scene_id, old_sql, new_sql, old_sql_regist_variable, new_sql_regist_variable, \ old_sql_hope_result, new_sql_hope_result, old_mysql_id, new_mysql_id = request_get_values( 'page', 'scene_page', 'name', 'url', 'method', 'data', 'case_group', 'request_headers', 'regist_variable', 'regular', 'hope_result', 'testcase_scene_id', 'old_sql', 'new_sql', 'old_sql_regist_variable', 'new_sql_regist_variable', 'old_sql_hope_result', 'new_sql_hope_result', 'old_mysql', 'new_mysql') print('UpdateTestCase post:testcase_scene_id ', testcase_scene_id, scene_page) id = request.args.get('id', id) user_id = session.get('user_id') update_regist_variable(id, old_sql_regist_variable, new_sql_regist_variable, user_id) update_test_case_sql = 'update testcases set name=?,url=?,data=?,method=?,group_id=?,' \ 'request_headers_id=?,regist_variable=?,regular=?,hope_result=?,' \ 'old_sql=?,new_sql=?,old_sql_regist_variable=?,new_sql_regist_variable=?,' \ 'old_sql_hope_result=?, new_sql_hope_result=?, old_sql_id=?, new_sql_id=? where id=?' cdb().opeat_db(update_test_case_sql, (name, url, data, method, group_id, request_headers_id, regist_variable, regular, hope_result, old_sql, new_sql, old_sql_regist_variable, new_sql_regist_variable, old_sql_hope_result, new_sql_hope_result, old_mysql_id, new_mysql_id, id)) FrontLogs('编辑测试用例 name: %s 成功' % name).add_to_front_log() # app.logger.info('message:update testcases success, name: %s' % name) print('UpdateTestCase post:testcase_scene_id return :', testcase_scene_id, len(testcase_scene_id)) if testcase_scene_id not in (None, "None"): print('UpdateTestCase post:testcase_scene_id return :', testcase_scene_id is True, len(testcase_scene_id)) return redirect( url_for( 'testcase_scene_blueprint.testcase_scene_testcase_list', page=scene_page)) return redirect(url_for('testcase_blueprint.test_case_list', page=page))
def post(self): username, password = request_get_values('username', 'password') user_query_sql = 'select password from users where username=?' pwd = cdb().query_db(user_query_sql, (username, ), True) if pwd and password == pwd[0]: user = User.query.filter(User.username == username).first() resp = make_response( redirect(url_for('testcase_blueprint.test_case_list'))) resp.set_cookie('SESSIONID', '{};{}'.format(username, password), path='/', expires=datetime.now() + timedelta(days=7)) session['username'] = username session['user_id'] = user.id return resp flash('账号或密码错误') return render_template('login/login.html')
def get(self): user_id = session.get('user_id') request_headers_search = request_get_values('request_headers_search') request_headers = RequestHeaders.query.all() print('request_headers:', request_headers) if request.is_xhr: request_headers_query_sql = 'select id,name from request_headers where user_id=?' request_headerses = cdb().query_db(request_headers_query_sql, (user_id, )) request_headers_dict = {} for index, request_headers in enumerate(request_headerses): print('request_header:', request_headers) request_headers_dict.update({ "index": index, "id%s" % index: request_headers[0], "name%s" % index: request_headers[1] }) print("request_headers_dict: ", request_headers_dict) print('request_headers_list_ajax : True') return json.dumps( {"request_headers_dict": str(request_headers_dict)}) # 需要转行成字符串再转成json page = request.args.get('page', 1, type=int) FrontLogs('进入请求头部列表 第%s页' % page).add_to_front_log() # pagination是salalchemy的方法,第一个参数:当前页数,per_pages:显示多少条内容 error_out:True 请求页数超出范围返回404错误 False:反之返回一个空列表 pagination = RequestHeaders.query.filter( RequestHeaders.name.like("%" + request_headers_search + "%") if request_headers_search is not None else "", RequestHeaders.user_id == user_id).order_by( RequestHeaders.timestamp.desc()).paginate( page, per_page=current_app.config['FLASK_POST_PRE_ARGV'], error_out=False) # 返回一个内容对象 request_headerses = pagination.items print("request_headers_pagination: ", pagination) return render_template('request_headers/request_headers_list.html', pagination=pagination, items=request_headerses)
def get(self): user_id = session.get('user_id') user = User.query.get(user_id) mysqls = Mysql.query.all() for mysql in mysqls: mysql.ip, mysql.port, mysql.name, mysql.user, mysql.password = AnalysisParams( ).analysis_more_params(mysql.ip, mysql.port, mysql.name, mysql.user, mysql.password) page, scene_page = request_get_values('page', 'scene_page') case_groups = user.user_case_groups testcase_scene_id = request.args.get('testcase_scene_id', None) request_headers_querys_sql = 'select id,name from request_headers where user_id=?' request_headers = cdb().query_db(request_headers_querys_sql, (user_id, )) print('request_headers: ', request_headers) FrontLogs('进入添加测试用例页面').add_to_front_log() return render_template('test_case/test_case_add.html', case_groups=case_groups, request_headers=request_headers, testcase_scene_id=testcase_scene_id, scene_page=scene_page, page=page, mysqls=mysqls)
def get(self): user_id = session.get('user_id') case_group_search = request_get_values('case_group_search') user = User.query.get(user_id) case_groups = user.user_case_groups print('case_groups: ', case_groups) if request.is_xhr: case_groups_query_sql = 'select id,name from case_group where user_id=?' case_groups = cdb().query_db(case_groups_query_sql, (user_id, )) case_groups_dict = {} for index, case_group in enumerate(case_groups): print('case_group:', case_group) case_groups_dict.update({ "index": index, "id%s" % index: case_group[0], "name%s" % index: case_group[1] }) print("case_groups_dict: ", case_groups_dict) print('case_group_list_ajax : True') return json.dumps({"case_groups_dict": str(case_groups_dict)}) # 需要转行成字符串再转成json else: page = request.args.get('page', 1, type=int) FrontLogs('进入测试用例分组列表页面 第%s页' % page).add_to_front_log() # pagination是salalchemy的方法,第一个参数:当前页数,per_pages:显示多少条内容 error_out:True 请求页数超出范围返回404错误 False:反之返回一个空列表 pagination = CaseGroup.query.filter(CaseGroup.name.like( "%"+case_group_search+"%") if case_group_search is not None else "", CaseGroup.user_id == user_id).\ order_by(CaseGroup.timestamp.desc()).paginate( page, per_page=current_app.config['FLASK_POST_PRE_ARGV'], error_out=False) # 返回一个内容对象 case_groups = pagination.items print("pagination: ", pagination) return render_template('case_group/case_group_list.html', pagination=pagination, items=case_groups)
def get(self, id=-1): delete_case_group_sql = 'delete from case_group where id=?' cdb().opeat_db(delete_case_group_sql, (id, )) FrontLogs('删除测试用例分组 id:%s 成功' % id).add_to_front_log() # app.logger.info('message:delete case_group success, id: %s' % id) return redirect(url_for('case_group_blueprint.case_group_list'))
def post(self): user_id = session.get('user_id') print('要添加的测试用例:', request.form) page, scene_page, name, url, method, regist_variable, regular, request_headers_id, old_sql, new_sql, \ old_sql_regist_variable, new_sql_regist_variable, old_sql_hope_result, new_sql_hope_result, old_mysql_id, \ new_mysql_id = \ request_get_values('page', 'scene_page', 'name', 'url', 'method', 'regist_variable', 'regular', 'request_headers', 'old_sql', 'new_sql', 'old_sql_regist_variable', 'new_sql_regist_variable', 'old_sql_hope_result', 'new_sql_hope_result', 'old_mysql', 'new_mysql') group_id = request.form.get('case_group', None) data = request.form.get('data', '').replace('/n', '').replace(' ', '') request_headers_query_sql = 'select value from request_headers where id=?' request_headers = cdb().query_db(request_headers_query_sql, (request_headers_id, ), True)[0] print('TestCaseAdd request_headers before: ', request_headers, method) request_headers = AnalysisParams().analysis_params(request_headers, is_change="headers") print('TestCaseAdd request_headers: ', request_headers) testcase_scene_id = request.args.get('testcase_scene_id', None) if testcase_scene_id == "None": testcase_scene_id = None print('testcase_scene_id的值:', testcase_scene_id, type(testcase_scene_id)) headers = json.loads(request_headers) print('request_headers_id: %s headers:%s ' % (request_headers_id, headers)) hope_result = request.form.get('hope_result') if request.form.get('test', 0) == '测试': data = RangName(data).rand_str() url = AnalysisParams().analysis_params(url) result = MethodRequest().request_value( method, url, data, headers).replace('<', '').replace('>', '') return '''%s''' % result print('testcase_scene_id的值:', testcase_scene_id, type(testcase_scene_id)) testcase = TestCases(name, url, data, regist_variable, regular, method, group_id, request_headers_id, hope_result=hope_result, testcase_scene_id=testcase_scene_id, user_id=user_id, old_sql=old_sql, new_sql=new_sql, old_sql_regist_variable=old_sql_regist_variable, new_sql_regist_variable=new_sql_regist_variable, old_sql_hope_result=old_sql_hope_result, new_sql_hope_result=new_sql_hope_result, old_sql_id=old_mysql_id, new_sql_id=new_mysql_id) add_regist_variable(old_sql_regist_variable, new_sql_regist_variable, user_id) db.session.add(testcase) db.session.commit() FrontLogs('添加测试用例 name: %s 成功' % name).add_to_front_log() # app.logger.info('message:insert into testcases success, name: %s' % name) if testcase_scene_id not in (None, "None"): return redirect( url_for( 'testcase_scene_blueprint.testcase_scene_testcase_list', page=scene_page)) return redirect(url_for('testcase_blueprint.test_case_list', page=page))
def test_report(testcase_time_id, allocation, testcase_scene_list): data = [] testcase_scene_count_dict = {} for testcase_scene in testcase_scene_list: testcase_scene_count_dict.update({'testcase_scene_' + str(testcase_scene.name): testcase_scene}) testcase_time = TestCaseStartTimes.query.get(testcase_time_id) testcase_scene_counts_sql = 'select testcase_scenes.name, count(*),testcase_scenes.id from test_case_start_times,test_case_result,testcases,testcase_scenes where test_case_start_times.id=' \ 'test_case_result.testcase_start_time_id and testcases.id=test_case_result.testcase_id and testcases.testcase_scene_id=testcase_scenes.id ' \ 'and test_case_start_times.id=%s and testcases.testcase_scene_id is not null group by testcases.testcase_scene_id' % testcase_time_id testcase_scene_counts = cdb().query_db(testcase_scene_counts_sql) print('testcase_scene_counts count:', testcase_scene_counts) for testcase_scene_count in testcase_scene_counts: testcase_scene_count_dict.update({testcase_scene_count[0]: [testcase_scene_count[1], testcase_scene_count[2]]}) testcase_results_query_sql = 'select testcases.name,testcases.url,testcases.method,testcases.data,test_case_result.response_body,' \ ' testcases.hope_result,test_case_result.old_sql_value,test_case_result.new_sql_value,' \ 'test_case_result.testcase_test_result,test_case_result.old_sql_value_result,test_case_result.new_sql_value_result,' \ 'testcases.testcase_scene_id,testcases.old_sql_hope_result,testcases.new_sql_hope_result from testcases,test_case_result where testcases.id=test_case_result.testcase_id ' \ 'and test_case_result.testcase_start_time_id=%s' % testcase_time_id testcase_results = cdb().query_db(testcase_results_query_sql) for testcase_result in testcase_results: if testcase_result[11]: testcase_scene_name = TestCaseScene.query.get(testcase_result[11]).name else: testcase_scene_name = '' t_name = AnalysisParams().analysis_params(testcase_result[0]) t_url = AnalysisParams().analysis_params(testcase_result[1]) t_method = testcase_result[2] t_request_body = AnalysisParams().analysis_params(testcase_result[3]) t_response_body = testcase_result[4] t_hope = AnalysisParams().analysis_params(testcase_result[5]) old_database_value = testcase_result[6] new_database_value = testcase_result[7] if testcase_result[8] == "测试失败" or testcase_result[9] == "测试失败" or testcase_result[10] == "测试失败": t_result = "测试失败" else: t_result = "测试成功" # print('testcase_result: ', testcase_result) content = {"t_name": t_name, "t_url": t_url, "t_method": t_method, "t_request_body": t_request_body, "t_hope": t_hope, "t_response_body": t_response_body, "old_database_value": old_database_value, "new_database_value": new_database_value, "t_result": testcase_result[8], "t_old_sql_value_result": testcase_result[9], "t_new_sql_value_result": testcase_result[10], "t_testcase_scene": testcase_scene_name, "t_testcase_result": t_result, "t_old_sql_hope": AnalysisParams().analysis_params(testcase_result[12]), "t_new_sql_hope": AnalysisParams().analysis_params(testcase_result[13]) } data.append(content) data = data[::-1] filename = testcase_time.filename data_title = {"test_name": allocation.test_name, "test_version": allocation.zdbm_version, "test_pl": allocation.test_pl, "test_net": allocation.test_net} data_re = {"test_sum": allocation.test_sum, "test_success": allocation.test_success, "test_failed": allocation.fail_sum, "test_date": allocation.time_strftime} r = Report() # print("data_re", data_title, data_re, allocation.time_strftime, filename) r.init(data_title, data_re, int(allocation.test_success * 100 / allocation.test_sum), title_name=allocation.title_name, filename=filename) r.test_detail(data, len(data), len(data), testcase_scene_count_dict, testcase_time_id)
def get(self, id=-1): delete_variables_sql = 'delete from variables where id=?' cdb().opeat_db(delete_variables_sql, (id, )) FrontLogs('删除全局变量 id: %s 成功' % id).add_to_front_log() # app.logger.info('message:delete variables success, id: %s' % id) return redirect(url_for('variables_blueprint.variable_list'))
def __init__(self): variables_query_sql = 'select name from variables' self.variables = cdb().query_db(variables_query_sql) print('init:self.variables:', self.variables)