Best Python code snippet using pyresttest_python
validator_ex.py
Source:validator_ex.py
...49 comparator = None50 comparator_name = ""51 expected = None52 isTemplateExpected = False53 def get_readable_config(self, context=None):54 """ Get a human-readable config string """55 string_frags = list()56 string_frags.append(57 "Extractor: " + self.extractor.get_readable_config(context=context))58 if isinstance(self.expected, validators.AbstractExtractor):59 string_frags.append("Expected value extractor: " +60 self.expected.get_readable_config(context=context))61 elif self.isTemplateExpected:62 string_frags.append(63 'Expected is templated, raw value: {0}'.format(self.expected))64 return os.linesep.join(string_frags)65 def validate(self, body=None, headers=None, context=None):66 try:67 extracted_val = self.extractor.extract(68 body=body, headers=headers, context=context)69 except Exception as e:70 trace = traceback.format_exc()71 return validators.Failure(message="Extractor threw exception", details=trace, validator=self, failure_type=validators.FAILURE_EXTRACTOR_EXCEPTION)72 # Compute expected output, either templating or using expected value73 file_name = None74 if self.isTemplateExpected and context:75 file_name = string.Template(76 self.expected).safe_substitute(context.get_values())77 else:78 file_name = self.expected79 expected_val = None80 expected_file_name = file_name + PATTERN_FILE_EXT81 output_file_name = file_name + OUTPUT_FILE_EXT82 try:83 with open(expected_file_name, "r") as f:84 expected_val = json.load(f)85 except Exception as e:86 trace = traceback.format_exc()87 dump_output(output_file_name, extracted_val)88 return validators.Failure(message="Cannot load pattern file {0}.".format(expected_file_name), details=trace, validator=self, failure_type=validators.FAILURE_VALIDATOR_EXCEPTION)89 # Handle a bytes-based body and a unicode expected value seamlessly90 if isinstance(extracted_val, binary_type) and isinstance(expected_val, text_type):91 expected_val = expected_val.encode('utf-8')92 comparison = self.comparator(extracted_val, expected_val)93 if not comparison:94 failure = validators.Failure(validator=self)95 failure.message = "Comparison failed, evaluating {0}({1}, {2}) returned False".format(96 self.comparator_name, extracted_val, expected_val)97 failure.details = self.get_readable_config(context=context)98 failure.failure_type = validators.FAILURE_VALIDATOR_FAILED99 dump_output(output_file_name, extracted_val)100 return failure101 else:102 return True103 @staticmethod104 def parse(config):105 """ Create a validator that does an extract from body and applies a comparator,106 Then does comparison vs expected value107 Syntax sample:108 { jsonpath_mini: 'node.child',109 operator: 'eq',110 expected: 'my_file_name'111 }...
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!!