Esempio n. 1
0
    def test_bugfix_type_match(self):
        testcase_file_path = os.path.join(os.getcwd(),
                                          'tests/data/test_bugfix.yml')
        testcases = FileUtils.load_file(testcase_file_path)
        config_dict = {"path": testcase_file_path}
        self.test_runner.init_config(config_dict, "testset")

        test = testcases[2]["test"]
        self.test_runner.run_test(test)
Esempio n. 2
0
    def parameterize(self, csv_file_name, fetch_method="Sequential"):
        parameter_file_path = os.path.join(os.path.dirname(self.file_path),
                                           "{}".format(csv_file_name))
        csv_content_list = FileUtils.load_file(parameter_file_path)

        if fetch_method.lower() == "random":
            random.shuffle(csv_content_list)

        return csv_content_list
Esempio n. 3
0
    def load_testsets_by_path(path):
        """ load testcases from file path
        @param path: path could be in several type
            - absolute/relative file path
            - absolute/relative folder path
            - list/set container with file(s) and/or folder(s)
        @return testcase sets list, each testset is corresponding to a file
            [
                testset_dict_1,
                testset_dict_2
            ]
        """
        if isinstance(path, (list, set)):
            testsets = []

            for file_path in set(path):
                testset = TestcaseLoader.load_testsets_by_path(file_path)
                if not testset:
                    continue
                testsets.extend(testset)

            return testsets

        if not os.path.isabs(path):
            path = os.path.join(os.getcwd(), path)

        if path in TestcaseLoader.testcases_cache_mapping:
            return TestcaseLoader.testcases_cache_mapping[path]

        if os.path.isdir(path):
            files_list = FileUtils.load_folder_files(path)
            testcases_list = TestcaseLoader.load_testsets_by_path(files_list)

        elif os.path.isfile(path):
            try:
                testset = TestcaseLoader.load_test_file(path)
                '''修改源码:增加key是否存在判断'''
                if testset["testcases"] or ('api' in testset.keys()
                                            and testset["api"]):
                    testcases_list = [testset]
                else:
                    testcases_list = []
                    raise Exception(
                        '用例执行错误,请检查',
                        '用例名称:' + testset.get('config', {}).get('name'))
            except exceptions.FileFormatError:
                testcases_list = []
            # except Exception:
            #     testcases_list = []

        else:
            logger.log_error(u"file not found: {}".format(path))
            testcases_list = []

        TestcaseLoader.testcases_cache_mapping[path] = testcases_list
        return testcases_list
    def test_load_yaml_file_file_format_error(self):
        yaml_tmp_file = "tests/data/tmp.yml"
        # create empty yaml file
        with open(yaml_tmp_file, 'w') as f:
            f.write("")

        with self.assertRaises(exception.FileFormatError):
            FileUtils._load_yaml_file(yaml_tmp_file)

        os.remove(yaml_tmp_file)

        # create invalid format yaml file
        with open(yaml_tmp_file, 'w') as f:
            f.write("abc")

        with self.assertRaises(exception.FileFormatError):
            FileUtils._load_yaml_file(yaml_tmp_file)

        os.remove(yaml_tmp_file)
Esempio n. 5
0
 def test_load_csv_file_one_parameter(self):
     csv_file_path = os.path.join(os.getcwd(), 'tests/data/user_agent.csv')
     csv_content = FileUtils.load_file(csv_file_path)
     self.assertEqual(csv_content, [{
         'user_agent': 'iOS/10.1'
     }, {
         'user_agent': 'iOS/10.2'
     }, {
         'user_agent': 'iOS/10.3'
     }])
 def test_load_yaml_testcases(self):
     testcase_file_path = os.path.join(
         os.getcwd(), 'tests/data/demo_testset_hardcode.yml')
     testcases = FileUtils.load_file(testcase_file_path)
     self.assertEqual(len(testcases), 3)
     test = testcases[0]["test"]
     self.assertIn('name', test)
     self.assertIn('request', test)
     self.assertIn('url', test['request'])
     self.assertIn('method', test['request'])
 def test_load_csv_file_multiple_parameters(self):
     csv_file_path = os.path.join(
         os.getcwd(), 'tests/data/account.csv')
     csv_content = FileUtils.load_file(csv_file_path)
     self.assertEqual(
         csv_content,
         [
             {'username': '******', 'password': '******'},
             {'username': '******', 'password': '******'},
             {'username': '******', 'password': '******'}
         ]
     )
    def test_load_folder_files(self):
        folder = os.path.join(os.getcwd(), 'tests')
        file1 = os.path.join(os.getcwd(), 'tests', 'test_utils.py')
        file2 = os.path.join(os.getcwd(), 'tests', 'data', 'demo_binds.yml')

        files = FileUtils.load_folder_files(folder, recursive=False)
        self.assertNotIn(file2, files)

        files = FileUtils.load_folder_files(folder)
        self.assertIn(file2, files)
        self.assertNotIn(file1, files)

        files = FileUtils.load_folder_files(folder)
        api_file = os.path.join(os.getcwd(), 'tests', 'api', 'basic.yml')
        self.assertIn(api_file, files)

        files = FileUtils.load_folder_files("not_existed_foulder", recursive=False)
        self.assertEqual([], files)

        files = FileUtils.load_folder_files(file2, recursive=False)
        self.assertEqual([], files)
    def test_load_json_file_file_format_error(self):
        json_tmp_file = "tests/data/tmp.json"
        # create empty file
        with open(json_tmp_file, 'w') as f:
            f.write("")

        with self.assertRaises(exception.FileFormatError):
            FileUtils._load_json_file(json_tmp_file)

        os.remove(json_tmp_file)

        # create empty json file
        with open(json_tmp_file, 'w') as f:
            f.write("{}")

        with self.assertRaises(exception.FileFormatError):
            FileUtils._load_json_file(json_tmp_file)

        os.remove(json_tmp_file)

        # create invalid format json file
        with open(json_tmp_file, 'w') as f:
            f.write("abc")

        with self.assertRaises(exception.FileFormatError):
            FileUtils._load_json_file(json_tmp_file)

        os.remove(json_tmp_file)
Esempio n. 10
0
    def load_test_dependencies():
        """ load all api and suite definitions.
            default api folder is "$CWD/tests/api/".
            default suite folder is "$CWD/tests/suite/".
        """
        # TODO: cache api and suite loading
        # load api definitions
        api_def_folder = os.path.join(os.getcwd(), "tests", "api")
        for test_file in FileUtils.load_folder_files(api_def_folder):
            TestcaseLoader.load_api_file(test_file)

        # load suite definitions
        suite_def_folder = os.path.join(os.getcwd(), "tests", "suite")
        for suite_file in FileUtils.load_folder_files(suite_def_folder):
            suite = TestcaseLoader.load_test_file(suite_file)
            if "def" not in suite["config"]:
                raise exception.ParamsError("def missed in suite file: {}!".format(suite_file))

            call_func = suite["config"]["def"]
            function_meta = parse_function(call_func)
            suite["function_meta"] = function_meta
            TestcaseLoader.overall_def_dict["suite"][function_meta["func_name"]] = suite
Esempio n. 11
0
    def test_run_single_testcase(self):
        for testcase_file_path in self.testcase_file_path_list:
            testcases = FileUtils.load_file(testcase_file_path)

            config_dict = {"path": testcase_file_path}
            self.test_runner.init_config(config_dict, "testset")

            test = testcases[0]["test"]
            self.test_runner.run_test(test)

            test = testcases[1]["test"]
            self.test_runner.run_test(test)

            test = testcases[2]["test"]
            self.test_runner.run_test(test)
Esempio n. 12
0
    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
Esempio n. 13
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", "")

            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
Esempio n. 14
0
 def setUp(self):
     self.context = Context()
     testcase_file_path = os.path.join(os.getcwd(), 'tests/data/demo_binds.yml')
     self.testcases = FileUtils.load_file(testcase_file_path)
Esempio n. 15
0
 def setUp(self):
     self.context = Context()
     testcase_file_path = os.path.join(os.getcwd(), 'tests/data/demo_binds.yml')
     self.testcases = FileUtils.load_file(testcase_file_path)
Esempio n. 16
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
Esempio n. 17
0
 def test_load_testcases_bad_filepath(self):
     testcase_file_path = os.path.join(os.getcwd(), 'tests/data/demo')
     with self.assertRaises(exception.FileNotFoundError):
         FileUtils.load_file(testcase_file_path)