Best Python code snippet using pyresttest_python
tests.py
Source:tests.py
...62 datafile_variable_binds = None63 @staticmethod64 def has_contains():65 return 'contains' in validators.VALIDATORS66 def ninja_copy(self):67 """ Optimization: limited copy of test object, for realize() methods68 This only copies fields changed vs. class, and keeps methods the same69 """70 output = Test()71 myvars = vars(self)72 output.__dict__ = myvars.copy()73 return output74 # Template handling logic75 def set_template(self, variable_name, template_string):76 """ Add a templating instance for variable given """77 if self.templates is None:78 self.templates = dict()79 self.templates[variable_name] = string.Template(template_string)80 def del_template(self, variable_name):81 """ Remove template instance, so we no longer use one for this test """82 if self.templates is not None and variable_name in self.templates:83 del self.templates[variable_name]84 def realize_template(self, variable_name, context):85 """ Realize a templated value, using variables from context86 Returns None if no template is set for that variable """87 # val = None88 if context is None or self.templates is None or variable_name not in self.templates:89 return None90 return self.templates[variable_name].safe_substitute(context.get_values())91 # These are variables that can be templated92 def set_body(self, value):93 """ Set body, directly """94 self._body = value95 def get_body(self, context=None):96 """ Read body from file, applying template if pertinent """97 if self._body is None:98 return None99 elif isinstance(self._body, basestring):100 return self._body101 else:102 return self._body.get_content(context=context)103 body = property(get_body, set_body, None,104 'Request body, if any (for POST/PUT methods)')105 NAME_URL = 'url'106 def set_url(self, value, isTemplate=False):107 """ Set URL, passing flag if using a template """108 if isTemplate:109 self.set_template(self.NAME_URL, value)110 else:111 self.del_template(self.NAME_URL)112 self._url = value113 def get_url(self, context=None):114 """ Get URL, applying template if pertinent """115 val = self.realize_template(self.NAME_URL, context)116 if val is None:117 val = self._url118 return val119 url = property(get_url, set_url, None, 'URL fragment for request')120 NAME_HEADERS = 'headers'121 # Totally different from others122 def set_headers(self, value, isTemplate=False):123 """ Set headers, passing flag if using a template """124 if isTemplate:125 self.set_template(self.NAME_HEADERS, 'Dict_Templated')126 else:127 self.del_template(self.NAME_HEADERS)128 self._headers = value129 def get_headers(self, context=None):130 """ Get headers, applying template if pertinent """131 if not context or not self.templates or self.NAME_HEADERS not in self.templates:132 return self._headers133 # We need to apply templating to both keys and values134 vals = context.get_values()135 def template_tuple(tuple_input):136 return (string.Template(str(tuple_item)).safe_substitute(vals) for tuple_item in tuple_input)137 return dict(map(template_tuple, self._headers.items()))138 headers = property(get_headers, set_headers, None,139 'Headers dictionary for request')140 def update_context_before(self, context):141 """ Make pre-test context updates, by applying variable and generator updates """142 if self.variable_binds:143 context.bind_variables(self.variable_binds)144 if self.generator_binds:145 for key, value in self.generator_binds.items():146 context.bind_generator_next(key, value)147 def update_context_after(self, response_body, headers, context):148 """ Run the extraction routines to update variables based on HTTP response body """149 if self.extract_binds:150 for key, value in self.extract_binds.items():151 result = value.extract(152 body=response_body, headers=headers, context=context)153 context.bind_variable(key, result)154 def is_context_modifier(self):155 """ Returns true if context can be modified by this test156 (disallows caching of templated test bodies) """157 return self.variable_binds or self.generator_binds or self.extract_binds158 def is_dynamic(self):159 """ Returns true if this test does templating """160 if self.templates:161 return True162 elif isinstance(self._body, ContentHandler) and self._body.is_dynamic():163 return True164 return False165 def realize(self, context=None):166 """ Return a fully-templated test object167 Warning: this is a SHALLOW copy, mutation of fields will cause problems!168 Can accept a None context """169 if not self.is_dynamic() or context is None:170 return self171 else:172 selfcopy = self.ninja_copy()173 selfcopy.templates = None174 if isinstance(self._body, ContentHandler):175 selfcopy._body = self._body.get_content(context)176 selfcopy._url = self.get_url(context=context)177 selfcopy._headers = self.get_headers(context=context)178 return selfcopy179 def realize_partial(self, context=None):180 """ Attempt to template out what is static if possible, and load files.181 Used for performance optimization, in cases where a test is re-run repeatedly182 WITH THE SAME Context.183 """184 if self.is_context_modifier():185 # Don't template what is changing186 return self...
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!!