So, by mocking, you end up coupling your test with the implementation.Īlso, another downside is the amount of setup you have to do before calling the function-at least three lines of code. A good test doesn't change when your implementation changes. Every time you change the implementation of find_weather_for, you will have to adapt the test. ConsĪs for cons, the problem is, what if you don't want to use requests anymore and decide to go with the standard library's urllib. Well, one pro already discussed is that by mocking the API's return, you make your tests easier. Isolate the communication with the API and make the test predictable. Tests/test_weather_app.py::test_retrieve_weather_using_mocks PASSED If you run the test, you get the following output: = test session starts = Weather_info = retrieve_weather(city="London")Īssert weather_info = om_dict(fake_weather_info) Mocker.patch("weather_", return_value=fake_resp) # Mock the json method to return the static weather dataįake_resp.json = mocker.Mock(return_value=fake_weather_info) # Creates a fake requests response object """Given a city name, test that a HTML report about the weather is generated Return json.load(f) def test_retrieve_weather_using_mocks(mocker, fake_weather_info): With open("tests/resources/weather.json") as f: """Fixture that returns a static weather data.""" It's just the static data you saved from a previous request. This test uses two fixtures: One is the mocker fixture provided by the pytest-mock plugin. Here's a complete test case for the retrieve_weather function. If you are curious, I discuss more about it in another blog post. This plugin abstracts a bunch of setups from unittest.mock and makes your testing code very concise. To accomplish our mocking goals, use pytest-mock. ![]() The pytest library is very extensible through plugins. pytest-mockįor this tutorial, you'll use pytest as your testing framework of choice. To test the retrieve_weather function, you can then mock requests.get and return static data. In Python, you can mock any object using the unittest.mock lib that is part of the standard library. Testing the API using mocksĪccording to Wikipedia, a mock object is an object that simulates the behavior of a real object by mimicking it. Before moving forward, unit test those functions. Good, you have the basic building blocks for our app. """Finds the weather for a city and returns a WeatherInfo instance.""" def retrieve_weather(city: str) -> WeatherInfo: You'll use this function to call the API and return a WeatherInfo so you can build your HTML page. Now, you'll create a function called retrieve_weather. You can even get information from different sources and combine them in the from_dict method! WeatherInfo:ĭesc: from_dict(cls, data: dict) -> "WeatherInfo": Other parts of the code won't be affected. For example, if the API changes the way it structures the JSON, you can change the logic in just one place, the from_dict method. This is good because you keep the representation stable. This class has a factory method that gets the dictionary and returns a WeatherInfo instance. In order to encapsulate all the details, you can represent them as a dataclass. The data is returned as a Python dictionary when you call resp.json(). You can get everything you need by sending a GET request to: &units=metric" ![]() Fortunately, OpenWeatherMap provides everything you need through its REST API service. To get the information about the weather, you must find it somewhere.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |