Пример #1
0
 def test_parse_testcase_template_miss_bind_variable(self):
     testcase = {
         "request": {
             "url": "http://127.0.0.1:5000/api/users/${uid}",
             "method": "${method}"
         }
     }
     with self.assertRaises(exception.ParamsError):
         parse_template(testcase, self.variables_binds)
Пример #2
0
    def get_parsed_request(self):
        """ get parsed request, with each variable replaced by bind value.
        """
        parsed_request = testcase.parse_template(
            self.testcase_request_config, self.testcase_variables_mapping)

        return parsed_request
Пример #3
0
    def run_test(self, testcase):
        """ run single testcase.
        @param (dict) testcase
            {
                "name": "testcase description",
                "requires": [],  # optional, override
                "function_binds": {}, # optional, override
                "variable_binds": {}, # optional, override
                "request": {
                    "url": "http://127.0.0.1:5000/api/users/1000",
                    "method": "POST",
                    "headers": {
                        "Content-Type": "application/json",
                        "authorization": "${authorization}",
                        "random": "${random}"
                    },
                    "body": '{"name": "user", "password": "******"}'
                },
                "extract_binds": {},
                "validators": []
            }
        @return (tuple) test result of single testcase
            (success, diff_content_list)
        """
        self.update_context(testcase)

        # each testcase shall inherit from testset request configs,
        # but can not override testset configs,
        # that's why we use copy.deepcopy here.
        testcase_request = copy.deepcopy(self.testset_req_overall_configs)
        testcase_request.update(testcase["request"])

        parsed_request = parse_template(testcase_request,
                                        self.context.variables)
        try:
            url = parsed_request.pop('url')
            method = parsed_request.pop('method')
        except KeyError:
            raise exception.ParamsError("URL or METHOD missed!")

        resp = self.client.request(url=url, method=method, **parsed_request)
        resp_obj = response.ResponseObject(resp)

        extract_binds = testcase.get("extract_binds", {})
        extracted_variables_mapping = resp_obj.extract_response(extract_binds)
        self.context.update_variables(extracted_variables_mapping)

        validators = testcase.get("validators", [])
        diff_content_list = resp_obj.validate(validators,
                                              self.context.variables)

        return resp_obj.success, diff_content_list
Пример #4
0
    def get_parsed_request(self):
        """ get parsed request, with each variable replaced by bind value.
            testcase request shall inherit from testset request configs,
            but can not change testset configs, that's why we use copy.deepcopy here.
        """
        testcase_request_config = utils.deep_update_dict(
            copy.deepcopy(self.testset_config["request"]),
            self.testcase_config["request"])

        parsed_request = testcase.parse_template(
            testcase_request_config, self._get_evaluated_testcase_variables())

        return parsed_request
Пример #5
0
    def test_parse_testcase_with_new_variable_binds(self):
        testcase = {
            "request": {
                "url": "http://127.0.0.1:5000/api/users/$uid",
                "method": "$method"
            }
        }
        new_variable_binds = {"method": "GET"}
        self.variables_binds.update(new_variable_binds)
        parsed_testcase = parse_template(testcase, self.variables_binds)

        self.assertEqual(parsed_testcase["request"]["method"],
                         new_variable_binds["method"])
Пример #6
0
    def test_parse_testcase_template(self):
        testcase = {
            "request": {
                "url": "http://127.0.0.1:5000/api/users/${uid}",
                "method": "POST",
                "headers": {
                    "Content-Type": "application/json",
                    "authorization": "${authorization}",
                    "random": "${random}"
                },
                "body": "${json}"
            },
            "response": {
                "status_code": "${expected_status}",
                "headers": {
                    "Content-Type": "application/json"
                },
                "body": {
                    "success": "${expected_success}",
                    "msg": "user created successfully."
                }
            }
        }
        parsed_testcase = parse_template(testcase, self.variables_binds)

        self.assertEqual(
            parsed_testcase["request"]["url"],
            "http://127.0.0.1:5000/api/users/%s" % self.variables_binds["uid"]
        )
        self.assertEqual(
            parsed_testcase["request"]["headers"]["authorization"],
            self.variables_binds["authorization"]
        )
        self.assertEqual(
            parsed_testcase["request"]["headers"]["random"],
            self.variables_binds["random"]
        )
        self.assertEqual(
            parsed_testcase["request"]["body"],
            self.variables_binds["json"]
        )
        self.assertEqual(
            parsed_testcase["response"]["status_code"],
            self.variables_binds["expected_status"]
        )
        self.assertEqual(
            parsed_testcase["response"]["body"]["success"],
            self.variables_binds["expected_success"]
        )