Esempio n. 1
0
 def test_parse_parameters_custom_function(self):
     parameters = [
         {"user_agent": "${get_user_agent()}"},
         {"app_version": "${gen_app_version()}"},
         {"username-password": "******"},
         {"username2-password2": "${get_account_in_tuple()}"}
     ]
     dot_env_path = os.path.join(
         os.getcwd(), "tests", ".env"
     )
     loader.load_dot_env_file(dot_env_path)
     from tests import debugtalk
     cartesian_product_parameters = parser.parse_parameters(
         parameters,
         functions_mapping=loader.load_module_functions(debugtalk)
     )
     self.assertIn(
         {
             'user_agent': 'iOS/10.1',
             'app_version': '2.8.5',
             'username': '******',
             'password': '******',
             'username2': 'user1',
             'password2': '111111'
         },
         cartesian_product_parameters
     )
     self.assertEqual(
         len(cartesian_product_parameters),
         2 * 2 * 2 * 2
     )
Esempio n. 2
0
    def test_parse_parameters_testcase(self):
        variables = {
            "user_agent": "chrome",
            "sum": 5,
        }
        param = [
            {
                "user_agent": ["iOS/10.1", "iOS/10.2"],
                "username-password": "******",
                "sum": "${add_two_nums(1, 2)}",
            }
        ]

        functions = {
            "add_two_nums": lambda a, b=1: [a + b, b-a],
        }

        parsed_params = parser.parse_parameters(param, variables, functions)
        self.assertIn({'username': '******', 'password': '******', 'user_agent': 'iOS/10.1', 'sum': 3}, parsed_params)
        self.assertIn({'username': '******', 'password': '******', 'user_agent': 'iOS/10.1', 'sum': 1}, parsed_params)
        self.assertIn({'username': '******', 'password': '******', 'user_agent': 'iOS/10.2', 'sum': 3}, parsed_params)
        self.assertIn({'username': '******', 'password': '******', 'user_agent': 'iOS/10.2', 'sum': 1}, parsed_params)
        self.assertIn({'username': '******', 'password': '******', 'user_agent': 'iOS/10.1', 'sum': 3}, parsed_params)
        self.assertIn({'username': '******', 'password': '******', 'user_agent': 'iOS/10.1', 'sum': 1}, parsed_params)
        self.assertIn({'username': '******', 'password': '******', 'user_agent': 'iOS/10.2', 'sum': 3}, parsed_params)
        self.assertIn({'username': '******', 'password': '******', 'user_agent': 'iOS/10.2', 'sum': 1}, parsed_params)
Esempio n. 3
0
 def test_parse_parameters_parameterize(self):
     loader.load_project_tests(os.path.join(os.getcwd(), "tests"))
     parameters = [{
         "app_version": "${parameterize(data/app_version.csv)}"
     }, {
         "username-password": "******"
     }]
     cartesian_product_parameters = parser.parse_parameters(parameters)
     self.assertEqual(len(cartesian_product_parameters), 2 * 3)
Esempio n. 4
0
 def test_parse_parameters_parameterize(self):
     parameters = [
         {"app_version": "${parameterize(tests/data/app_version.csv)}"},
         {"username-password": "******"}
     ]
     cartesian_product_parameters = parser.parse_parameters(parameters)
     self.assertEqual(
         len(cartesian_product_parameters),
         2 * 3
     )
Esempio n. 5
0
 def test_parse_parameters_raw_list(self):
     parameters = [{
         "user_agent": ["iOS/10.1", "iOS/10.2", "iOS/10.3"]
     }, {
         "username-password": [("user1", "111111"), ["test2", "222222"]]
     }]
     cartesian_product_parameters = parser.parse_parameters(parameters)
     self.assertEqual(len(cartesian_product_parameters), 3 * 2)
     self.assertEqual(cartesian_product_parameters[0], {
         'user_agent': 'iOS/10.1',
         'username': '******',
         'password': '******'
     })
Esempio n. 6
0
    def test_parse_parameters_mix(self):
        loader.load_project_tests(os.path.join(os.getcwd(), "tests"))
        project_mapping = loader.project_mapping

        parameters = [{
            "user_agent": ["iOS/10.1", "iOS/10.2", "iOS/10.3"]
        }, {
            "app_version": "${gen_app_version()}"
        }, {
            "username-password": "******"
        }]
        cartesian_product_parameters = parser.parse_parameters(
            parameters, functions_mapping=project_mapping["functions"])
        self.assertEqual(len(cartesian_product_parameters), 3 * 2 * 3)
Esempio n. 7
0
    def test_parse_parameters_parameterize(self):
        parameters = [
            {"app_version": "${parameterize(tests/data/app_version.csv)}"},
            {"username-password": "******"}
        ]
        variables_mapping = {}
        functions_mapping = {}

        cartesian_product_parameters = parser.parse_parameters(
            parameters, variables_mapping, functions_mapping)
        self.assertEqual(
            len(cartesian_product_parameters),
            2 * 3
        )
Esempio n. 8
0
 def test_parse_parameters_custom_function(self):
     parameters = [{
         "app_version": "${gen_app_version()}"
     }, {
         "username-password": "******"
     }]
     testset_path = os.path.join(os.getcwd(),
                                 "tests/data/demo_parameters.yml")
     from tests import debugtalk
     debugtalk_module = loader.load_python_module(debugtalk)
     cartesian_product_parameters = parser.parse_parameters(
         parameters, debugtalk_module["variables"],
         debugtalk_module["functions"])
     self.assertEqual(len(cartesian_product_parameters), 2 * 2)
Esempio n. 9
0
    def test_parse_parameters_parameterize(self):
        parameters = [{
            'app_version':
            '${parameterize(tests/data/app_version.csv)}'
        }, {
            'username-password':
            '******'
        }]
        variables_mapping = {}
        functions_mapping = {}

        cartesian_product_parameters = parser.parse_parameters(
            parameters, variables_mapping, functions_mapping)

        print(cartesian_product_parameters)
        assert len(cartesian_product_parameters) == 2 * 3
Esempio n. 10
0
 def test_parse_parameters_raw_list(self):
     parameters = [{
         'user_agent': ['ios/10.1', 'ios/10.2', 'ios/10.3']
     }, {
         'username-password': [('user1', '111111'), ('user2', '222222')]
     }]
     variables_mapping = {}
     functions_mapping = {}
     cartesian_product_parameters = parser.parse_parameters(
         parameters, variables_mapping, functions_mapping)
     assert len(cartesian_product_parameters) == 3 * 2
     assert cartesian_product_parameters[0] == {
         'user_agent': 'ios/10.1',
         'username': '******',
         'password': '******'
     }
Esempio n. 11
0
    def test_parse_parameters_mix(self):
        loader.load_project_tests(os.path.join(os.getcwd(), "tests"))
        project_mapping = loader.project_mapping

        parameters = [{
            "user_agent": ["iOS/10.1", "iOS/10.2", "iOS/10.3"]
        }, {
            "app_version": "${gen_app_version()}"
        }, {
            "username-password":
            "******"
        }]
        variables_mapping = {}
        functions_mapping = project_mapping["debugtalk"]["functions"]
        testset_path = os.path.join(os.getcwd(),
                                    "tests/data/demo_parameters.yml")
        cartesian_product_parameters = parser.parse_parameters(
            parameters, variables_mapping, functions_mapping)
        self.assertEqual(len(cartesian_product_parameters), 3 * 2 * 3)
Esempio n. 12
0
    def parse_tests(self, testcases, variables_mapping=None):
        """ parse testcases configs, including variables/parameters/name/request.

        Args:
            testcases (list): testcase list, with config unparsed.
            variables_mapping (dict): if variables_mapping is specified, it will override variables in config block.

        Returns:
            list: parsed testcases list, with config variables/parameters/name/request parsed.

        """
        self.exception_stage = "parse tests"
        variables_mapping = variables_mapping or {}

        parsed_testcases_list = []
        for testcase in testcases:
            # parse config parameters
            config_parameters = testcase.setdefault("config",
                                                    {}).pop("parameters", [])

            cartesian_product_parameters_list = parser.parse_parameters(
                config_parameters,
                self.project_mapping["debugtalk"]["variables"],
                self.project_mapping["debugtalk"]["functions"]) or [{}]

            for parameter_mapping in cartesian_product_parameters_list:
                testcase_dict = testcase
                config = testcase_dict.setdefault("config", {})

                testcase_dict["config"]["functions"] = {}

                # imported_module = importlib.reload(importlib.import_module('func_list.build_in'))
                # testcase_dict["config"]["functions"].update(loader.load_python_module(imported_module)["functions"])

                if config.get('import_module_functions'):
                    for f in config.get('import_module_functions'):
                        imported_module = importlib.reload(
                            importlib.import_module(f))
                        debugtalk_module = loader.load_python_module(
                            imported_module)
                        testcase_dict["config"]["functions"].update(
                            debugtalk_module["functions"])
                testcase_dict["config"]["functions"].update(
                    self.project_mapping["debugtalk"]["functions"])
                # self.project_mapping["debugtalk"]["functions"].update(debugtalk_module["functions"])
                raw_config_variables = config.get("variables", [])
                parsed_config_variables = parser.parse_data(
                    raw_config_variables,
                    self.project_mapping["debugtalk"]["variables"],
                    testcase_dict["config"]["functions"])

                # priority: passed in > debugtalk.py > parameters > variables
                # override variables mapping with parameters mapping
                config_variables = utils.override_mapping_list(
                    parsed_config_variables, parameter_mapping)
                # merge debugtalk.py module variables
                config_variables.update(
                    self.project_mapping["debugtalk"]["variables"])
                # override variables mapping with passed in variables_mapping
                config_variables = utils.override_mapping_list(
                    config_variables, variables_mapping)

                testcase_dict["config"]["variables"] = config_variables

                # parse config name
                testcase_dict["config"]["name"] = parser.parse_data(
                    testcase_dict["config"].get("name", ""), config_variables,
                    self.project_mapping["debugtalk"]["functions"])

                # parse config request
                testcase_dict["config"]["request"] = parser.parse_data(
                    testcase_dict["config"].get("request",
                                                {}), config_variables,
                    self.project_mapping["debugtalk"]["functions"])
                # put loaded project functions to config
                # testcase_dict["config"]["functions"] = self.project_mapping["debugtalk"]["functions"]
                parsed_testcases_list.append(testcase_dict)
        return parsed_testcases_list
Esempio n. 13
0
    def test_parse_parameters_testcase(self):
        parameters = {
            "user_agent": ["iOS/10.1", "iOS/10.2"],
            "username-password": "******",
            "sum": "${calculate_two_nums(1, 2)}",
        }
        load_project_meta(
            os.path.join(
                os.path.dirname(os.path.dirname(__file__)),
                "examples",
                "postman_echo",
                "request_methods",
            ),
        )
        parsed_params = parser.parse_parameters(parameters)
        self.assertEqual(len(parsed_params), 2 * 3 * 2)

        self.assertIn(
            {
                "username": "******",
                "password": "******",
                "user_agent": "iOS/10.1",
                "sum": 3,
            },
            parsed_params,
        )
        self.assertIn(
            {
                "username": "******",
                "password": "******",
                "user_agent": "iOS/10.1",
                "sum": 1,
            },
            parsed_params,
        )
        self.assertIn(
            {
                "username": "******",
                "password": "******",
                "user_agent": "iOS/10.2",
                "sum": 3,
            },
            parsed_params,
        )
        self.assertIn(
            {
                "username": "******",
                "password": "******",
                "user_agent": "iOS/10.2",
                "sum": 1,
            },
            parsed_params,
        )
        self.assertIn(
            {
                "username": "******",
                "password": "******",
                "user_agent": "iOS/10.1",
                "sum": 3,
            },
            parsed_params,
        )
        self.assertIn(
            {
                "username": "******",
                "password": "******",
                "user_agent": "iOS/10.1",
                "sum": 1,
            },
            parsed_params,
        )
        self.assertIn(
            {
                "username": "******",
                "password": "******",
                "user_agent": "iOS/10.2",
                "sum": 3,
            },
            parsed_params,
        )
        self.assertIn(
            {
                "username": "******",
                "password": "******",
                "user_agent": "iOS/10.2",
                "sum": 1,
            },
            parsed_params,
        )
Esempio n. 14
0
    def parse_tests(self, testcases, variables_mapping=None):
        """ parse testcases configs, including variables/parameters/name/request.

        Args:
            testcases (list): testcase list, with config unparsed.
            variables_mapping (dict): if variables_mapping is specified, it will override variables in config block.

        Returns:
            list: parsed testcases list, with config variables/parameters/name/request parsed.

        """
        variables_mapping = variables_mapping or {}

        parsed_testcases_list = []
        for testcase in testcases:

            config = testcase.setdefault("config", {})

            # parse config parameters
            config_parameters = config.pop("parameters", [])
            cartesian_product_parameters_list = parser.parse_parameters(
                config_parameters,
                self.project_mapping["debugtalk"]["variables"],
                self.project_mapping["debugtalk"]["functions"]) or [{}]

            for parameter_mapping in cartesian_product_parameters_list:
                # parse config variables
                raw_config_variables = config.get("variables", [])
                parsed_config_variables = parser.parse_data(
                    raw_config_variables,
                    self.project_mapping["debugtalk"]["variables"],
                    self.project_mapping["debugtalk"]["functions"])

                # priority: passed in > debugtalk.py > parameters > variables
                # override variables mapping with parameters mapping
                config_variables = utils.override_mapping_list(
                    parsed_config_variables, parameter_mapping)
                # merge debugtalk.py module variables
                config_variables.update(
                    self.project_mapping["debugtalk"]["variables"])
                # override variables mapping with passed in variables_mapping
                config_variables = utils.override_mapping_list(
                    config_variables, variables_mapping)

                testcase["config"]["variables"] = config_variables

                # parse config name
                testcase["config"]["name"] = parser.parse_data(
                    testcase["config"].get("name", ""), config_variables,
                    self.project_mapping["debugtalk"]["functions"])

                # parse config request
                testcase["config"]["request"] = parser.parse_data(
                    testcase["config"].get("request", {}), config_variables,
                    self.project_mapping["debugtalk"]["functions"])

                # put loaded project functions to config
                testcase["config"]["functions"] = self.project_mapping[
                    "debugtalk"]["functions"]
                parsed_testcases_list.append(testcase)

        return parsed_testcases_list