Skip to content

Instantly share code, notes, and snippets.

@spencerkittleson
Created June 14, 2023 17:24
Show Gist options
  • Save spencerkittleson/f09b6d3647d169873509e916a0cef7cb to your computer and use it in GitHub Desktop.
Save spencerkittleson/f09b6d3647d169873509e916a0cef7cb to your computer and use it in GitHub Desktop.
pytest with ducktyping
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))
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