示例#1
0
def upload_file_logic(files, project, module, account):
    """
    解析yaml或者json用例
    :param files:
    :param project:
    :param module:
    :param account:
    :return:
    """

    for file in files:
        file_suffix = os.path.splitext(file)[1].lower()
        if file_suffix == '.json':
            with io.open(file, encoding='utf-8') as data_file:
                try:
                    content = json.load(data_file)
                except JSONDecodeError:
                    err_msg = u"JSONDecodeError: JSON file format error: {}".format(
                        file)
                    logging.error(err_msg)

        elif file_suffix in ['.yaml', '.yml']:
            with io.open(file, 'r', encoding='utf-8') as stream:
                content = yaml.load(stream)

        for test_case in content:
            test_dict = {
                'project': project,
                'module': module,
                'author': account,
                'include': []
            }
            if 'config' in test_case.keys():
                test_case.get('config')['config_info'] = test_dict
                test_case['user_account'] = account
                add_config_data(type=True, **test_case)

            if 'test' in test_case.keys():  # 忽略config
                test_case.get('test')['case_info'] = test_dict

                if 'validate' in test_case.get(
                        'test').keys():  # 适配validate两种格式
                    validate = test_case.get('test').pop('validate')
                    new_validate = []
                    for check in validate:
                        if 'comparator' not in check.keys():
                            for key, value in check.items():
                                tmp_check = {
                                    "check": value[0],
                                    "comparator": key,
                                    "expected": value[1]
                                }
                                new_validate.append(tmp_check)

                    test_case.get('test')['validate'] = new_validate
                test_case['user_account'] = account
                add_case_data(type=True, **test_case)
示例#2
0
def case_info_logic(type=True, **kwargs):
    test = kwargs.pop('test')
    '''
        动态展示模块
    '''
    if 'request' not in test.keys():
        return load_modules(**test)
    else:
        if test.get('name').get('case_name') is '':
            return '用例名称不可为空'
        if test.get('name').get('project') is None or test.get('name').get(
                'project') is '':
            return '请先添加项目'
        if test.get('name').get('module') is None or test.get('name').get(
                'module') is '':
            return '请先添加模块'
        if test.get('name').get('author') is '':
            return '创建者不能为空'
        if test.get('request').get('url') is '':
            return '接口地址不能为空'
        if not test.get('validate'):
            return '至少需要一个结果校验!'

        name = test.pop('name')
        test.setdefault('name', name.pop('case_name'))

        test.setdefault('case_info', name)

        validate = test.pop('validate')
        test.setdefault('validate', key_value_list(**validate))

        extract = test.pop('extract')
        if extract:
            test.setdefault('extract', key_value_list(mode=2, **extract))

        request_data = test.get('request').pop('request_data')
        date_type = test.get('request').pop('type')
        if request_data and date_type:
            test.get('request').setdefault(date_type,
                                           key_value_dict(**request_data))

        headers = test.get('request').pop('headers')
        if headers:
            test.get('request').setdefault('headers',
                                           key_value_dict(mode=2, **headers))

        variables = test.pop('variables')
        if variables:
            test.setdefault('variables', key_value_list(mode=3, **variables))

        setup = test.pop('setup')
        if setup:
            test.setdefault('setup', key_value_list(mode=2, **setup))

        teardown = test.pop('teardown')
        if teardown:
            test.setdefault('teardown', key_value_list(mode=2, **teardown))

        kwargs.setdefault('test', test)
        return add_case_data(type, **kwargs)
def case_info_logic(type=True, **kwargs):
    test = kwargs.pop('test')
    '''
        动态展示模块
    '''
    if 'request' not in test.keys():
        return load_modules(**test)
    else:
        logging.info('用例原始信息: {kwargs}'.format(kwargs=kwargs))
        if test.get('name').get('case_name') is '':
            return '用例名称不可为空'
        if test.get('name').get('project') is None or test.get('name').get(
                'project') is '':
            return '请先添加项目'
        if test.get('name').get('module') is None or test.get('name').get(
                'module') is '':
            return '请先添加模块'
        if test.get('name').get('author') is '':
            return '创建者不能为空'
        if test.get('request').get('url') is '':
            return '接口地址不能为空'

        name = test.pop('name')
        test.setdefault('name', name.pop('case_name'))

        test.setdefault('case_info', name)

        validate = test.pop('validate')
        test.setdefault('validate', key_value_list('validate', **validate))

        extract = test.pop('extract')
        if extract:
            test.setdefault('extract', key_value_list('extract', **extract))

        request_data = test.get('request').pop('request_data')
        data_type = test.get('request').pop('type')
        if request_data and data_type:
            if data_type == 'json':
                test.get('request').setdefault(data_type, request_data)
            else:
                test.get('request').setdefault(
                    data_type, key_value_dict('data', **request_data))

        headers = test.get('request').pop('headers')
        if headers:
            test.get('request').setdefault(
                'headers', key_value_dict('headers', **headers))

        variables = test.pop('variables')
        if variables:
            test.setdefault('variables',
                            key_value_list('variables', **variables))

        kwargs.setdefault('test', test)
        return add_case_data(type, **kwargs)
示例#4
0
def upload_file_logic(files, project, module, account):
    """
    :param file: 需要解析的文件,可以是列表形式
    :return:  统一返回结果列表
    """

    for file in files:
        with io.open(file, 'r', encoding='utf-8') as stream:
            yaml_content = yaml.load(stream)
            for test_case in yaml_content:
                if 'test' in test_case.keys():  #忽略config
                    name = test_case.get('test').get('name')
                    test_case.get('test')['case_info'] = {
                        'name': name,
                        'project': project,
                        'module': module,
                        'author': account,
                        'include': []
                    }
                    add_case_data(type=True, **test_case)
示例#5
0
def case_info_logic(type=True, **kwargs):
    """
    用例信息逻辑处理以数据处理
    :param type: boolean: True 默认新增用例信息, False: 更新用例
    :param kwargs: dict: 用例信息
    :return: str: ok or tips
    """
    test = kwargs.pop('test')
    '''
        动态展示模块
    '''
    if 'request' not in test.keys():
        type = test.pop('type')
        return load_modules(**test) if type == 'module' else load_cases(**test)
    else:
        logging.info('用例原始信息: {kwargs}'.format(kwargs=kwargs))
        if test.get('name').get('case_name') is '':
            return '用例名称不可为空'
        if test.get('name').get('author') is '':
            return '创建者不能为空'
        if test.get('request').get('url') is '':
            return '接口地址不能为空'
        if test.get('name').get('module') == '请选择':
            return '请选择模块'
        if test.get('name').get('project') == '请选择':
            return '请选择项目'
        if test.get('name').get('project') == '':
            return '请先添加项目'
        if test.get('name').get('module') == '':
            return '请先添加模块'

        name = test.pop('name')
        test.setdefault('name', name.pop('case_name'))

        test.setdefault('case_info', name)

        validate = test.pop('validate')
        if validate:
            validate_list = key_value_list('validate', **validate)
            if not isinstance(validate_list, list):
                return validate_list
            test.setdefault('validate', validate_list)

        extract = test.pop('extract')
        if extract:
            test.setdefault('extract', key_value_list('extract', **extract))

        request_data = test.get('request').pop('request_data')
        data_type = test.get('request').pop('type')
        if request_data and data_type:
            if data_type == 'json':
                test.get('request').setdefault(data_type, request_data)
            else:
                data_dict = key_value_dict('data', **request_data)
                if not isinstance(data_dict, dict):
                    return data_dict
                test.get('request').setdefault(data_type, data_dict)

        headers = test.get('request').pop('headers')
        if headers:
            test.get('request').setdefault(
                'headers', key_value_dict('headers', **headers))

        variables = test.pop('variables')
        if variables:
            variables_list = key_value_list('variables', **variables)
            if not isinstance(variables_list, list):
                return variables_list
            test.setdefault('variables', variables_list)

        parameters = test.pop('parameters')
        if parameters:
            params_list = key_value_list('parameters', **parameters)
            if not isinstance(params_list, list):
                return params_list
            test.setdefault('parameters', params_list)

        hooks = test.pop('hooks')
        if hooks:

            setup_hooks_list = key_value_list('setup_hooks', **hooks)
            if not isinstance(setup_hooks_list, list):
                return setup_hooks_list
            test.setdefault('setup_hooks', setup_hooks_list)

            teardown_hooks_list = key_value_list('teardown_hooks', **hooks)
            if not isinstance(teardown_hooks_list, list):
                return teardown_hooks_list
            test.setdefault('teardown_hooks', teardown_hooks_list)

        kwargs.setdefault('test', test)
        return add_case_data(type, **kwargs)
示例#6
0
def upload_file_logic(files, project, module, account):
    """
    解析yaml或者json用例
    :param files:
    :param project:
    :param module:
    :param account:
    :return:
    """

    is_har = 0

    for file in files:
        file_suffix = os.path.splitext(file)[1].lower()
        if file_suffix == '.json':
            with io.open(file, encoding='utf-8') as data_file:
                try:
                    content = json.load(data_file)
                except JSONDecodeError:
                    err_msg = u"JSONDecodeError: JSON file format error: {}".format(
                        file)
                    logging.error(err_msg)

        elif file_suffix in ['.yaml', '.yml']:
            with io.open(file, 'r', encoding='utf-8') as stream:
                content = yaml.load(stream)
        elif file_suffix == '.har':
            har_parser = HarParser(file)
            content = har_parser._make_testcase("v1")
            is_har = 1

        version = 1
        for test_case in content:
            test_dict = {
                'project': project,
                'module': module,
                'author': account,
                'include': []
            }
            if 'config' in test_case.keys():
                test_case.get('config')['config_info'] = test_dict
                # add_config_data(type=True, **test_case)

            if 'test' in test_case.keys():  # 忽略config
                test_case.get('test')['case_info'] = test_dict

                if 'validate' in test_case.get(
                        'test').keys():  # 适配validate两种格式
                    validate = test_case.get('test').pop('validate')
                    new_validate = []
                    for check in validate:
                        if 'comparator' not in check.keys():
                            for key, value in check.items():
                                tmp_check = {
                                    "check": value[0],
                                    "comparator": key,
                                    "expected": value[1]
                                }
                                new_validate.append(tmp_check)

                    test_case.get('test')['validate'] = new_validate
                '''通过har导入的,如果头信息里面包含apiCode,则将用例名称设置为apiCode'''
                if is_har == 1:
                    '''验证导入有问题,先不导入'''
                    test_case.get('test')['validate'] = []

                    req = test_case.get('test').get('request')
                    name = test_case.get('test').get('name')
                    names = name.split('.')
                    suffix = names[len(names) - 1]
                    if suffix == 'js' or suffix == 'css' or suffix == 'svg' or suffix == 'ico' or suffix == 'woff' or suffix == 'png':
                        continue

                    if req is not None:
                        method = req.get('headers').get('method')
                        if method is not None:
                            test_case['test']['name'] = method

                # 替换用例中名称包含的'/'符号,并追加版本号
                if '/' in test_case['test']['name']:
                    test_case['test']['name'] = str(
                        test_case['test']['name']).replace(
                            '/', '-') + '-' + str(version)

                add_case_data(type=True, **test_case)
                version = version + 1
示例#7
0
def case_info_logic(type=True, **kwargs):
    data = package_test_data(type, **kwargs)
    if isinstance(data, str):
        return data
    return add_case_data(type, **data)
示例#8
0
def case_info_logic(type=True, **kwargs):
    """
    用例信息逻辑处理以数据处理
    :param type: boolean: True 默认新增用例信息, False: 更新用例
    :param kwargs: dict: 用例信息
    :return: str: ok or tips
    """
    test = kwargs.pop('test')
    '''
        动态展示模块
    '''
    if 'request' not in test.keys():
        type = test.pop('type')
        if type == 'module':
            return load_modules(**test)
        elif type == 'case':
            return load_cases(**test)
        else:
            return load_cases(type=2, **test)

    else:
        logging.info('用例原始信息: {kwargs}'.format(kwargs=kwargs))
        if test.get('name').get('case_name') is '':
            return '用例名称不可为空'
        if test.get('name').get('module') == '请选择':
            return '请选择或者添加模块'
        if test.get('name').get('project') == '请选择':
            return '请选择项目'
        if test.get('name').get('project') == '':
            return '请先添加项目'
        if test.get('name').get('module') == '':
            return '请添加模块'

        name = test.pop('name')
        test.setdefault('name', name.pop('case_name'))

        test.setdefault('case_info', name)

        validate = test.pop('validate')
        if validate:
            validate_list = key_value_list('validate', **validate)
            if not isinstance(validate_list, list):
                return validate_list
            test.setdefault('validate', validate_list)

        extract = test.pop('extract')
        if extract:
            test.setdefault('extract', key_value_list('extract', **extract))

        request_data = test.get('request').pop('request_data')
        data_type = test.get('request').pop('type')
        if request_data!={} and data_type:   # 修改if request_data and data_type 为if request_data != {} and data_type 兼容type=json且数据为False情况
            if data_type == 'json':
                test.get('request').setdefault(data_type, request_data)
            else:
                data_dict = key_value_dict('data', **request_data)
                if not isinstance(data_dict, dict):
                    return data_dict
                if len(data_dict)==1 and list(data_dict.values())[0] == "FilesOnly":# 新增data中x-FilesOnly时候吃,存入data:"x"
                    data_dict = str(list(data_dict.keys())[0])
                test.get('request').setdefault(data_type, data_dict)

        headers = test.get('request').pop('headers')
        if headers:
            test.get('request').setdefault('headers', key_value_dict('headers', **headers))

        variables = test.pop('variables')
        if variables:
            variables_list = key_value_list('variables', **variables)
            if not isinstance(variables_list, list):
                return variables_list
            test.setdefault('variables', variables_list)

        parameters = test.pop('parameters')
        if parameters:
            params_list = key_value_list('parameters', **parameters)
            if not isinstance(params_list, list):
                return params_list
            test.setdefault('parameters', params_list)

        hooks = test.pop('hooks')
        if hooks:

            setup_hooks_list = key_value_list('setup_hooks', **hooks)
            if not isinstance(setup_hooks_list, list):
                return setup_hooks_list
            test.setdefault('setup_hooks', setup_hooks_list)

            teardown_hooks_list = key_value_list('teardown_hooks', **hooks)
            if not isinstance(teardown_hooks_list, list):
                return teardown_hooks_list
            test.setdefault('teardown_hooks', teardown_hooks_list)

        kwargs.setdefault('test', test)
        return add_case_data(type, **kwargs)
示例#9
0
def case_info_logic(type=True, **kwargs):
    """
    用例信息逻辑处理以数据处理
    :param type: boolean: True 默认新增用例信息, False: 更新用例
    :param kwargs: dict: 用例信息
    :return: str: ok or tips
    """
    test = kwargs.pop('test')

    # dic = {
    #     "case_name": {
    #         "error_val": "",
    #         "error_msg": "项目名称不能为空"
    #     },
    #     "module": {
    #         "error_val": "",
    #         "error_msg": "负责人不能为空"
    #     },
    #     "project": {
    #         "error_val": "",
    #         "error_msg": "测试人员不能为空"
    #     },
    #     "dev_user": {
    #         "error_val": "",
    #         "error_msg": "开发人员不能为空"
    #     },
    #     "publish_app": {
    #         "error_val": "",
    #         "error_msg": "发布应用不能为空"
    #     },
    # }
    # 动态展示模块
    if 'request' not in test.keys():
        case_type = test.pop('type')
        if case_type == 'module':
            return load_modules(**test)
        elif case_type == 'case':
            return load_cases(**test)
        else:
            return load_cases(case_type=2, **test)

    else:
        logging.info('用例原始信息: {kwargs}'.format(kwargs=kwargs))
        if test.get('name').get('case_name') is '':
            return '用例名称不可为空'
        if test.get('name').get('module') == '请选择':
            return '请选择或者添加模块'
        if test.get('name').get('project') == '请选择':
            return '请选择项目'
        if test.get('name').get('project') == '':
            return '请先添加项目'
        if test.get('name').get('module') == '':
            return '请添加模块'

        name = test.pop('name')
        test.setdefault('name', name.pop('case_name'))

        test.setdefault('case_info', name)

        validate = test.pop('validate')
        if validate:
            validate_list = key_value_list('validate', **validate)
            if not isinstance(validate_list, list):
                return validate_list
            test.setdefault('validate', validate_list)

        extract = test.pop('extract')
        if extract:
            test.setdefault('extract', key_value_list('extract', **extract))

        request_data = test.get('request').pop('request_data')
        data_type = test.get('request').pop('type')
        if request_data and data_type:
            if data_type == 'json':
                test.get('request').setdefault(data_type, request_data)
            else:
                data_dict = key_value_dict('data', **request_data)
                if not isinstance(data_dict, dict):
                    return data_dict
                test.get('request').setdefault(data_type, data_dict)

        headers = test.get('request').pop('headers')
        if headers:
            test.get('request').setdefault(
                'headers', key_value_dict('headers', **headers))

        variables = test.pop('variables')
        if variables:
            variables_list = key_value_list('variables', **variables)
            if not isinstance(variables_list, list):
                return variables_list
            test.setdefault('variables', variables_list)

        parameters = test.pop('parameters')
        if parameters:
            params_list = key_value_list('parameters', **parameters)
            if not isinstance(params_list, list):
                return params_list
            test.setdefault('parameters', params_list)

        hooks = test.pop('hooks')
        if hooks:

            setup_hooks_list = key_value_list('setup_hooks', **hooks)
            if not isinstance(setup_hooks_list, list):
                return setup_hooks_list
            test.setdefault('setup_hooks', setup_hooks_list)

            teardown_hooks_list = key_value_list('teardown_hooks', **hooks)
            if not isinstance(teardown_hooks_list, list):
                return teardown_hooks_list
            test.setdefault('teardown_hooks', teardown_hooks_list)

        kwargs.setdefault('test', test)
        return add_case_data(type, **kwargs)