Exemplo n.º 1
0
def run_testcase_by_id(testcase_id, httpobj=None, testplan='无计划'):
    try:
        testcase_info = mytestlink.getTestCase(testcase_id)  # 获取测试用例基本信息
        logger.info('获取测试用例信息 %s' % testcase_info)
    except Exception as e:
        logger.error('获取用例信息失败 %s,,暂停执行该用例' % e)
        return ('Fail',[('global_funtion_module','获取用例信息失败 %s' % e)])
    # 获取用例所在套件和项目名称
    response = mytestlink.getFullPath([int(testcase_id)])
    response = response[str(testcase_id)]
    testsuite_name = ''
    for suit in response[1:]:
        testsuite_name = testsuite_name + '-' + suit
        testsuite_name = testsuite_name.lstrip('-')
    project_name = response[0]

    # 构造测试用例对象
    testcase_name = testcase_info[0]['name']
    testcase_steps = testcase_info[0]['steps']
    testcase_isactive = int(testcase_info[0]['active'])
    testsuite_id = int(testcase_info[0]['testsuite_id'])
    preconditions = testcase_info[0]['preconditions']
    tc_external_id = testcase_info[0]['full_tc_external_id']
    testcase_obj = TestCase(testcase_id, testcase_name, testcase_steps, testcase_isactive, project_name, testsuite_id, tc_external_id, preconditions)
    if httpobj:
        myhttp = httpobj
    else:
        myhttp = get_http_conf_of_project(project_name)
        if not myhttp:
            logger.critical('用例所在项目配置存在问题,停止执行该用例')
            return ('Block',[('golbal_function_module', '用例所在项目配置存在问题,停止执行该用例')])
    try:
        sql_insert = 'INSERT INTO '+testcase_report_tb +'(executed_history_id, testcase_id, testcase_name, testsuit, testplan, project, runresult, runtime, tc_external_id)' \
                                                        ' VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s)'
        data = (executed_history_id, testcase_id, testcase_name, testsuite_name, testplan, project_name, 'Block','0000-00-00 00:00:00', tc_external_id)
        logger.info('记录测试用例到测试用例报表')
        testdb.execute_insert(sql_insert, data)

        logger.info('开始执行测试用例[id=%s,name=%s]' % (testcase_id, testcase_name))
        run_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())  # 记录运行时间
        case_executed_history_id = time.strftime('%Y%m%d%H%M%S', time.localtime())  # 流水记录编号
        testcase_run_result = testcase_obj.run_testcase(myhttp, testplan, case_executed_history_id)

        logger.info('正在更新用例执行结果')
        sql_update = 'UPDATE '+testcase_report_tb +' SET runresult=\"%s\", runtime=\"%s\",' \
                                                   ' case_exec_history_id=\"%s\"' \
                                                   ' WHERE executed_history_id = %s and testcase_id = %s' \
                                                   ' AND project=\'%s\' AND testplan=\'%s\''
        data = (testcase_run_result[0], run_time, str(case_executed_history_id), executed_history_id, testcase_id, project_name, testplan)
        testdb.execute_update(sql_update, data)

        logger.info('指定用例[%s]已执行完' % testcase_name)

        return testcase_run_result
    except Exception as e:
        logger.error('运行用例出错 %s' % e)
        return ('Fail',[('golbal_function_module', '%s' % e)])
Exemplo n.º 2
0
    if sys.argv[1] == '1':
        run_mode_conf = RunModeConfig('./config/runmodeconfig_test.conf')
    elif sys.argv[1] == '2':
        run_mode_conf = RunModeConfig('./config/runmodeconfig_release.conf')
    run_mode = int(run_mode_conf.get_run_mode())

    logger.info('正在获取运行全局初始化用例……')
    global_cases_list = run_mode_conf.get_global_cases()

    project_httpobj_dict = {}
    for item in global_cases_list:
        project_name = item[0]
        cases_list = item[1]
        httpobj = get_http_conf_of_project(project_name)
        if not httpobj:
            logger.critical('初始化用例所在项目配置存在问题,提前终止程序运行')
            exit()
        else:
            logger.info('正在运行项目:"%s"的全局初始化用例id列表:%s' %
                        (project_name, cases_list))
            project_httpobj_dict[project_name] = httpobj
            for case_id in cases_list:
                res = run_testcase_by_id(case_id, httpobj)
                logger.info('用例[%s]运行结果:%s' % (id, res))

    if 1 == run_mode:
        logger.info('按项目运行测试')
        project_mode = run_mode_conf.get_project_mode()
        testplans_name_list = []
        if 1 == project_mode:
            logger.info('运行所有项目')