Created
June 14, 2023 17:24
-
-
Save spencerkittleson/f09b6d3647d169873509e916a0cef7cb to your computer and use it in GitHub Desktop.
pytest with ducktyping
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import xml.etree.ElementTree as ET | |
def get_current_weather(uri: str, client = requests) -> str: | |
result = client.get(uri) | |
weather = ET.ElementTree(ET.fromstring(result.text)).getroot().findall("./weather")[0].text | |
return weather | |
if __name__ == '__main__': | |
print(get_current_weather("https://w1.weather.gov/xml/current_obs/KMYF.xml", requests)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import do | |
def test_get_current_weather(): | |
assert do.get_current_weather( | |
"https://w1.weather.gov/xml/current_obs/KMYF.xml", MockRequestsClient) == "foobar" | |
class MockRequestsClient: | |
def get(self): | |
return MockClientResponse("<current_observation><weather>foobar</weather></current_observation>") | |
class MockClientResponse: | |
def __init__(self, get_response): | |
self.text = get_response | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment