示例#1
0
def generate_optional_param(valid_param={}, optional_key_list=[]):
    param_list = []
    for key in optional_key_list:

        # if(key == "token"):
        #     token =
        #     param_list.append({"valid": login()})

        param = dict(valid_param)
        if para_input[key]["valid"]:
            param[key] = random.choice(para_input[key]["valid"])
            param_list.append({"valid": param})
        else:
            _logging()._warn("%s has no valid value" % key)

        if para_input[key].has_key("invalid"):

            for item in para_input[key]["invalid"]:
                inparam = dict(valid_param)
                inparam[key] = item
                if key == 'poi':
                    param_list.append({"poi_invalid": inparam})
                else:
                    param_list.append({"invalid": inparam})

        if para_input[key].has_key("no_data"):

            for item in para_input[key]["no_data"]:
                inparam = dict(valid_param)
                inparam[key] = item

                param_list.append({"no_data": inparam})
    if optional_key_list:
        para_with_all_option = dict(valid_param)
        for key in optional_key_list:
            para_with_all_option[key] = random.choice(para_input["key"]["valid"])
        param_list.append({"valid": para_with_all_option})
        # else:
        #     _logging()._warn("%s has no invalid value" % key)

    return param_list
示例#2
0
def yaml_load_all(file_path, version="v2.2", default_method="ALL"):

    load_logging = _logging()

    new_mtime = os.path.getmtime(file_path)

    time_config = Config()

    mtime = time_config.get("time", "mtime")

    if new_mtime > float(mtime):

        time_config.set("time", "mtime", 1453880617)

        stream = file(file_path, "r")

        data = yaml.load(stream)

        # base_url = data["configuration"]["uri"]
        # access_url = const.base_url
        methods = data["versions"].get(version)["methods"]

        if default_method != "ALL":
            load_logging._info("Specify method: %s" % default_method)

            if default_method not in methods.keys():
                load_logging._error("Method Wrong!!!")
                raise Exception
                #todo
            else:
                print 1, default_method
                generate_case_by_method(methods, default_method, load_logging)

        else:

            load_logging._info("Total methods: %d" % len(methods.keys()))
            # token = login()
            # methods_list = list()
            index = 1
            for method in methods.keys():
                print index, method
                generate_case_by_method(methods, method, load_logging)
                index += 1
    else:
        load_logging._info("Yaml file has not been modified since last loading...No generating...")
示例#3
0
def generate_test_data(method_name, url, para_list=None, need_token=True):

    gen_logging = _logging()

    try:
        gen_logging._info("Start creating robot file for %s..." % method_name)
        case_path = "../robot/TEST_" + method_name + ".robot"

        gen_logging._info(case_path)

        # if os.path.exists(case_path):
        #     os.remove(case_path)

        shutil.copy("../robot/Base_Robot_Suite.robot", case_path)
        case_file = open(case_path, "a+")

        # for line in case_file.readlines():
        #     pass

        index = 1

        # para_list is NONE
        if not para_list:

            case_name = method_name + "_00000"

            if not need_token:
                gen_logging._info("Method %s has no parameters." % method_name)
                test_case_template = case_config.base_case_template % (case_name, url, "", "need_token=False")
                case_file.write(test_case_template)
                case_file.write(case_config.response_validate_template % (method_name, "normal"))

            else:
                gen_logging._info("Method %s has one parameter:token." % method_name)
                test_case_template = case_config.base_case_template % (case_name, url, "", "need_token=True")
                case_file.write(test_case_template)
                case_file.write(case_config.response_validate_template % (method_name, "normal"))

                case_name = method_name + "_00000_bad_token"
                write_test_token_case(case_file, case_name, method_name, url, test_token=True)

                case_name = method_name + "_00000_no_token"
                write_test_token_case(case_file, case_name, method_name, url, test_no_token=True)

        # para_list is not NONE
        else:
            test_token_param = para_list[0]["valid"]

            for para in para_list:
                case_name = method_name + "_" + "%05d" % index

                is_valid = para.has_key("valid")
                is_poi_invalid = para.has_key("poi_invalid")
                no_data_tag = para.has_key("no_data")
                if need_token:
                    test_case_template = case_config.base_case_template % (case_name, url, para.popitem()[1], "need_token=True")
                else:
                    test_case_template = case_config.base_case_template % (case_name, url, para.popitem()[1], "need_token=False")
                case_file.write(test_case_template)
                if is_valid:
                    case_file.write(case_config.response_validate_template % (method_name, "normal"))
                elif is_poi_invalid:
                    case_file.write(case_config.response_validate_template % (method_name, "invalid_position"))
                elif no_data_tag:
                    case_file.write(case_config.response_validate_template % (method_name, "no_data"))
                else:
                    case_file.write(case_config.response_validate_template % (method_name, "para_error"))

                index += 1

            # test case: bad token
            if need_token:
                case_name = method_name + "_" + "%05d" % index
                write_test_token_case(case_file, case_name, method_name, url, test_token_param=test_token_param, test_token=True)

                # test case: no token
                case_name = method_name + "_" + "%05d" % (index+1)
                write_test_token_case(case_file, case_name, method_name, url, test_token_param=test_token_param, test_no_token=True)

        gen_logging._info("Create TEST_%s.robot successfully!" % method_name)

        case_file.close()
    except Exception,e:
        gen_logging._exception(e)