Home
Skills
Certificates
Blog
CV
Contact
Back to Blogs
The Mystic Art of Writing Stubs
Prakhar Srivastava | June 2, 2024
In programming, we often run into situations where there are dependencies amongst teams working together on the same project. It may also happen when two developers are working on the same feature. **For instance,** Team A builds module X which is going to be utilized by team B which is building feature Y. `X -> Y` In such a situation, if we are the developers from team B, we have two choices: - Develop the feature and hold off unit testing until feature X is done - Or write stubs and complete the development **What is a Stub?** Simply put, it is a dummy implementation which gives you the data structure and value which you are expecting from the dependent module/s. **Why to use Stubs?** Using stubs has invaluable advantages, - Parallel and early development, as the dependent modules can be mocked - Reliable and robust integration as the developers can return a variety of responses to check positive as well as negative (error) scenarios - Early completion of unit testing Writing stubs in the beginning might be a little challenging, however, with practice it becomes second nature. Here is an example enumerating how to write a simple stub. **Let's say,** team A's feature X is an API which returns the number of users who are currently looking at your website. To have a mock implementation, all you have to do is write a function which returns a random integer in order to unit test your code. ##### Team B Calling Team A API ``` def get_user_count(): user_count = None api_endpoint = “https://www.team-a-module.com/get-user-count” headers = { # your headers (auth token etc.) } response = requests.get(url=api_endpoint, headers=headers) # assuming response has a key user_count which gives us the count of users as integer if response.status == 200: response_dict = response.json() user_count = response_dict.get(“user_count”) return user_count ``` However, since this API is still being constructed, you will never get a 200 response from their server. **Stub to the rescue!** ##### Team B Calling Stub to Emulate Team A API ``` def get_user_count_stub(): # return a random integer between one thousand and one crore (ten million) user_count = random.randint(1000, 10000000) return user_count ``` ![](/media/editor\stub_example_20240602191547182517.png) ##### Team B Code Implementation ``` # some script doing a lot of things # call the function with API; Keep it commented if it's not ready; Uncomment later # user_count = get_user_count() user_count = get_user_count_stub() # do something amazing with the user_count you just received ``` Well that's it! You can keep calling this stub to test your code until Team A's API is ready. As soon as they finish their development, revert to the original function which calls their API. Congratulations, you have unit tested and integrated the code without any delays involving dependencies. This is a very simple example and in real world the dependencies may not be this straight forward, but understand, it comes down to what you are expecting as the output from a remote module being developed by a different developer or team, i.e., the data structure and the value. Happy Coding and Thanks for reading😊