Writing Testable Code

Writing Testable Code

An introduction to testing and how to manually test by breaking a code into smaller components

Listen to this article

Being active contributors to the development community, the folks at GeekyAnts are innovators who love to research and experiment to come up with groundbreaking tech solutions. One such event led to the recent tech talk that happened where Saurabh Sahu, the CTO of GeekyAnts who has extensive experience in the field, discussed his experience in a group discussion about writing testable code along with exploring the different types of testing and the suitable circumstances for each use case. This insightful article elaborates on the discussions that transpired during the event through an example project where an app is broken down into small parts with test cases written with the intent of improving the code.

What Is Testing?

In software development, testing is the process of checking an application using testing tools and frameworks to identify and fix errors which results in a well-functioning and robust app. In this discussion, the panelists explore the viability of each type of testing and the limitations that come with it. It is a common understanding that not every app can be tested, especially client projects which have deadlines, due to the time constraints that are attached with the assignment due to which developers have to find creative ways to test the product and ensure a successful app. One popular testing process used is the end-to-end testing method wherein the developers try to replicate real life user scenarios to test the functioning of the application on the whole while unit testing involves dealing with the behavior of individual components. In this article, we are going to deal with the topic of unit testing which includes breaking your app into smaller units and testing, creating buttons and checking if each click is successful.

Why Unit Testing?

This method of testing separate units also serves the purpose of strengthening the intent of including that particular component in the app as it shows a working demonstration of the feature. This was further elaborated with an example where the focus of the testing is on the implementation details because of which the logic of the form does not change when a library is updated because of which the attention needs to be brought back to the implementation details in order to ensure an error free app. This brought in another point of concern where complete testing is not possible due to the different testing levels involved in the process because of which the development team should take maximum care to attain the functionalities affecting the code through attentive measures.

The Experiment

To explore more on how to apply unit testing to a certain use case, an experiment was conducted to simplify the booking of vaccination slots on the COWIN platform. Saurabh had noticed the hassles that were being faced by the people of the country were trying to book a slot to get inoculated but the shortage of vaccinations and the unreliability of the COWIN platform in general has made this a task for the majority. He then came across an appointment availability API on the application which could be leveraged to make this process easier. The next assignment was to build an express Node.js application which could break down a few functions which would then send a notification through mail about the availability of slots in the area based on the data that was input. I.e. pin code and email. The purpose of the app would be to run the predefined functions on loop five times a day where a refreshed search would be generated every minute for the next five days.

The Testing

In this experiment, the function was broken down in such a way as to send an email when the processed condition resolves to be true and considering the function is split, this makes it easy to test its functions. This was used by using the JEST framework because of its efficiency in ensuring the accuracy of any JavaScript code. Two integers, i.e. “a” and “b”, were passed through the test framework along with the condition that it should result in a successful answer if it fulfils “a+b” if the app is functioning as intended. When using unit testing to check the send email function, it is also important to check if it's returning the promise when using JEST as the parameters take in both the subject and the text. Next, a JSON package was run on the framework with the necessary dependencies added for JEST based on what to expect and the prospective output. In this scenario, the test case will run through each item in the folder and return the respective output. This is just to test if the function is returning a promise but we can also write an integration which will pass when the email is returned by the user. If the function is returning a result, we can also check for the data that it is returning by mocking APIs. We can use test cases to smoothly enhance any application even in the case that there are two libraries which have the same API surface.

Conclusion

This event was one of the initial sessions to be conducted about testing and its intricacies. While this discussion covered the basics of writing testable code, there are more discussions soon to be held which will cover interesting topics like TypeScript vs Testable code.

You can watch the entire discussion here.

 
Share this