Python Newbie- Doctest
Any Software Development process consists of five stages:
1. Requirement Analysis
Though each and every process mentioned above is important in SDLC lifecycle, this post will mainly focus on the importance of testing and enlighten on how we can use doctest a module in python to perform testing.
Importance of testing
We all make mistakes and if left unchecked, some of these mistakes can lead to failures or bugs that can be very expensive to recover from. Testing our code helps to catch these mistakes or avoid getting them into production in the first place.
Testing therefore is very important in software development.
Used effectively, tests help to identify bugs, ensure the quality of the product, and to verify that the software does what it is meant to do.
Python module- doctest
Doctest helps you test your code by running examples embedded in the documentation and verifying that they produce the expected results. It works by parsing the help text to find examples, running them, then comparing the output text against the expected value.
To make things easier, let us start by understanding the above implementation using a simple example:
So, in the above snippet, I have written a basic inline function that adds up a number to itself.
For this function, I run manually a couple of test cases to do some basic verification (to do sanity check of my function).
Now, consider a scenario in which python can read the above output and perform the sanity check for us at the run time. This is exactly the idea behind a doctest.
Now, let’s see how we can implement one.
Let’s take a very simple example to calculate what day of the week it will be ahead of the current weekday.
We write a docstring for our function which helps us to understand what our function does and what inputs it takes and so on. In this docstring, I have added couple of test cases which will be read by the doctest module at the run time while testing is carried out.
When we run the above script from the command, we will get the following output:
We can see from the above snippet that all test cases mentioned in the docstring were successful as the resulted outcome matched with the expected outcome.
But what happens if any test fails, or the script does not behave as expected?
To test this, we add a false test case as we know our script can only take integers as input.
What will happen if we give a string as an input? Let us check out.
I have used the same script but made a small change in the test case where I have passed strings as an input.
So, what is the outcome for the above test case?
Voila! We can see that the doctest has clearly identified the failed test case and listed the reason for the failure of the above-mentioned test case.
That is all you require to get started with doctest. In this post, we have come across the power of doctest and how it makes a lot easier to carry out automated testing for most of the script. Though, many developers find doctest easier as compared to unittest because in its simplest form, there is no API to learn before using it. However, as the examples become more complex, the lack of fixture management can make writing doctest tests more cumbersome than when using unittest. But still due to the ease of its module, doctest is worth adding to our codes.
In upcoming blogs, we are going to discuss more about handy python module that can ease our task and we will also dive into some other concepts like Machine Learning and Deep Learning. Until then, keep learning!