示例#1
0
    def get(self, id=-1):
        testcase_id = request.args.get('id', id)
        user_id = session.get('user_id')
        print('testcase_id:', testcase_id)
        testcase = TestCases.query.get(testcase_id)
        if testcase.old_sql_id:
            old_mysql = Mysql.query.get(testcase.old_sql_id)
            old_mysql.name = AnalysisParams().analysis_params(old_mysql.name)
        else:
            old_mysql = ''
        if testcase.new_sql_id:
            new_mysql = Mysql.query.get(testcase.new_sql_id)
            new_mysql.name = AnalysisParams().analysis_params(new_mysql.name)
        else:
            new_mysql = ''
        case_groups = CaseGroup.query.all()
        case_group_id_before = testcase.group_id
        request_headers_id_before = testcase.request_headers_id
        request_headerses = RequestHeaders.query.all()
        FrontLogs('查看测试用例 name: %s ' % testcase.name).add_to_front_log()

        return render_template(
            'test_case/test_case_look.html',
            item=testcase,
            case_groups=case_groups,
            request_headers_id_before=request_headers_id_before,
            case_group_id_before=case_group_id_before,
            request_headerses=request_headerses,
            old_mysql=old_mysql,
            new_mysql=new_mysql)
示例#2
0
def post_testcase(test_case_id, testcase_time_id):
    testcase = TestCases.query.get(test_case_id)
    url, data, hope_result = AnalysisParams().analysis_more_params(
        testcase.url, testcase.data, testcase.hope_result)
    method = testcase.method
    response_body, regist_variable_value = to_execute_testcase(testcase)
    testcase_test_result = AssertMethod(
        actual_result=response_body, hope_result=hope_result).assert_method()
    if testcase.old_sql and testcase.old_sql_id and testcase.old_sql_regist_variable:
        old_sql_value = mysqlrun(
            mysql_id=testcase.old_sql_id,
            sql=testcase.old_sql,
            regist_variable=testcase.old_sql_regist_variable,
            is_request=False)
        old_sql_value_result = AssertMethod(
            actual_result=old_sql_value,
            hope_result=AnalysisParams().analysis_params(
                testcase.old_sql_hope_result)).assert_method()
    else:
        old_sql_value = old_sql_value_result = ''

    if testcase.new_sql and testcase.new_sql_id and testcase.new_sql_regist_variable:
        new_sql_value = mysqlrun(
            mysql_id=testcase.new_sql_id,
            sql=testcase.new_sql,
            regist_variable=testcase.new_sql_regist_variable,
            is_request=False)
        new_sql_value_result = AssertMethod(
            actual_result=new_sql_value,
            hope_result=AnalysisParams().analysis_params(
                testcase.new_sql_hope_result)).assert_method()

    else:
        new_sql_value = new_sql_value_result = ''
    # 调用比较的方法判断响应报文是否满足期望

    print('testcase_test_result:', testcase_test_result)
    if testcase_test_result == "测试失败" or old_sql_value_result == "测试失败" or new_sql_value_result == "测试失败":
        test_result = "测试失败"
    else:
        test_result = "测试成功"
    testcase_result = TestCaseResult(test_case_id,
                                     testcase.name,
                                     url,
                                     data,
                                     method,
                                     hope_result,
                                     testcase_time_id,
                                     response_body,
                                     testcase_test_result,
                                     old_sql_value=old_sql_value,
                                     new_sql_value=new_sql_value,
                                     old_sql_value_result=old_sql_value_result,
                                     new_sql_value_result=new_sql_value_result,
                                     result=test_result,
                                     scene_id=testcase.testcase_scene_id)
    # 测试结果实例化
    db.session.add(testcase_result)
    db.session.commit()
    return response_body
示例#3
0
def to_execute_testcase(testcase):

    testcase_request_header = testcase.testcase_request_header  # 通过反向引用获得case对应的请求头对象
    testcase_name, testcase_url, testcase_data = AnalysisParams().analysis_more_params(testcase.name, testcase.url, testcase.data)

    testcase_request_header_value = AnalysisParams().analysis_params(
        testcase_request_header.value, is_change='headers')
    print('请求的url:%s 请求的headers:%s' % (testcase_url, testcase_request_header_value))
    testcase_result, regist_variable_value = to_regist_variables(testcase_name, testcase.method, testcase_url, testcase_data,
                                          json.loads(testcase_request_header_value),
                                          testcase.regist_variable, testcase.regular)
    return testcase_result, regist_variable_value
示例#4
0
def mysqlrun(mysql_id=None, sql='', regist_variable='', is_request=True):
    print('MysqlRun:', sql, regist_variable)
    mysql = Mysql.query.get(mysql_id)
    host, port, db_name, user, password = AnalysisParams(
    ).analysis_more_params(mysql.ip, mysql.port, mysql.db_name, mysql.user,
                           mysql.password)
    try:
        result = ConnMysql(host, int(port), user, password, db_name,
                           sql).select_mysql()
        if regist_variable:
            if Variables.query.filter(
                    Variables.name == regist_variable).count() > 0:
                Variables.query.filter(Variables.name == regist_variable
                                       ).first().value = str(result)
            else:
                variable = Variables(regist_variable,
                                     str(result),
                                     is_private=1,
                                     user_id=session.get('user_id'))
                db.session.add(variable)
            db.session.commit()
            if is_request:
                result = '【查询结果】<br>' + str(
                    result
                ) + '<br>【注册变量名】 【' + regist_variable + '】<br>' + str(result)
            else:
                return result
        else:
            result = '【查询结果】<br>' + str(result) + '<br>【未注册变量】'
        return json.dumps(result)
    except Exception as e:
        print(e)
        return json.dumps(str(e))
示例#5
0
 def get(self, id=-1):
     user_id = session.get('user_id')
     user = User.query.get(user_id)
     page = request_get_values('page')
     mysqls = Mysql.query.filter(Mysql.user_id == user_id).all()
     for mysql in mysqls:
         mysql.name = AnalysisParams().analysis_params(mysql.name)
     testcase_scene_id = request.args.get('testcase_scene_id', None)
     scene_page = request.args.get('scene_page')
     print('UpdateTestCase get:testcase_scene_id ', testcase_scene_id)
     testcase = TestCases.query.filter(TestCases.id == id).first()
     print('testcase.group_id:', testcase.group_id)
     # 获取测试用例分组的列表
     case_groups = user.user_case_groups
     case_group_id_before = testcase.group_id
     request_headers_id_before = testcase.request_headers_id
     request_headerses = user.user_request_headers
     print('testcase:', testcase)
     print('case_groups :', case_groups)
     print('request_headerses:', request_headerses)
     FrontLogs('进入编辑测试用例 id: %s 页面' % id).add_to_front_log()
     return render_template(
         'test_case/test_case_search.html',
         item=testcase,
         case_groups=case_groups,
         request_headers_id_before=request_headers_id_before,
         case_group_id_before=case_group_id_before,
         request_headerses=request_headerses,
         testcase_scene_id=testcase_scene_id,
         scene_page=scene_page,
         page=page,
         mysqls=mysqls)
示例#6
0
 def __init__(self, host, port, user, password, db_name, sql):
     print('ConnMysql:', host, port, user, password, db_name, sql)
     self.db = pymysql.connect(host=host,
                               port=port,
                               user=user,
                               passwd=password,
                               db=db_name,
                               charset='utf8')
     if sql:
         sql = AnalysisParams().analysis_params(sql)
     self.sql = sql
示例#7
0
 def get(self):
     user_id = session.get('user_id')
     testcase_search = request_get_values('testcase_search')
     user = User.query.get(user_id)
     model_testcases = TestCases.query.filter(
         TestCases.is_model == 1, TestCases.user_id == user_id).all()
     # 过滤有测试用例分组的查询结果
     testcases = TestCases.query.filter(
         TestCases.testcase_scene_id.is_(None),
         TestCases.user_id == user_id).all()
     # 获取测试用例分组的列表
     print('testcases: ', testcases)
     for testcase in testcases:
         testcase.name = AnalysisParams().analysis_params(testcase.name)
         testcase.url = AnalysisParams().analysis_params(testcase.url)
         testcase.data = AnalysisParams().analysis_params(testcase.data)
     case_groups = user.user_case_groups
     print('case_groups: ', case_groups)
     request_headers = user.user_request_headers
     print('request_headers: ', request_headers)
     page = request.args.get('page', 1, type=int)
     #  pagination是salalchemy的方法,第一个参数:当前页数,per_pages:显示多少条内容 error_out:True 请求页数超出范围返回404错误 False:反之返回一个空列表
     pagination = TestCases.query.filter(TestCases.name.like(
         "%" + testcase_search + "%") if testcase_search is not None else "",
                                         TestCases.testcase_scene_id.is_(None),
                                         TestCases.user_id == user_id).order_by \
         (TestCases.timestamp.desc()).paginate(
         page, per_page=current_app.config['FLASK_POST_PRE_ARGV'], error_out=False)
     # 返回一个内容对象
     testcaseses = pagination.items
     print("pagination: ", pagination)
     FrontLogs('进入测试用例列表页面 第%s页' % page).add_to_front_log()
     return render_template('test_case/test_case_list.html',
                            pagination=pagination,
                            items=testcaseses,
                            case_groups=case_groups,
                            request_headers=request_headers,
                            page=page,
                            model_testcases=model_testcases)
示例#8
0
    def __init__(self, testcase_result):
        print('Test :', testcase_result)

        self.t_name, self.url, self.request_body, self.hope = AnalysisParams().\
            analysis_more_params(testcase_result[0], testcase_result[1], testcase_result[3], testcase_result[5])
        print('self.t_name: ', self.t_name)

        self.method, self.response_body, self.old_database_value, self.new_database_value, self.result, \
        self.old_sql_value_result, self.new_sql_value_result \
            = testcase_result[2], testcase_result[4], testcase_result[6], testcase_result[7], testcase_result[8], \
        testcase_result[9], testcase_result[10]

        try:
            self.scene_id = testcase_result[11]
        except Exception:
            pass
示例#9
0
 def get(self):
     user_id = session.get('user_id')
     mysql_search = request_get_values('mysql_search')
     page = request.args.get('page', 1, type=int)
     FrontLogs('进入mysql配置列表页面 第%s页' % page).add_to_front_log()
     #  pagination是salalchemy的方法,第一个参数:当前页数,per_pages:显示多少条内容 error_out:True 请求页数超出范围返回404错误 False:反之返回一个空列表
     pagination = Mysql.query.filter(Mysql.name.like(
             "%"+mysql_search+"%") if mysql_search is not None else "", Mysql.user_id == user_id). \
         order_by(Mysql.timestamp.desc()).paginate(
         page, per_page=current_app.config['FLASK_POST_PRE_ARGV'], error_out=False)
     # 返回一个内容对象
     mysqls = pagination.items
     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)
     return render_template('database/mysql_list.html',
                            pagination=pagination,
                            mysqls=mysqls)
示例#10
0
    def post(self):
        print('TestCaseRequest post request.form: ', request.form)
        testcase_ids = request.form.getlist('testcase')
        print("request_from_list: ", testcase_ids)
        testcase_list = []
        for index, testcase_id in enumerate(testcase_ids):
            testcase = TestCases.query.get(testcase_id)
            testcase.name = AnalysisParams().analysis_params(testcase.name)
            testcase_list.append(testcase)
        print('testcase_list: ', testcase_list)

        testcase_scene_ids = request.form.getlist('testcase_scene')
        for testcase_scene_id in testcase_scene_ids:
            testcase_scene = TestCaseScene.query.get(testcase_scene_id)
            testcases = testcase_scene.testcases
            for testcase in testcases:
                testcase.scene_name = testcase.testcase_scene.name
                testcase_list.append(testcase)
        print("request_testcase_ids_list: ", testcase_ids)

        return render_template('test_case_request/test_case_request_list.html',
                               items=testcase_list)
示例#11
0
 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)
示例#12
0
    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))
示例#13
0
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)