Ejemplo n.º 1
0
    def test_extend_validators_with_dict(self):
        def_validators = [{'eq': ["a", {"v": 1}]}, {'eq': [{"b": 1}, 200]}]
        current_validators = [{'len_eq': ['s3', 12]}, {'eq': [{"b": 1}, 201]}]
        def_validators = [
            validator.uniform_validator(_validator)
            for _validator in def_validators
        ]
        ref_validators = [
            validator.uniform_validator(_validator)
            for _validator in current_validators
        ]

        extended_validators = validator.extend_validators(
            def_validators, ref_validators)
        self.assertEqual(len(extended_validators), 3)
        self.assertIn(
            {
                'check': {
                    'b': 1
                },
                'expect': 201,
                'comparator': 'equals'
            }, extended_validators)
        self.assertNotIn(
            {
                'check': {
                    'b': 1
                },
                'expect': 200,
                'comparator': 'equals'
            }, extended_validators)
Ejemplo n.º 2
0
    def test_parse_validator(self):
        _validator = {
            "check": "status_code",
            "comparator": "eq",
            "expect": 201
        }
        self.assertEqual(validator.uniform_validator(_validator), {
            "check": "status_code",
            "comparator": "equals",
            "expect": 201
        })

        _validator = {'eq': ['status_code', 201]}
        self.assertEqual(validator.uniform_validator(_validator), {
            "check": "status_code",
            "comparator": "equals",
            "expect": 201
        })
Ejemplo n.º 3
0
    def test_extend_validators(self):
        def_validators = [{
            'eq': ['v1', 200]
        }, {
            "check": "s2",
            "expect": 16,
            "comparator": "len_eq"
        }]
        current_validators = [{
            "check": "v1",
            "expect": 201
        }, {
            'len_eq': ['s3', 12]
        }]
        def_validators = [
            validator.uniform_validator(_validator)
            for _validator in def_validators
        ]
        ref_validators = [
            validator.uniform_validator(_validator)
            for _validator in current_validators
        ]

        extended_validators = validator.extend_validators(
            def_validators, ref_validators)
        self.assertIn({
            "check": "v1",
            "expect": 201,
            "comparator": "equals"
        }, extended_validators)
        self.assertIn(
            {
                "check": "s2",
                "expect": 16,
                "comparator": "length_equals"
            }, extended_validators)
        self.assertIn(
            {
                "check": "s3",
                "expect": 12,
                "comparator": "length_equals"
            }, extended_validators)
Ejemplo n.º 4
0
def __prepare_testcase_tests(tests,
                             config,
                             project_mapping,
                             session_variables_set=None):
    """ override tests with testcase config variables, base_url and verify.
        test maybe nested testcase.

        variables priority:
        testcase config > testcase test > testcase_def config > testcase_def test > api

        base_url priority:
        testcase test > testcase config > testsuite test > testsuite config > api

        verify priority:
        testcase teststep (api) > testcase config > testsuite config

    Args:
        tests (list):
        config (dict):
        project_mapping (dict):

    """
    global temp_extracted_set
    config_variables = config.get("variables", {})
    config_base_url = config.get("base_url", "")
    config_verify = config.get("verify", True)
    functions = project_mapping.get("functions", {})

    prepared_testcase_tests = []
    session_variables_set = set(config_variables.keys()) | (
        session_variables_set or set()) | temp_extracted_set
    for test_dict in tests:

        teststep_variables_set = {"request", "response"}

        # 1, testcase config => testcase tests
        # override test_dict variables
        config_variables_tmp = utils.deepcopy_dict(config_variables)
        test_dict_variables = utils.extend_variables(
            config_variables_tmp, test_dict.pop("variables", {}))
        test_dict["variables"] = test_dict_variables

        # base_url & verify: priority test_dict > config
        if (not test_dict.get("base_url")) and config_base_url:
            test_dict["base_url"] = config_base_url

        # unify validators' format
        if "validate" in test_dict:
            ref_raw_validators = test_dict.pop("validate", [])
            test_dict["validate"] = [
                validator.uniform_validator(_validator)
                for _validator in ref_raw_validators
            ]

        if "testcase_def" in test_dict:
            # test_dict is nested testcase

            if "output" in test_dict:
                session_variables_set |= set(test_dict["output"])

            # 2, testcase test_dict => testcase_def config
            testcase_def = test_dict.pop("testcase_def")
            _extend_with_testcase(test_dict, testcase_def)

            # verify priority: nested testcase config > testcase config
            test_dict["config"].setdefault("verify", config_verify)

            # 3, testcase_def config => testcase_def test_dict
            test_dict = _parse_testcase(test_dict, project_mapping,
                                        session_variables_set)

        elif "api_def" in test_dict:
            # test_dict has API reference
            # 2, test_dict => api
            api_def_dict = test_dict.pop("api_def")
            _extend_with_api(test_dict, api_def_dict)

        # current teststep variables
        teststep_variables_set |= set(test_dict.get("variables", {}).keys())

        # verify priority: testcase teststep > testcase config
        if "request" in test_dict and "verify" not in test_dict["request"]:
            test_dict["request"]["verify"] = config_verify

        # move extracted variable to session variables
        if test_dict.get("extract", {}):
            extract_mapping = utils.ensure_mapping_format(test_dict["extract"])
            session_variables_set |= set(extract_mapping.keys())
            temp_extracted_set |= session_variables_set

        session_variables_set |= temp_extracted_set
        teststep_variables_set |= session_variables_set

        # convert validators to lazy function
        validators = test_dict.pop("validate", [])
        prepared_validators = []
        for _validator in validators:
            function_meta = {
                "func_name": _validator["comparator"],
                "args": [_validator["check"], _validator["expect"]],
                "kwargs": {}
            }
            prepared_validators.append(
                LazyFunction(function_meta, functions, teststep_variables_set))
        test_dict["validate"] = prepared_validators

        # convert variables and functions to lazy object.
        # raises VariableNotFound if undefined variable exists in test_dict
        prepared_test_dict = prepare_lazy_data(test_dict, functions,
                                               teststep_variables_set)
        prepared_testcase_tests.append(prepared_test_dict)

    return prepared_testcase_tests
Ejemplo n.º 5
0
def _extend_with_api(test_dict, api_def_dict):
    """ extend test with api definition, test will merge and override api definition.

    Args:
        test_dict (dict): test block, this will override api_def_dict
        api_def_dict (dict): api definition

    Examples:
        >>> api_def_dict = {
                "name": "get token 1",
                "request": {...},
                "validate": [{'eq': ['status_code', 200]}]
            }
        >>> test_dict = {
                "name": "get token 2",
                "extract": {"token": "content.token"},
                "validate": [{'eq': ['status_code', 201]}, {'len_eq': ['content.token', 16]}]
            }
        >>> _extend_with_api(test_dict, api_def_dict)
        >>> print(test_dict)
            {
                "name": "get token 2",
                "request": {...},
                "extract": {"token": "content.token"},
                "validate": [{'eq': ['status_code', 201]}, {'len_eq': ['content.token', 16]}]
            }

    """
    # override api name
    test_dict.setdefault("name", api_def_dict.pop("name",
                                                  "api name undefined"))

    # override variables
    def_variables = api_def_dict.pop("variables", [])
    test_dict["variables"] = utils.extend_variables(
        def_variables, test_dict.get("variables", {}))

    # merge & override validators TODO: relocate
    def_raw_validators = api_def_dict.pop("validate", [])
    def_validators = [
        validator.uniform_validator(_validator)
        for _validator in def_raw_validators
    ]
    ref_validators = test_dict.pop("validate", [])
    test_dict["validate"] = validator.extend_validators(
        def_validators, ref_validators)

    # merge & override extractors
    def_extrators = api_def_dict.pop("extract", {})
    test_dict["extract"] = utils.extend_variables(def_extrators,
                                                  test_dict.get("extract", {}))

    # merge & override request
    test_dict["request"] = api_def_dict.pop("request", {})

    # base_url & verify: priority api_def_dict > test_dict
    if api_def_dict.get("base_url"):
        test_dict["base_url"] = api_def_dict["base_url"]

    if "verify" in api_def_dict:
        test_dict["request"]["verify"] = api_def_dict["verify"]

    # merge & override setup_hooks
    def_setup_hooks = api_def_dict.pop("setup_hooks", [])
    ref_setup_hooks = test_dict.get("setup_hooks", [])
    extended_setup_hooks = ref_setup_hooks + def_setup_hooks
    if extended_setup_hooks:
        test_dict["setup_hooks"] = extended_setup_hooks
    # merge & override teardown_hooks
    def_teardown_hooks = api_def_dict.pop("teardown_hooks", [])
    ref_teardown_hooks = test_dict.get("teardown_hooks", [])
    extended_teardown_hooks = def_teardown_hooks + ref_teardown_hooks
    if extended_teardown_hooks:
        test_dict["teardown_hooks"] = extended_teardown_hooks

    # TODO: extend with other api definition items, e.g. times
    test_dict.update(api_def_dict)