Best Python code snippet using rester_python
exc.py
Source:exc.py  
...91        92        # execute all the assignment statements93        if hasattr(test_step, 'postAsserts') and test_step.postAsserts is not None:          94            for key, value in test_step.postAsserts.items().items():95                self._process_post_asserts(response_wrapper.body, key, value)96        return None97    def _assert_element_list(self, section, failures, test_step, response, assert_list):98        self.logger.debug("Inside assert_element_list: %s", response)99        test_step.assertResults = []100        for key, value in assert_list:101            self.logger.debug('key : %s, value : %s', key, value)102            json_eval_expr = getattr(response, key, '')103            if json_eval_expr is None:104                assert_message = 'assert statement :%s not found in target response', key105                self.logger.error('%s', assert_message)106                failures.errors.append(assert_message)107                continue108            self.logger.debug('---> json_eval_expr : %s and type : %s', json_eval_expr, type(json_eval_expr))109            # check for basic JSON types110            json_types = {'Integer':'int', 'String':'str', 'Array':'list', 'Float':'float', 'Boolean':'bool', 'Object':'dict'}111            if value in json_types:112                self.logger.info('Found json type : %s ', value)113                if type(json_eval_expr) == DictWrapper:114                    value = 'Object'115                    json_eval_expr = {}116                if type(json_eval_expr) == unicode:117                    json_eval_expr = ''118                value = eval(json_types[value])119                json_eval_expr = type(json_eval_expr)120            # Check for logical operators121            logic_ops = {'-gt':'>', '-ge':'>=', '-lt':'<', '-le':'<=', '-ne':'!=', '-eq':'==', 'exec': 'exec'}122            lg_op_expr = check_for_logical_op(value)123            if lg_op_expr:124                self.logger.debug("---> Found lg_op_expr : " + lg_op_expr)125            if lg_op_expr in logic_ops:126                final_lg_op = logic_ops[lg_op_expr]127                value = value[len(lg_op_expr):]128                self.logger.debug("     -----> Rest of the expression : " + value)129            else:130                # - If no operators found then assume '=='131                final_lg_op = logic_ops['-eq']132            self.logger.debug("---> Final final_lg_op : " + final_lg_op)133            # do variable expansion...134            value = self.case.variables.expand(value)135            self.logger.debug(' ---> final evaluated expression  : %s and type %s ', value, type(value))136            if isinstance(json_eval_expr, basestring):137                value = str(value)138            # construct the logical assert expression139            if final_lg_op != 'exec':140                assert_expr = 'json_eval_expr {0} value'.format(final_lg_op)141                assert_literal_expr = "{}:{{{}}} {} {}".format(key, json_eval_expr, final_lg_op, value)142                self.logger.debug('     ---> Assert_expr : ' + assert_expr)143                assert_result = eval(assert_expr)144            else:145                assert_expr = 'exec_result = {0}'.format(value)146                assert_literal_expr = '"f({0}) <- {1}"'.format(json_eval_expr, value)147                exec(assert_expr)148                assert_result = _evaluate(value, json_eval_expr)149            self.logger.debug('assert evaluation result  : %s', assert_result)150            if not assert_result:151                assert_message = '{} Assert Statement : {}   ---> Fail!'.format(section, assert_literal_expr)152                self.logger.error('%s', assert_message)153                failures.errors.append(assert_message)154            else:155                assert_message = '{} Assert Statement : {}  ----> Pass!'.format(section, assert_literal_expr)156                self.logger.info('%s', assert_message)157    def _process_post_asserts(self, response, key, value):158        self.logger.debug("evaled value: {}".format(getattr(response, value, '')))159        self.case.variables.add_variable(key, getattr(response, value, ''))160def _evaluate(clause, value):161    assert_expr = 'result = {0}'.format(clause)162    #self.logger.debug('     ---> Assert_exec : ' + assert_expr)163    exec(assert_expr)164    return result #@UndefinedVariable165def check_for_logical_op(expression):166    if expression and isinstance(expression, basestring):167        #self.logger.debug("_check_for_logical_op : expression %s", expression)168        oprtr_regex = re.compile("-lt|-le|-gt|-ge|-eq|-ne|exec")169        match = re.match(oprtr_regex, expression)170        if match:171            return match.group()Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
