Beispiel #1
0
 def test_validate_exception(self):
     testcases = [{
         "config": {
             'name': "test validation"
         },
         "teststeps": [{
             "name":
             "test validation",
             "request": {
                 "url": "http://127.0.0.1:5000/",
                 "method": "GET",
             },
             "variables": {
                 "resp_status_code": 200,
                 "resp_body_success": True
             },
             "validate": [{
                 "eq": ["$resp_status_code", 201]
             }, {
                 "check": "$resp_status_code",
                 "expect": 201
             }, {
                 "check": "$resp_body_success",
                 "comparator": "eq",
                 "expect": True
             }]
         }]
     }]
     tests_mapping = {"testcases": testcases}
     testcases = parser.parse_tests(tests_mapping)
     parsed_testcase = testcases[0]
     test_runner = runner.Runner(parsed_testcase["config"])
     teststep = parsed_testcase["teststeps"][0]
     with self.assertRaises(exceptions.ValidationFailure):
         test_runner.run_test(teststep)
Beispiel #2
0
    def test_run_testcase_with_hooks(self):
        start_time = time.time()

        testcases = [
            {
                "config": {
                    "name": "basic test with httpbin",
                    "base_url": HTTPBIN_SERVER,
                    "setup_hooks": [
                        "${sleep_N_secs(0.5)}",
                        "${hook_print(setup)}"
                    ],
                    "teardown_hooks": [
                        "${sleep_N_secs(1)}",
                        "${hook_print(teardown)}"
                    ]
                },
                "teststeps": [
                    {
                        "name": "get token",
                        "request": {
                            "url": "http://127.0.0.1:5000/api/get-token",
                            "method": "POST",
                            "headers": {
                                "content-type": "application/json",
                                "user_agent": "iOS/10.3",
                                "device_sn": "HZfFBh6tU59EdXJ",
                                "os_platform": "ios",
                                "app_version": "2.8.6"
                            },
                            "json": {
                                "sign": "5188962c489d1a35effa99e9346dd5efd4fdabad"
                            }
                        },
                        "validate": [
                            {"check": "status_code", "expect": 200}
                        ]
                    }
                ]
            }
        ]
        tests_mapping = {
            "project_mapping": {
                "functions": self.debugtalk_functions
            },
            "testcases": testcases
        }
        parsed_testcases = parser.parse_tests(tests_mapping)
        parsed_testcase = parsed_testcases[0]
        test_runner = runner.Runner(parsed_testcase["config"])
        end_time = time.time()
        # check if testcase setup hook executed
        self.assertGreater(end_time - start_time, 0.5)

        start_time = time.time()
        test_runner.run_test(parsed_testcase["teststeps"][0])
        end_time = time.time()
        # testcase teardown hook has not been executed now
        self.assertLess(end_time - start_time, 1)
Beispiel #3
0
 def test_bugfix_type_match(self):
     testcase_file_path = os.path.join(
         os.getcwd(), 'tests/data/bugfix_type_match.yml')
     tests_mapping = loader.load_tests(testcase_file_path)
     parsed_testcases = parser.parse_tests(tests_mapping)
     parsed_testcase = parsed_testcases[0]
     test_runner = runner.Runner(parsed_testcase["config"])
     test_runner.run_test(parsed_testcase["teststeps"][0])
Beispiel #4
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.reset_all()
Beispiel #5
0
    def test_run_single_testcase(self):
        testcase_file_path_list = [
            os.path.join(
                os.getcwd(), 'tests/data/demo_testcase_hardcode.yml'),
            os.path.join(
                os.getcwd(), 'tests/data/demo_testcase_hardcode.json')
        ]

        for testcase_file_path in testcase_file_path_list:
            tests_mapping = loader.load_tests(testcase_file_path)
            parsed_testcases = parser.parse_tests(tests_mapping)
            parsed_testcase = parsed_testcases[0]
            test_runner = runner.Runner(parsed_testcase["config"])
            test_runner.run_test(parsed_testcase["teststeps"][0])
            test_runner.run_test(parsed_testcase["teststeps"][1])
            test_runner.run_test(parsed_testcase["teststeps"][2])
Beispiel #6
0
    def test_run_testcase_with_teardown_hooks_fail(self):
        testcases = [
            {
                "config": {
                    "name": "basic test with httpbin"
                },
                "teststeps": [
                    {
                        "name": "get token",
                        "request": {
                            "url": "http://127.0.0.1:5000/api/get-token2",
                            "method": "POST",
                            "headers": {
                                "content-type": "application/json",
                                "user_agent": "iOS/10.3",
                                "device_sn": "HZfFBh6tU59EdXJ",
                                "os_platform": "ios",
                                "app_version": "2.8.6"
                            },
                            "json": {
                                "sign": "5188962c489d1a35effa99e9346dd5efd4fdabad"
                            }
                        },
                        "validate": [
                            {"check": "status_code", "expect": 404}
                        ],
                        "teardown_hooks": ["${teardown_hook_sleep_N_secs($response, 2)}"]
                    }
                ]
            }
        ]
        tests_mapping = {
            "project_mapping": {
                "functions": self.debugtalk_functions
            },
            "testcases": testcases
        }
        parsed_testcases = parser.parse_tests(tests_mapping)
        parsed_testcase = parsed_testcases[0]
        test_runner = runner.Runner(parsed_testcase["config"])

        start_time = time.time()
        test_runner.run_test(parsed_testcase["teststeps"][0])
        end_time = time.time()
        # check if teardown function executed
        self.assertGreater(end_time - start_time, 2)
Beispiel #7
0
 def test_validate(self):
     testcases = [{
         "config": {
             'name': "test validation"
         },
         "teststeps": [{
             "name":
             "test validation",
             "request": {
                 "url": "http://127.0.0.1:5000/",
                 "method": "GET",
             },
             "variables": {
                 "resp_status_code": 200,
                 "resp_body_success": True
             },
             "validate": [{
                 "eq": ["$resp_status_code", 200]
             }, {
                 "check": "$resp_status_code",
                 "comparator": "eq",
                 "expect": 200
             }, {
                 "check": "$resp_body_success",
                 "expect": True
             }, {
                 "check": "${is_status_code_200($resp_status_code)}",
                 "expect": True
             }]
         }]
     }]
     from tests.debugtalk import is_status_code_200
     tests_mapping = {
         "project_mapping": {
             "functions": {
                 "is_status_code_200": is_status_code_200
             }
         },
         "testcases": testcases
     }
     testcases = parser.parse_tests(tests_mapping)
     parsed_testcase = testcases[0]
     test_runner = runner.Runner(parsed_testcase["config"])
     teststep = parsed_testcase["teststeps"][0]
     test_runner.run_test(teststep)
Beispiel #8
0
 def test_run_testcase_with_hooks_modify_request(self):
     testcases = [
         {
             "config": {
                 "name": "basic test with httpbin",
                 "base_url": HTTPBIN_SERVER
             },
             "teststeps": [
                 {
                     "name": "modify request headers",
                     "base_url": HTTPBIN_SERVER,
                     "request": {
                         "url": "/anything",
                         "method": "POST",
                         "headers": {
                             "content-type": "application/json",
                             "user_agent": "iOS/10.3"
                         },
                         "json": {
                             "os_platform": "ios",
                             "sign": "5188962c489d1a35effa99e9346dd5efd4fdabad"
                         }
                     },
                     "setup_hooks": [
                         "${modify_request_json($request, android)}"
                     ],
                     "validate": [
                         {"check": "status_code", "expect": 200},
                         {"check": "content.json.os_platform", "expect": "android"}
                     ]
                 }
             ]
         }
     ]
     tests_mapping = {
         "project_mapping": {
             "functions": self.debugtalk_functions
         },
         "testcases": testcases
     }
     parsed_testcases = parser.parse_tests(tests_mapping)
     parsed_testcase = parsed_testcases[0]
     test_runner = runner.Runner(parsed_testcase["config"])
     test_runner.run_test(parsed_testcase["teststeps"][0])
Beispiel #9
0
 def test_run_testcase_with_hooks_assignment(self):
     testcases = [
         {
             "config": {
                 "name": "basic test with httpbin",
                 "base_url": HTTPBIN_SERVER
             },
             "teststeps": [
                 {
                     "name": "modify request headers",
                     "base_url": HTTPBIN_SERVER,
                     "request": {
                         "url": "/anything",
                         "method": "POST",
                         "headers": {
                             "user_agent": "iOS/10.3",
                             "os_platform": "ios"
                         },
                         "data": "a=1&b=2"
                     },
                     "setup_hooks": [
                         {"total": "${sum_two(1, 5)}"}
                     ],
                     "validate": [
                         {"check": "status_code", "expect": 200}
                     ]
                 }
             ]
         }
     ]
     tests_mapping = {
         "project_mapping": {
             "functions": self.debugtalk_functions
         },
         "testcases": testcases
     }
     parsed_testcases = parser.parse_tests(tests_mapping)
     parsed_testcase = parsed_testcases[0]
     test_runner = runner.Runner(parsed_testcase["config"])
     test_runner.run_test(parsed_testcase["teststeps"][0])
     test_variables_mapping = test_runner.session_context.test_variables_mapping
     self.assertEqual(test_variables_mapping["total"], 6)
     self.assertEqual(test_variables_mapping["request"]["data"], "a=1&b=2")
Beispiel #10
0
 def test_run_validate_elapsed(self):
     testcases = [
         {
             "config": {},
             "teststeps": [
                 {
                     "name": "get token",
                     "request": {
                         "url": "http://127.0.0.1:5000/api/get-token",
                         "method": "POST",
                         "headers": {
                             "content-type": "application/json",
                             "user_agent": "iOS/10.3",
                             "device_sn": "HZfFBh6tU59EdXJ",
                             "os_platform": "ios",
                             "app_version": "2.8.6"
                         },
                         "json": {
                             "sign": "5188962c489d1a35effa99e9346dd5efd4fdabad"
                         }
                     },
                     "validate": [
                         {"check": "status_code", "expect": 200},
                         {"check": "elapsed.seconds", "comparator": "lt", "expect": 1},
                         {"check": "elapsed.days", "comparator": "eq", "expect": 0},
                         {"check": "elapsed.microseconds", "comparator": "gt", "expect": 1000},
                         {"check": "elapsed.total_seconds", "comparator": "lt", "expect": 1}
                     ]
                 }
             ]
         }
     ]
     tests_mapping = {
         "project_mapping": {
             "functions": self.debugtalk_functions
         },
         "testcases": testcases
     }
     parsed_testcases = parser.parse_tests(tests_mapping)
     parsed_testcase = parsed_testcases[0]
     test_runner = runner.Runner(parsed_testcase["config"])
     test_runner.run_test(parsed_testcase["teststeps"][0])
Beispiel #11
0
    def _add_tests(self, testcases):
        """ initialize testcase with Runner() and add to test suite.

        Args:
            testcases (list): testcases list.

        Returns:
            unittest.TestSuite()

        """
        def _add_test(test_runner, test_dict):
            """ add test to testcase.
            """
            def test(self):
                try:
                    test_runner.run_test(test_dict)
                except exceptions.MyBaseFailure as ex:
                    self.fail(str(ex))
                finally:
                    self.meta_datas = test_runner.meta_datas

            if "config" in test_dict:
                # run nested testcase
                test.__doc__ = test_dict["config"].get("name")
                variables = test_dict["config"].get("variables", {})
            else:
                # run api test
                test.__doc__ = test_dict.get("name")
                variables = test_dict.get("variables", {})

            if isinstance(test.__doc__, parser.LazyString):
                parsed_variables = parser.parse_variables_mapping(variables,
                                                                  ignore=True)
                test.__doc__ = parser.parse_lazy_data(test.__doc__,
                                                      parsed_variables)

            return test

        test_suite = unittest.TestSuite()
        for testcase in testcases:
            config = testcase.get("config", {})
            test_runner = runner.Runner(config)
            TestSequense = type('TestSequense', (unittest.TestCase, ), {})

            tests = testcase.get("teststeps", [])
            for index, test_dict in enumerate(tests):
                for times_index in range(int(test_dict.get("times", 1))):
                    # suppose one testcase should not have more than 9999 steps,
                    # and one step should not run more than 999 times.
                    test_method_name = 'test_{:04}_{:03}'.format(
                        index, times_index)
                    test_method = _add_test(test_runner, test_dict)
                    setattr(TestSequense, test_method_name, test_method)

            loaded_testcase = self.test_loader.loadTestsFromTestCase(
                TestSequense)
            setattr(loaded_testcase, "config", config)
            setattr(loaded_testcase, "teststeps", tests)
            setattr(loaded_testcase, "runner", test_runner)
            test_suite.addTest(loaded_testcase)

        return test_suite