Ejemplo n.º 1
0
 def setUp(self):
     loader.load_project_tests(os.path.join(os.getcwd(), "tests"))
     project_mapping = loader.project_mapping
     self.context = context.SessionContext(
         functions=project_mapping["functions"],
         variables={"SECRET_KEY": "DebugTalk"}
     )
Ejemplo n.º 2
0
    def test_extend_with_api(self):
        loader.load_project_tests(os.path.join(os.getcwd(), "tests"))
        raw_testinfo = {
            "name": "get token",
            "base_url": "https://debugtalk.com",
            "api": "api/get_token.yml",
        }
        api_def_dict = loader.load_teststep(raw_testinfo)
        test_block = {
            "name": "override block",
            "times": 3,
            "variables": [
                {"var": 123}
            ],
            "base_url": "https://httprunner.org",
            'request': {
                'url': '/api/get-token',
                'method': 'POST',
                'headers': {'user_agent': '$user_agent', 'device_sn': '$device_sn', 'os_platform': '$os_platform', 'app_version': '$app_version'},
                'json': {'sign': '${get_sign($user_agent, $device_sn, $os_platform, $app_version)}'}
            },
            'validate': [
                {'eq': ['status_code', 201]},
                {'len_eq': ['content.token', 32]}
            ]
        }

        extended_block = parser._extend_with_api(test_block, api_def_dict)
        self.assertEqual(extended_block["base_url"], "https://debugtalk.com")
        self.assertEqual(extended_block["name"], "override block")
        self.assertEqual({'var': 123}, extended_block["variables"])
        self.assertIn({'check': 'status_code', 'expect': 201, 'comparator': 'eq'}, extended_block["validate"])
        self.assertIn({'check': 'content.token', 'comparator': 'len_eq', 'expect': 32}, extended_block["validate"])
        self.assertEqual(extended_block["times"], 3)
Ejemplo n.º 3
0
 def test_run_httprunner_with_teardown_hooks_error(self):
     testcases = [
         {
             "config": {
                 "name": "test teardown hooks"
             },
             "teststeps": [
                 {
                     "name": "test teardown hooks",
                     "request": {
                         "url": "{}/headers".format(HTTPBIN_SERVER),
                         "method": "GET",
                         "data": "abc"
                     },
                     "teardown_hooks": [
                         "${alter_response_error($response)}"
                     ]
                 }
             ]
         }
     ]
     loader.load_project_tests("tests")
     tests_mapping = {
         "project_mapping": loader.project_mapping,
         "testcases": testcases
     }
     self.runner.run_tests(tests_mapping)
     summary = self.runner.summary
     self.assertFalse(summary["success"])
     self.assertEqual(summary["stat"]["teststeps"]["errors"], 1)
Ejemplo n.º 4
0
 def test_load_project_tests(self):
     loader.load_project_tests(os.path.join(os.getcwd(), "tests"))
     api_file_path = os.path.join(os.getcwd(), "tests", "api",
                                  "get_token.yml")
     self.assertIn(api_file_path, self.tests_def_mapping["api"])
     self.assertEqual(self.project_mapping["env"]["PROJECT_KEY"],
                      "ABCDEFGH")
Ejemplo n.º 5
0
 def test_load_project_tests(self):
     loader.load_project_tests(os.path.join(os.getcwd(), "tests"))
     project_mapping = loader.project_mapping
     self.assertEqual(
         project_mapping["debugtalk"]["variables"]["SECRET_KEY"],
         "DebugTalk")
     self.assertIn("get_token", project_mapping["def-api"])
     self.assertIn("setup_and_reset", project_mapping["def-testcase"])
     self.assertEqual(project_mapping["env"]["PROJECT_KEY"], "ABCDEFGH")
Ejemplo n.º 6
0
    def setup_method(self):
        loader.load_project_tests(os.path.join(os.getcwd(), 'tests'))
        self.confcustom_module = loader.project_mapping['confcustom']

        self.context = context.Context(self.confcustom_module['variables'],
                                       self.confcustom_module['functions'])
        testcase_file_path = os.path.join(os.getcwd(),
                                          'tests/data/demo_binds.yml')
        self.testcases = loader.load_file(testcase_file_path)
Ejemplo n.º 7
0
    def setUp(self):
        loader.load_project_tests(os.path.join(os.getcwd(), "tests"))
        self.debugtalk_module = loader.project_mapping["debugtalk"]

        self.context = context.Context(self.debugtalk_module["variables"],
                                       self.debugtalk_module["functions"])
        testcase_file_path = os.path.join(os.getcwd(),
                                          'tests/data/demo_binds.yml')
        self.testcases = loader.load_file(testcase_file_path)
Ejemplo n.º 8
0
 def setUp(self):
     loader.load_project_tests(os.path.join(os.getcwd(), "tests"))
     self.debugtalk_module = loader.project_mapping["debugtalk"]
     config_dict = {
         "variables": self.debugtalk_module["variables"],
         "functions": self.debugtalk_module["functions"]
     }
     self.test_runner = runner.Runner(config_dict)
     self.reset_all()
Ejemplo n.º 9
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)
Ejemplo n.º 10
0
 def test_load_project_tests(self):
     loader.load_project_tests(os.path.join(os.getcwd(), 'tests'))
     project_mapping = loader.project_mapping
     assert project_mapping['confcustom']['variables'][
         'SECRET_KEY'] == 'MyHttpRunner'
     assert 'get_token' in project_mapping['def-api']
     assert 'setup_and_reset' in project_mapping['def-testcase']
     if not os.path.exists(os.path.join(os.getcwd(), 'tests', '.env')):
         pytest.skip('.env not exists')
     assert project_mapping['env']['PROJECT_KEY'] == 'ABCDEFGH'
Ejemplo n.º 11
0
    def setUp(self):
        loader.load_project_tests(os.path.join(os.getcwd(), "tests"))
        project_mapping = loader.project_mapping
        self.debugtalk_functions = project_mapping["functions"]

        config = {
            "name": "XXX",
            "base_url": "http://127.0.0.1",
            "verify": False
        }
        self.test_runner = runner.Runner(config, self.debugtalk_functions)
        self.reset_all()
Ejemplo n.º 12
0
    def test_load_debugtalk_module(self):
        loader.load_project_tests(os.path.join(os.getcwd(), "httprunner"))
        project_mapping = loader.project_mapping
        self.assertNotIn("alter_response", project_mapping["functions"])

        loader.load_project_tests(os.path.join(os.getcwd(), "tests"))
        project_mapping = loader.project_mapping
        self.assertIn("alter_response", project_mapping["functions"])

        is_status_code_200 = project_mapping["functions"]["is_status_code_200"]
        self.assertTrue(is_status_code_200(200))
        self.assertFalse(is_status_code_200(500))
Ejemplo n.º 13
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)
Ejemplo n.º 14
0
 def test_load_project_tests(self):
     project_dir = os.path.join(os.getcwd(), "tests")
     project_tests = loader.load_project_tests(project_dir)
     self.assertEqual(project_tests["debugtalk"]["variables"]["SECRET_KEY"],
                      "DebugTalk")
     self.assertIn("get_token", project_tests["def-api"])
     self.assertIn("setup_and_reset", project_tests["def-testcase"])
Ejemplo n.º 15
0
    def test_load_confcustom_module(self):
        loader.load_project_tests(os.path.join(os.getcwd(), 'httprunner'))
        imported_module_items = loader.project_mapping['confcustom']
        assert 'equals' in imported_module_items['functions']
        assert 'SECRET_KEY' not in imported_module_items['variables']
        assert 'alter_response' not in imported_module_items['functions']

        loader.load_project_tests(os.path.join(os.getcwd(), 'tests'))
        imported_module_items = loader.project_mapping['confcustom']
        assert imported_module_items['variables'][
            'SECRET_KEY'] == 'MyHttpRunner'
        assert 'hook_print' in imported_module_items['functions']
        is_status_code_200 = imported_module_items['functions'][
            'is_status_code_200']
        assert is_status_code_200(200)
        assert not is_status_code_200(300)
Ejemplo n.º 16
0
    def test_load_debugtalk_module(self):
        loader.load_project_tests(os.path.join(os.getcwd(), "httprunner"))
        imported_module_items = loader.project_mapping["debugtalk"]
        self.assertIn("equals", imported_module_items["functions"])
        self.assertNotIn("SECRET_KEY", imported_module_items["variables"])
        self.assertNotIn("alter_response", imported_module_items["functions"])

        loader.load_project_tests(os.path.join(os.getcwd(), "tests"))
        imported_module_items = loader.project_mapping["debugtalk"]
        self.assertEqual(imported_module_items["variables"]["SECRET_KEY"],
                         "DebugTalk")
        self.assertIn("alter_response", imported_module_items["functions"])

        is_status_code_200 = imported_module_items["functions"][
            "is_status_code_200"]
        self.assertTrue(is_status_code_200(200))
        self.assertFalse(is_status_code_200(500))
Ejemplo n.º 17
0
 def test_run_httprunner_with_teardown_hooks_alter_response(self):
     testcases = [{
         "config": {
             "name": "test teardown hooks"
         },
         "teststeps": [{
             "name":
             "test teardown hooks",
             "request": {
                 "url": "{}/headers".format(HTTPBIN_SERVER),
                 "method": "GET",
                 "data": "abc"
             },
             "teardown_hooks": ["${alter_response($response)}"],
             "validate": [{
                 "eq": ["status_code", 500]
             }, {
                 "eq": ["headers.content-type", "html/text"]
             }, {
                 "eq": ["json.headers.Host", "127.0.0.1:8888"]
             }, {
                 "eq": ["content.headers.Host", "127.0.0.1:8888"]
             }, {
                 "eq": ["text.headers.Host", "127.0.0.1:8888"]
             }, {
                 "eq": ["new_attribute", "new_attribute_value"]
             }, {
                 "eq": ["new_attribute_dict", {
                     "key": 123
                 }]
             }, {
                 "eq": ["new_attribute_dict.key", 123]
             }]
         }]
     }]
     loader.load_project_tests("tests")
     tests_mapping = {
         "project_mapping": loader.project_mapping,
         "testcases": testcases
     }
     self.runner.run_tests(tests_mapping)
     summary = self.runner.summary
     self.assertTrue(summary["success"])
Ejemplo n.º 18
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)
Ejemplo n.º 19
0
    def test_load_debugtalk_py(self):
        loader.load_project_tests("tests/data/demo_testcase.yml")
        project_working_directory = loader.project_mapping["PWD"]
        debugtalk_functions = loader.project_mapping["functions"]
        self.assertEqual(
            project_working_directory,
            os.path.join(os.getcwd(), "tests")
        )
        self.assertIn("gen_md5", debugtalk_functions)

        loader.load_project_tests("tests/base.py")
        project_working_directory = loader.project_mapping["PWD"]
        debugtalk_functions = loader.project_mapping["functions"]
        self.assertEqual(
            project_working_directory,
            os.path.join(os.getcwd(), "tests")
        )
        self.assertIn("gen_md5", debugtalk_functions)

        loader.load_project_tests("httprunner/__init__.py")
        project_working_directory = loader.project_mapping["PWD"]
        debugtalk_functions = loader.project_mapping["functions"]
        self.assertEqual(
            project_working_directory,
            os.getcwd()
        )
        self.assertEqual(debugtalk_functions, {})
Ejemplo n.º 20
0
    def __init__(self, **kwargs):
        """ initialize HttpRunner.

        Args:
            kwargs (dict): key-value arguments used to initialize TextTestRunner.
            Commonly used arguments:

            resultclass (class): HtmlTestResult or TextTestResult
            failfast (bool): False/True, stop the test run on the first error or failure.
            dot_env_path (str): .env file path.

        Attributes:
            project_mapping (dict): save project loaded api/testcases, environments and debugtalk.py module.

        """
        dot_env_path = kwargs.pop("dot_env_path", None)
        loader.load_dot_env_file(dot_env_path)
        loader.load_project_tests("tests")  # TODO: remove tests
        self.project_mapping = loader.project_mapping
        utils.set_os_environ(self.project_mapping["env"])

        kwargs.setdefault("resultclass", HtmlTestResult)
        self.runner = unittest.TextTestRunner(**kwargs)
Ejemplo n.º 21
0
 def setUpClass(cls):
     cls.project_mapping = loader.load_project_tests(
         os.path.join(os.getcwd(), "tests"))
Ejemplo n.º 22
0
 def setup_class(cls):
     loader.load_project_tests(os.path.join(os.getcwd(), 'tests'))
Ejemplo n.º 23
0
 def setUpClass(cls):
     project_dir = os.path.join(os.getcwd(), "tests")
     loader.load_project_tests(project_dir)
Ejemplo n.º 24
0
    def load_tests(self, path_or_testcases):
        """ load testcases, extend and merge with api/testcase definitions.

        Args:
            path_or_testcases (str/dict/list): YAML/JSON testcase file path or testcase list
                path (str): testcase file/folder path
                testcases (dict/list): testcase dict or list of testcases

        Returns:
            list: valid testcases list.

                [
                    # testcase data structure
                    {
                        "config": {
                            "name": "desc1",
                            "path": "",         # optional
                            "variables": [],    # optional
                            "request": {}       # optional
                        },
                        "teststeps": [
                            # teststep data structure
                            {
                                'name': 'test step desc2',
                                'variables': [],    # optional
                                'extract': [],      # optional
                                'validate': [],
                                'request': {},
                                'function_meta': {}
                            },
                            teststep2   # another teststep dict
                        ]
                    },
                    {}  # another testcase dict
                ]

        """
        if validator.is_testcases(path_or_testcases):
            # TODO: refactor
            if isinstance(path_or_testcases, list):
                for testcase in path_or_testcases:
                    try:
                        test_path = os.path.dirname(testcase["config"]["path"])
                    except KeyError:
                        test_path = os.getcwd()
                    loader.load_project_tests(test_path)
            else:
                try:
                    test_path = os.path.dirname(
                        path_or_testcases["config"]["path"])
                except KeyError:
                    test_path = os.getcwd()
                loader.load_project_tests(test_path)

            testcases = path_or_testcases
        else:
            testcases = loader.load_testcases(path_or_testcases)

        self.project_mapping = loader.project_mapping

        if not testcases:
            raise exceptions.TestcaseNotFound

        if isinstance(testcases, dict):
            testcases = [testcases]

        return testcases