def load_test_file(edit_dict_case):
    """ load testset file, get testset data structure.
    @param file_path: absolute valid testset file path
    @return testset dict
        {
            "name": "desc1",
            "config": {},
            "api": {},
            "testcases": [testcase11, testcase12]
        }
    """
    testset = {
        "name": "",
        "config": {
            "path": os.getcwd() + "/debugtalk",
            "output": []
        },
        "api": {},
        "testcases": []
    }
    tests_list = edit_dict_case

    for item in tests_list:
        if not isinstance(item, dict) or len(item) != 1:
            raise exception.FileFormatError(
                "Testcase format error: {}".format(item))

        key, test_block = item.popitem()
        if not isinstance(test_block, dict):
            raise exception.FileFormatError(
                "Testcase format error: {}".format(item))

        if key == "config":
            testset["config"].update(test_block)
            testset["name"] = test_block.get("name", "")

        elif key == "test":
            if "api" in test_block:
                ref_call = test_block["api"]
                def_block = TestcaseLoader._get_block_by_name(ref_call, "api")
                TestcaseLoader._override_block(def_block, test_block)
                testset["testcases"].append(test_block)
            elif "suite" in test_block:
                ref_call = test_block["suite"]
                block = TestcaseLoader._get_block_by_name(ref_call, "suite")
                testset["testcases"].extend(block["testcases"])
            else:
                testset["testcases"].append(test_block)
            if "extract" in test_block:
                for i in test_block.get("extract"):
                    testset["config"]["output"].extend(i.keys())

        else:
            logger.log_warning(
                "unexpected block key: {}. block key should only be 'config' or 'test'."
                .format(key))

    return testset
Exemple #2
0
def _load_csv_file(csv_file):
    """ load csv file and check file content format
    @param
        csv_file: csv file path
        e.g. csv file content:
            username,password
            test1,111111
            test2,222222
            test3,333333
    @return
        list of parameter, each parameter is in dict format
        e.g.
        [
            {'username': '******', 'password': '******'},
            {'username': '******', 'password': '******'},
            {'username': '******', 'password': '******'}
        ]
    """
    csv_content_list = []
    parameter_list = None
    collums_num = 0
    with io.open(csv_file, encoding='utf-8') as data_file:
        for line in data_file:
            line_data = line.strip().split(",")
            if line_data == [""]:
                # ignore empty line
                continue

            if not parameter_list:
                # first line will always be parameter name
                expected_filename = "{}.csv".format("-".join(line_data))
                if not csv_file.endswith(expected_filename):
                    raise exception.FileFormatError(
                        "CSV file name does not match with headers: {}".format(
                            csv_file))

                parameter_list = line_data
                collums_num = len(parameter_list)
                continue

            # from the second line
            if len(line_data) != collums_num:
                err_msg = "CSV file collums does match with headers.\n"
                err_msg += "\tcsv file path: {}\n".format(csv_file)
                err_msg += "\terror line content: {}".format(line_data)
                raise exception.FileFormatError(err_msg)
            else:
                data = {}
                for index, parameter_name in enumerate(parameter_list):
                    data[parameter_name] = line_data[index]

                csv_content_list.append(data)

    return csv_content_list
Exemple #3
0
def check_format(file_path, content):
    """ check testcase format if valid
    """
    if not content:
        # testcase file content is empty
        err_msg = u"Testcase file content is empty: {}".format(file_path)
        logger.log_error(err_msg)
        raise exception.FileFormatError(err_msg)

    elif not isinstance(content, (list, dict)):
        # testcase file content does not match testcase format
        err_msg = u"Testcase file content format invalid: {}".format(file_path)
        logger.log_error(err_msg)
        raise exception.FileFormatError(err_msg)
    def load_api_file(file_path):
        """ load api definition from file and store in overall_def_dict["api"]
            api file should be in format below:
                [
                    {
                        "api": {
                            "def": "api_login",
                            "request": {},
                            "validate": []
                        }
                    },
                    {
                        "api": {
                            "def": "api_logout",
                            "request": {},
                            "validate": []
                        }
                    }
                ]
        """
        api_items = FileUtils.load_file(file_path)
        if not isinstance(api_items, list):
            raise exception.FileFormatError(
                "API format error: {}".format(file_path))

        for api_item in api_items:
            if not isinstance(api_item, dict) or len(api_item) != 1:
                raise exception.FileFormatError(
                    "API format error: {}".format(file_path))

            key, api_dict = api_item.popitem()
            if key != "api" or not isinstance(api_dict,
                                              dict) or "def" not in api_dict:
                raise exception.FileFormatError(
                    "API format error: {}".format(file_path))

            api_def = api_dict.pop("def")
            function_meta = parse_function(api_def)
            func_name = function_meta["func_name"]

            if func_name in TestcaseLoader.overall_def_dict["api"]:
                logger.log_warning(
                    "API definition duplicated: {}".format(func_name))

            api_dict["function_meta"] = function_meta
            TestcaseLoader.overall_def_dict["api"][func_name] = api_dict
Exemple #5
0
 def _check_format(file_path, content):
     """ check testcase fromat if valid
     """
     if not content:
         # testcase file content is empty
         err_msg = u"Testcase file conetent is empty: {}".format(file_path)
         logger.log_error(err_msg)
         raise exception.FileFormatError(err_msg)
Exemple #6
0
def _load_json_file(json_file):
    """ load json file and check file content format
    """
    with io.open(json_file, encoding='utf-8') as data_file:
        try:
            json_content = json.load(data_file)
        except exception.JSONDecodeError:
            err_msg = u"JSONDecodeError: JSON file format error: {}".format(json_file)
            logger.log_error(err_msg)
            raise exception.FileFormatError(err_msg)

        check_format(json_file, json_content)
        return json_content
    def load_test_file(file_path):
        """ load testcase file or suite file
        @param file_path: absolute valid file path
            file_path should be in format below:
                [
                    {
                        "config": {
                            "name": "",
                            "def": "suite_order()",
                            "request": {}
                        }
                    },
                    {
                        "test": {
                            "name": "add product to cart",
                            "api": "api_add_cart()",
                            "validate": []
                        }
                    },
                    {
                        "test": {
                            "name": "checkout cart",
                            "request": {},
                            "validate": []
                        }
                    }
                ]
        @return testset dict
            {
                "name": "desc1",
                "config": {},
                "testcases": [testcase11, testcase12]
            }
        """
        testset = {
            "name": "",
            "config": {
                "path": file_path
            },
            "testcases": []  # TODO: rename to tests
        }
        for item in FileUtils.load_file(file_path):
            if not isinstance(item, dict) or len(item) != 1:
                raise exception.FileFormatError(
                    "Testcase format error: {}".format(file_path))

            key, test_block = item.popitem()
            if not isinstance(test_block, dict):
                raise exception.FileFormatError(
                    "Testcase format error: {}".format(file_path))

            if key == "config":
                testset["config"].update(test_block)
                testset["name"] = test_block.get("name", "")

            elif key == "test":
                if "api" in test_block:
                    ref_call = test_block["api"]
                    def_block = TestcaseLoader._get_block_by_name(
                        ref_call, "api")
                    TestcaseLoader._override_block(def_block, test_block)
                    testset["testcases"].append(test_block)
                elif "suite" in test_block:
                    ref_call = test_block["suite"]
                    block = TestcaseLoader._get_block_by_name(
                        ref_call, "suite")
                    testset["testcases"].extend(block["testcases"])
                else:
                    testset["testcases"].append(test_block)

            else:
                logger.log_warning(
                    "unexpected block key: {}. block key should only be 'config' or 'test'."
                    .format(key))

        return testset
Exemple #8
0
    def load_test_file(file_path):
        """ load testcase file or suite file
        @param file_path: absolute valid file path
            file_path should be in format below:
                [
                    {
                        "config": {
                            "name": "",
                            "def": "suite_order()",
                            "request": {}
                        }
                    },
                    {
                        "test": {
                            "name": "add product to cart",
                            "api": "api_add_cart()",
                            "validate": []
                        }
                    },
                    {
                        "test": {
                            "name": "checkout cart",
                            "request": {},
                            "validate": []
                        }
                    }
                ]
        @return testset dict
            {
                "name": "desc1",
                "config": {},
                "testcases": [testcase11, testcase12]
            }
        """
        testset = {
            "name": "",
            "config": {
                "path": file_path
            },
            "testcases": []  # TODO: rename to tests
        }
        for item in FileUtils.load_file(file_path):
            if not isinstance(item, dict) or len(item) != 1:
                raise exception.FileFormatError(
                    "Testcase format error: {}".format(file_path))

            key, test_block = item.popitem()
            if not isinstance(test_block, dict):
                raise exception.FileFormatError(
                    "Testcase format error: {}".format(file_path))

            if key == "config":
                testset["config"].update(test_block)
                testset["name"] = test_block.get("name", "")
                #add by zhengchun, can define runner
                if "runner" in test_block:
                    # module_name, cls_name=item["config"]["runner"].split(".")[0:2]
                    s = test_block["runner"]
                    inx = s.rfind(".")
                    if (inx <= 0):
                        raise exception.ParamsError("runner format error[%s]" %
                                                    (s))
                    module_name = s[0:inx]
                    cls_name = s[inx + 1:]
                    imported_module = utils.get_imported_module(module_name)
                    ip_module_cls = getattr(imported_module, cls_name)
                    testset["runner"] = ip_module_cls
                else:
                    #defalut runner
                    imported_module = utils.get_imported_module("runner")
                    ip_module_cls = getattr(imported_module, "Runner")
                    testset["runner"] = ip_module_cls

            elif key == "test":
                if "api" in test_block:
                    ref_call = test_block["api"]
                    def_block = TestcaseLoader._get_block_by_name(
                        ref_call, "api")
                    TestcaseLoader._override_block(def_block, test_block)
                    testset["testcases"].append(test_block)
                elif "suite" in test_block:
                    ref_call = test_block["suite"]
                    block = TestcaseLoader._get_block_by_name(
                        ref_call, "suite")
                    testset["testcases"].extend(block["testcases"])
                else:
                    testset["testcases"].append(test_block)

            else:
                logger.log_warning(
                    "unexpected block key: {}. block key should only be 'config' or 'test'."
                    .format(key))

        return testset