Viewing posts by Zulfikar Akbar Muzakki
Python Mocking Introduction
Mocking is a process in unit testing when the test has external dependencies. We isolate our code during the test, without having to worry about the unexpected behavior of the dependencies. For example, we create a routine to save something to Firebase which utilizes 3rd party library called Firestore. There could be problems when saving data to Firebase, like internet connections, wrong configuration, non-existing document, you name it. Instead of testing every possible scenario when saving to Firebase, we only test that Firestore is called with correct parameter, which represents our data. The tests for Firestore itself should have been done by Firestore developer. Hence, we can shift our focus towards the implementation of our code. It also reduces testing time because we don’t need to send our data to Firebase.
Working with Web Data using Requests and Beautiful Soup
Imagine us in charge of developing a feature to show public data coming from a third party that doesn’t have an API. Let’s say, we need to show current and historical data of dam water level which we can access at http://www.dwa.gov.za/Hydrology/Weekly/ProvinceWeek.aspx?region=WC. We cannot possibly add the entry manually--it’s too time consuming. Some of us might choose the hard way to ask the data provider to make a public API, but there is a time constraint as we need it as soon as possible. One way to solve this is to use a method called “web scraping”.
Mocking Requests with requests_mock
Testing is an important part of software development, be it manual or automated. Untested code is a ticking time bomb ready to explode at the worst time possible, hence each developer must be responsible for testing their code. It’s even better when they make automated tests for their code, even if it’s only unit tests.