Best Python code snippet using PyHamcrest_python
matchers.py
Source:matchers.py
...12 def _matches(self, item: Response) -> bool:13 return self.status_code == item.status_code14 def describe_to(self, description: Description) -> None:15 description.append_description_of(f"response status code is {self.status_code}")16 def describe_mismatch(self, item: Response, mismatch_description: Description) -> None:17 super().describe_mismatch(f"{item.status_code} - {item.json()}", mismatch_description)18class ResponseContentMatcher(BaseMatcher):19 def __init__(self, content: Any) -> None:20 super().__init__()21 self.content = content22 def _matches(self, item: Response) -> bool:23 return self.content == item.content24 def describe_to(self, description: Description) -> None:25 description.append_description_of(f"response content is: {self.content}")26 def describe_mismatch(self, item: Response, mismatch_description: Description) -> None:27 super().describe_mismatch(f"{item.content}", mismatch_description)28class ResponseJsonContentMatcher(BaseMatcher):29 def __init__(self, content) -> None:30 super().__init__()31 self.matcher = has_entries(content)32 def _matches(self, item: Response) -> bool:33 return self.matcher.matches(item.json())34 def describe_to(self, description: Description) -> None: # pragma: no cover35 description.append_description_of(f"response JSON content is: {self.matcher}")36 def describe_mismatch(self, item: Response, mismatch_description: Description) -> None:37 super().describe_mismatch(item.json() if item.json() else item, mismatch_description)38class ConnectionMatcher(BaseMatcher):39 def __init__(self, property_name: str, matcher: Matcher) -> None:40 super().__init__()41 self.property_name = property_name42 self.matcher = matcher43 def _matches(self, item: Connection) -> bool:44 return self.property_name in item.__dict__ and \45 self.matcher.matches(item.__getattribute__(self.property_name))46 def describe_to(self, description: Description) -> None:47 description.append_text(f"{self.property_name} is ")48 self.matcher.describe_to(description)49 def describe_mismatch(self, item: Response, mismatch_description: Description) -> None:50 super().describe_mismatch(item.__getattribute__(self.property_name), mismatch_description)51def has_username(matcher: Matcher):52 return ConnectionMatcher(property_name="username", matcher=matcher)53def has_password(matcher: Matcher):54 return ConnectionMatcher(property_name="password", matcher=matcher)55def has_endpoint(endpoint: str, matcher: Matcher):56 return ConnectionMatcher(property_name="endpoints", matcher=has_entry(endpoint, matcher))57def has_protocol(matcher: Matcher):58 return has_endpoint("protocol", matcher)59def has_base(matcher: Matcher):60 return has_endpoint("base", matcher)61def has_status_code(status_code: int):62 return ResponseStatusCodeMatcher(status_code=status_code)63def has_json_content(content: dict):64 return ResponseJsonContentMatcher(content=content)
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!!