How to use describe_to method in PyHamcrest

Best Python code snippet using PyHamcrest_python

matchers.py

Source:matchers.py Github

copy

Full Screen

...10 super().__init__()11 self.status_code = status_code12 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)...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run PyHamcrest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful